Java program design fourth job content fifth job was released in October 9, for the third chapter all examples, java program design
Question 6: What is the day of the week when the corresponding Chinese words are output based on numbers using the judgment statement?
Directly Using an if statement
Int weekDay = 3;
If (weekDay = 1 ){
Sop ("Today is Monday ");
}
If (weekDay = 2 ){
Sop ("Today is Tuesday ");
}
If (weekDay = 3 ){
Sop ("Today is Wednesday ");
}
Use the associated if statement
If (weekDay = 1 ){
Sop ("Today is Monday ");
} Else if (weekDay = 2 ){
Sop ("Today is Tuesday ");
} Else if (weekDay = 3 ){
Sop ("Today is Wednesday ");
} Else {
Sop ("No such day ");
}
Use the if statement to do an exercise: one year has 12 months, in which spring is 3, 4, 5, summer is 6, 7, 8, autumn is 9, 10, 11 Winter is 12, 1, 2
Int month = 8; // you can enter a custom number on the keyboard.
Question 7: Use the branch statement, calculate +-*/%, and use the switch statement
int a=84,b=3; char option='%'; switch (option) { case '+': System.out.println("a+b="+(a+b)); break; case '-': System.out.println("a-b="+(a-b)); break; case '*': System.out.println("a*b="+(a*b)); break; case '/': System.out.println("a/b="+(a/b)); break; case '%': System.out.println("a%b="+(a%b)); break; default: System.out.println("a%b="+(a%b)); break; }
Can the branches be freely adjusted?
Can the branch content be omitted?
Use the switch statement to do an exercise: there are 12 months in a year, in which spring is 3, 4, 5, summer is 6, 7, 8, autumn is 9, 10, 11 Winter is 12, 1, 2
Question 8: How can I analyze the output results of a program using a loop statement?
What is the difference between do while and while?
Int x = 1;
Do {
Sop ("x =" + x );
X ++;
} While (x <1 );
Int y = 1;
While (y <1 ){
Sop ("y =" + y );
Y ++;
}
Question 9: How can I use for to write a simple loop statement to analyze the output result of a program?
For (int x = 1; x <3; x ++)
{
Sop ("x =" + x );
}
For (sop ("a"); sop ("B"); sop ("c ")
{
Sop ("d ");
}
Int x = 1; for (System. out. println ("a"); x <3; System. out. println ("c") System. out. println ("d"); x ++