赞
踩
(1)if…else
public class HEllo {
public static void main (String[] args){
int a = 10,b = 20,max;
if (a>b){
max = a;
}
else {
max = b;
}
System.out.println(max);
}
}
(2)if…else if …else
(3)switch—case
public class HEllo { public static void main (String[] args){ int a = 2; switch (a){ case 1: System.out.println("开心"); break; case 2: System.out.println("幸福"); break; default: System.out.println("不正确"); } } }
public class HEllo {
public static void main (String[] args){
if (true){
int b = 20;
System.out.println(b);
}
}
}
//累加1-100
public class HEllo {
public static void main (String[] args){
int sum = 0;
for (int num = 1; num <= 100;num++){
sum += num;
}
System.out.println(sum);
}
}
break:跳出循环,continue:跳出这次循环,进行下次循环
public class HEllo {
public static void main (String[] args){
String str = "hello";
System.out.println(str);
//字符串拼接+
str = "he"+"llo"+123;
System.out.println(str);//hello123
//如果需要读取一个字符
String strr = "hello";
char c = strr.charAt(0);
System.out.println(c);
}
}
//如果需要控制台输入,需添加此句话; import java.util.*; public class HEllo { public static void main (String[] args){ //在控制台输入内容 Scanner scanner = new Scanner(System.in); //读取在控制台输入的内容,并给line进行赋值。 String line = scanner.nextLine(); System.out.println(line); int num = scanner.nextInt(); System.out.println(num); // scanner.nextDouble(); // scanner.nextFloat(); // scanner.nextBoolean(); } }
public class HEllo {
public static void main (String[] args){
int sum = 0,num = 1;
while (num <= 100){
sum += num++;
}
System.out.println(sum);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。