1. 键盘输入五位数字的会员卡号:38503
使用“/”和“%”运算符分解获得的会员卡每一位上的数字
将每一位数字求和
package com.company;
import java.util.Scanner;
public class Main {
public static void fun(){
System.out.println("请输入五位会员卡号:");
Scanner scanner = new Scanner(System.in);
int num;
do{
num = scanner.nextInt();
if(num % 100000 !=0){
System.out.println("输入有误!");
continue;
}else{
break;
}
}while(true);
int num1 = num/10000;
int num2 = num/1000%10;
int num3 = num/100%10;
int num4 = num/10%10;
int num5 = num%10;
System.out.println("万位数:"+num1);
System.out.println("千位数:"+num2);
System.out.println("百位数:"+num3);
System.out.println("十位数:"+num4);
System.out.println("个位数:"+num5);
}
public static void main(String[] args) {
// write your code here
fun();
}
}
2. 键盘输入学生学号:20161103(2016 年+11 期+03 号)
使用“/”和“%”运算符分解学生学号获得年份期数和序号
并且输出
学生学号:20161103
学生入学年份:2016 年
学生期数:11 期
学生序号:03 号
package com.company;
import java.util.Scanner;
public class Main {
public static void fun(){
System.out.println("请输入学生学号:");
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
int num1 = num/10000;
int num2 = num/100%100;
int num3 = num%100;
System.out.println("学生学号:"+num);
System.out.println("学生入学年份:"+num1);
System.out.println("学生期数:"+num2);
System.out.println("学生序号:"+num3);
}
public static void main(String[] args) {
// write your code here
fun();
}
}
3. 编写万年历小程序
package com.company;
import java.util.Scanner;
public class Main {
public static boolean isLeapYear(int year){
boolean flag1 = (year%4==0);
boolean flag2 = (year%100==0);
boolean flag3 = (year%400==0);
if((flag1&&!flag2)||(flag3)){
return true;
}
return false;
}
private static int getDays(int month,boolean isLeap){
int days = 30;
switch (month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days++;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
days = 28;
if(isLeap)days++;
break;
}
return days;
}
public static void fun(){
System.out.println("请输入年:");
Scanner scanner = new Scanner(System.in);
int year = scanner.nextInt();
System.out.println("请输入月:");
int month = scanner.nextInt();
boolean isLeap = isLeapYear(year);
String string1 = isLeap ? "闰年" : "平年";
System.out.println(year+"年是"+string1);
int days = getDays(month,isLeap);
System.out.println(year+"年"+month+"月天数:"+days);
}
public static void main(String[] args) {
// write your code here
fun();
}
}
4.循环录入 5 个学生 Java 课的学生成绩,统计分数大于等于 80 分的学生比例
package com.company;
import java.util.Scanner;
public class Main {
public static void fun(){
int count = 0;
for(int i = 1 ; i <= 5 ; i++){
System.out.println("请输入第"+i+"位同学的成绩");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
if(n>=80){
count++;
}
}
System.out.println("超过80分以上的学生人数:"+count);
System.out.println("超过80分以上的学生比例:"+count*100/5+"%");
}
public static void main(String[] args) {
// write your code here
fun();
}
}