要求用户输入两个整数,判断第一个整数是否是第二个整数的倍数。
提示:使用运算符%
考察点:模%、if语句
package t1;import java.util.Scanner;public class TestModel{ public static void main (String []args){ Scanner sc=new Scanner(System.in); //读入数据 System.out.print("请输入第一个整数:"); int i=sc.nextInt(); //输入第一个整数i System.out.print("请输入第二个整数:"); int j=sc.nextInt(); //输入第二个整数j if(i%j==0){ //判断i Mol j 是否等于0,等于0,输出i是j的i/j倍! System.out.println(i+"是"+j+"的"+i/j+"倍!"); }else{ //否则输出i不是j的倍数! System.out.println(i+"不是"+j+"的"+"倍数!"); } } }
要求用户输入一个年份和一个月份,判断(要求使用嵌套的if…else和switch分别判断一次)该年该月有多少天。
考察点:if else、switch、闰年的判断条件
package t1;import java.util.Scanner;public class TestDay{ public static void main (String []args){ Scanner sc=new Scanner(System.in); //读入数据 System.out.print("请输入年份:"); int i=sc.nextInt(); //输入年份i System.out.print("请输入月份:"); int j=sc.nextInt(); //输入月份j if((i%4==0&&i%100!=0)||(i%400==0)){ if(j==2){ System.out.println(i+"年"+j+"月有29天!"); }else if(j==4||j==6||j==9||j==11){ System.out.println(i+"年"+j+"月有30天!"); }else{ System.out.println(i+"年"+j+"月有31天!"); } }else{ if(j==2){ System.out.println(i+"年"+j+"月有28天!"); }else if(j==4||j==6||j==9||j==11){ System.out.println(i+"年"+j+"月有30天!"); }else{ System.out.println(i+"年"+j+"月有31天!"); } } System.out.print("请输入年份:"); int i1=sc.nextInt(); //输入年份i1 System.out.print("请输入月份:"); int j1=sc.nextInt(); //输入月份j1 switch(j1){ case 1: case 3: case 5: case 7: case 8: case 10: case 12:System.out.println(i1+"年"+j1+"月有31天!");break; case 4: case 6: case 9: case 11:System.out.println(i1+"年"+j1+"月有30天!");break; case 2:if((i%4==0&&i%100!=0)||(i%400==0)){ System.out.println(i1+"年"+j1+"月有29天!"); }else{ System.out.println(i1+"年"+j1+"月有28天!"); } break; default:System.out.println("输入有误!");break; } } }
要求用户输入一个学生的分数(1~100),使用switch结构判断该分数属于什么等级(A、B、C、D、F)。
提示:switch(score/10)
考察点:switch语句、int
package t1;import java.util.Scanner;public class TestScore{ public static void main(String []args){ System.out.print("请输入你的分数(0~100):"); Scanner sc=new Scanner(System.in);//读入数据 int i=sc.nextInt(); switch(i/10){//整型变量/10=整数 case 10:if(i>100){ System.out.println("你的成绩输入有误!"); }break; case 9:System.out.println("你的成绩是A,优秀!");break; case 8:System.out.println("你的成绩是B,良好!");break; case 7:System.out.println("你的成绩是C,良!");break; case 6:System.out.println("你的成绩是D,及格!");break; case 5: case 4:System.out.println("你的成绩是E,加油啊!");break; case 3: case 2: case 1: case 0:System.out.println("你的成绩是F,该努力了!");break; default:System.out.println("你的成绩输入有误!");break; } }}
以上就是长沙牛耳教育java培训机构的小编针对“Java流程控制语句练习题”的内容进行的回答,希望对大家有所帮助,如有疑问,请在线咨询,有专业老师随时为你服务。