7、题目:求1~100之间,所有双数之和,和说有单数之和。/*** 创建一个类*/publicclass Lesson {/*** 创建一个main方法*/
public static voidmain(String[] args) {//定义双数和,单数和
int i=0;int a=0;//用for循环写出1到100的所有值
for (int x=1;x<=100;x++){//用%摸分辨出是双数或者单数
if(x%2==0){
i+=x;
}else if (x%2!=0){
a+=x;
}
}
System.out.println("双数和:"+i);
System.out.println("单数和:"+a);
}
}8、在写 swit case语句时,如果未加break,后面的都会执行。9、swit case语句最后面的default分支不需要加break,default需要写在最后一行,如果不在最后一行,需要加break。10、 在确定固定值时候用swit case 语句块,不确定值时候用if…else if…else if…语句。(例如)
publicclass Lesson2 {public static voidmain(String[] args) {
String A="好";switch(A){case "好":
System.out.println("男");break;case "不好":
System.out.println("女");break;default:
System.out.println("不存在");
}
}
}11、 Case 必须是固定值或者字符串。12、 题目:随机生成一个数1—9,这个数分四个等级奖励/*** 创建一个类*/publicclass Ti1 {/*** 创建一个main方法*/
public static voidmain(String[] args) {//随机生成一个数N(1-9)
int a=(int)(Math.random()*9+1);//使用if...else if ...进行输出a
System.out.println(a);if(a>=8){
System.out.println("一等奖");
}else if (a>=5 && a<8){
System.out.println("二等奖");
}else if (a>=3 && a<5){
System.out.println("三等奖");
}else{
System.out.println("安慰奖");
}
}
}13、 题目:在控制台输出一个三位数,三位数的个十百位进行从大到小,从新组合。
importjava.util.Scanner;/*** 创建一个类*/publicclass A2 {/*** 创建一个main方法*/
public static voidmain(String[] args) {//建立Scanner的一个对象
Scanner scanner=newScanner(System.in);//控制台上显示:输入任意三位数
System.out.println("输入任意三位数");//输入的为字符串是显示"你的输入有误"
if(scanner.hasNextInt() ) {//输入这个三位数
int x =scanner.nextInt();//个位数
int c = x % 10;//百位数
int a = x / 100;//十位数
int b = x / 10 % 10;//用if...else if...来比较三位数的大小,进行排序
if (a >= b && a >= c&& b >=c) {
System.out.println((a* 100) +(b * 10) +c);
}else if (a >= b && a>= c && c >=b) {
System.out.println((a* 100) +(c * 10) +b);
}else if (b >= a && b>= c && a >=c) {
System.out.println((b* 100) +(a * 10) +c);
}else if (b >= a && b>= c && c >=a) {
System.out.println((b* 100) +(c * 10) +a);
}else if (c >= a && c>= b && b >=a) {
System.out.println((c* 100) +(b * 10) +a);
}else if (c >= a && c>= b && a >=b) {
System.out.println((c* 100) +(a * 10) +b);
}
}else{
System.out.println("你的输入有误");
}
}
}