switch statement
- Depending on the value of the switch, it will perform the corresponding case statement. Break will encounter the end of the case statement.
- If the value of the switch is no matching case, will perform the default statements.
- We recommend a switch statement must bring the best default.
We see an example
public class Solution {
public static void main(String[] args) {
System.out.println("value="+switchit(4));
}
public static int switchit(int x) {
int j=1;
switch (x) {
case 1:j++;
break;
case 2:j++;
break;
case 3:j++;
break;
case 4:j++;
break;
case 5:j++;
break;
default:j++;
break;
}
return j+x;
}
}
If you remove the break, we will look at
public class Solution {
public static void main(String[] args) {
System.out.println("value="+switchit(4));
}
public static int switchit(int x) {
int j=1;
switch (x) {
case 1:j++;
case 2:j++;
case 3:j++;
case 4:j++;
case 5:j++;
default:j++;
}
return j+x;
}
}
The results and not the same as before, the reason is because they do not break when writing, case statement will in turn execute down, thus losing the multi-drop effect.
The while loop
Note:
- And if similar, while the following statement {} can not write, but do not write when only support a statement. Plus recommendations or {}
- And if similar, while {behind the proposals and while on the same line.
- And if similar, do not write back while semicolon, or it may result in a cycle can not be performed correctly
for the cycle
to calculate 1! + 2! +3! +4! +5!
public class Solution {
public static void main(String[] args) {
int sum = 0;
for(int i = 0;i <= 5;i++) {
int temp = 1;
for(int j = 1;j <= i;j++) {
temp *= j;
}
sum += temp;
}
System.out.println("sum =" + sum);
}
}
Notes (and while loops similar)
- And if similar, for the following statement {} can not write, but do not write when only support a statement. Plus recommendations or {}
- And if similar, for {behind the recommendation and while on the same line.
- And if similar, for the back do not write a semicolon, or it may result in a cycle can not be executed properly
do while loop
do {
循环语句
}while(判断条件)
First execution loop, and then judges that the circulation condition.
More recommendation for or while
Output to the console
System.out.println(msg); // 输出一个字符串, 带换行
System.out.print(msg); // 输出一个字符串, 不带换行
System.out.printf(format, msg); // 格式化输出
From the keyboard
- A character is read
directly System.in.read can be read into a character but needs with exception handling.
import java.io.IOException;
public class Solution {
public static void main(String[] args) {
try {
System.out.print("Enter a Char:");
char i = (char) System.in.read();
System.out.println("your char is :"+i);
} catch (IOException e) {
System.out.println("exception");
}
}
}
Too much trouble, we do not recommend the use of
- Use Scanner to read the string / integer / floating point
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的名字:");
String name = sc.nextLine();
System.out.println("请输入你的年龄:");
int age = sc.nextInt();
System.out.println("请输入你的工资:");
float salary = sc.nextFloat();
System.out.println("你的信息如下:");
System.out.println("姓名:"+name+"\n"+
"年龄:"+age+"\n"+"工资:"+salary);
sc.close();
}
}
- Scanner using N digital read cycle
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double sum = 0.0;
int num = 0;
while (sc.hasNextDouble()) {
double tmp = sc.nextDouble();
sum += tmp;
num++;
}
System.out.println("sum = " + sum);
System.out.println("avg = " + sum / num);
sc.close();
}
}
Note: When entering multiple cycles of data, use ctrl + z to end the input (on Windows using the ctrl + z, Linux / Mac use + Ctrl
d)