else if语句编写
public static void main(String[]args) {
Scanner sc=new Scanner(System.in);//创建扫描器
System.out.print("请输入您的奖号:");
int num=sc.nextInt();/*将键盘输入的值赋值给变量*/
System.out.printf("恭喜获得%d等奖:",num);
if(num==1) /*如果输入为1输出以下结果*/{
System.out.println("42寸彩电");//输出
}
else if(num==2)/*如果输入为2输出以下结果*/ {
System.out.println("光波炉");//输出
}
else if(num==3) /*如果输入为3输出以下结果*/{
System.out.println("加湿器");//输出
}
else if(num==4) /*如果输入为4输出以下结果*/{
System.out.println("16G-U盘");//输出
}
else//如果都不是
System.out.println("感谢参与");//输出
sc.close();//关闭扫描器
}
}
switch语句:
public class 第一题_2 {
public static void main(String[]args) {
Scanner sc=new Scanner(System.in);//创建扫描器
System.out.print("请输入您的奖号:");
int num=sc.nextInt();/*将键盘输入的值赋值给变量*/
System.out.printf("恭喜获得%d等奖:",num);
switch(num) {
case 1: System.out.println("42寸彩电");/*如果输入为1输出以下结果*/
break;
case 2:System.out.println("光波炉");/*如果输入为2输出以下结果*/
break;
case 3:System.out.println("加湿器");/*如果输入为3输出以下结果*/
break;
case 4:System.out.println("16G-U盘");/*如果输入为4输出以下结果*/
break;
default:
System.out.println("谢谢参与");
}
sc.close();
}
}
第四题
package third;
import java.util.Scanner;
public class yz {
public static void main(String[] args) { //主方法
int a = 0; //定义a并赋值
for(int i = 1;i <= 100;i++) { //for循环
if(i % 2 == 0) { //如果i除以2余零
a = a + i; //
}
}
System.out.println(a); //输出 a
}
}
第五题
package third;
import java.util.Scanner;
public class yz {
public static void main(String[] args) { //主方法
Scanner sc = new Scanner(System.in); //创建扫描器,获取值
int b = 1; //定义int变量并赋值
for(int a = 1;a <= 10;a++) { //for循环
int c = sc.nextInt(); //将输入的值赋值给c
b = b * c ;
}
System.out.println(b); //输出b的值
}
}
第六题
package third;
import java.util.Scanner;
public class yz {
public static void main(String[] args) { //主方法
for(int a = 1;a <= 9;a++) { //for循环
if(a == 6) { //如果a等于六
continue; //跳过本次循环
}
System.out.println(a); //输出a的值
}
}
第七题
package third;
import java.util.Scanner;
public class yz {
public static void main(String[] args) { //主方法
Scanner sc = new Scanner(System.in); //创建扫描器,获取键盘的值
System.out.println("请输入一个正整数:"); //输出"请输入一个正整数:"
int a = sc.nextInt(); //获取键盘的值
int b,c; //定义变量b,c
for(b = 1;b <= a;b++) { //外循环
for(c = 2;c < b;c++) { //内循环
if(b % c == 0) { //如果b除以c余0
break; //跳过本次循环
}
}
if(b == c) { //如果c等于b
System.out.println(b); //输出b的值
}
}
第八题
package third;
import java.util.Scanner;
public class yz {
public static void main(String[] args) { //主方法
for(int i = 1;i <= 9;i++) { //外循环
for(int j = 1;j <= i;j++) { //内循环
System.out.print(j + "*" + i + "=" + i*j + ' ');//输出语句
}
System.out.println(); //换行
}
}
第九题
package third;
import java.util.Scanner;
public class yz {
public static void main(String[] args) { //主方法
Scanner sc = new Scanner(System.in); //创建扫描器,获取内容
System.out.println("请输入一个年份n:"); //输出"请输入一个年份n:"
int year = sc.nextInt(); //用int类型变量year来接收输入内容
for(int a = 1949;a <= year;a++) { //for循环
if(a % 4 == 0 && a % 100 != 0 || a % 400 == 0){ //判断条件
System.out.println(a); //输出a的值
}
}