java入门第四天
The fourth day of entry
/*
switch statement format
Format description:
Expression: 1: The value is byte .short, int, char, it can be enumerated after JDK5, and it can be String after JDK7.
2: case: What follows is the value to be compared with the expression.
案例:春夏秋冬
*/
//case penetration (break)
import java.util.Scanner;
public class day04{ public static void main (String[] args){ //Class SwitchTest is public and should be declared in a file named SwitchTest.java- ---The class name and file name must be consistent public class SwitchTest{ Scanner sc =new Scanner(System.in); System.out.println("Please enter a month"); int month =sc.nextInt(); switch(month ){
case 1 :
case 2 :
case 12 :
System.out.println("冬季");
break;
case 5 :
case 3:
case 4:
System.out.println("春季");
break;
case 6:
case 7 :
case 8 :
System.out.println("夏季");
break;
case 10:
case 11:
case 9 :
System.out.println("秋季");
break;
default:
System.out.println("你输入的月份有误");
break;
}
}
}
for statement
for loop
sentence
requirements: output 1-5 and 5-1 data on the console
*/
/*
public class ForTest{
public static void main(String[] args){
for(int i=1; i<=5;i++){
System.out.println(i);
}
}
}
/
/
public class ForTest{
public static void main(String[] args){
for(int i=5; i>=1;i–){
System.out.println(i);
}
}
}
*/
/*
Sum:
Demand: Find the sum of the data between 1-5, and output the result of the sum on the print station;
*/
public class WWW{
public static void main (String[] args) {
//求和的最终结果必须保存起来,需要定义一个变量,用于保存求和的结果,初始值为0;
int sum = 0 ;
//从1开始到5结束的数据使用循环结构完成/
for(int i = 1;i<=5;i++){
//将反复进行的事情写入循环结构内部,此处反复进行的事情是将数据i加到用于保存最终的变量sum中
sum += i;
} //for 语句最好推荐用括括 如果是两语句没有使用括就会报错:错误: 非法的类型开始
//当循环结构结束是,最终将数据打印输出。
System.out.println("1-5数据之间的和是 :"+ sum);
}
//System.out.println("1-5数据之间的和是" + sum);错误: 需要<标识符>
}