一、使用 switch 把小写类型的 char型转为大写。只转换 a, b, c, d, e. 其它的输
出 “other”。
提示:String word = scan.next(); char c = word.charAt(0); switch(c){}
public class test1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入您想要转换的字母:");
String s1 = scan.next();
//从键盘输入的是字符串,题目要求是char型转换。所以在这里需要将字母转换成char型
//在这里考虑的是用户只输入字母,若希望用户输入字符串我想的是现将用户输入的字符串转
//换成数组或集合
//之后再遍历,在遍历内部将字母转换成char型的。具体之后实现~~
//铁铁有其他想法评论区一起讨论呀!
char s2 = s1.charAt(0);
switch (s2){
case 'a':
System.out.println("A");
break;
case 'b':
System.out.println("B");
break;
case 'c':
System.out.println("C");
break;
case 'd':
System.out.println("D");
break;
case 'e':
System.out.println("E");
break;
default:
System.out.println("other");
break;
}
}
二、对学生成绩大于60分的,输出“合格”。低于60分的,输出“不合格”
public class test1 {
public static void main(String[] args) {
/*
对学生成绩大于60分的,输出“合格”。低于60分的,输出“不合格”
*/
Scanner scan = new Scanner(System.in);
System.out.println("请输入该同学的成绩检验是否合格:");
int score = scan.nextInt();
if(score<0 && score > 100){
System.out.println("抱歉,您输入的数据非法,请重新输入");
}
int i = score / 10;
switch (i){
case 1:
System.out.println("不合格");
break;
case 2:
System.out.println("不合格");
break;
case 3:
System.out.println("不合格");
break;
case 4:
System.out.println("不合格");
break;
case 5:
System.out.println("不合格");
break;
case 6:
System.out.println("合格");
break;
case 7:
System.out.println("合格");
break;
case 8:
System.out.println("合格");
break;
case 9:
System.out.println("合格");
break;
}
}
}
三、从键盘分别输入年、月、日,判断这一天是当年的第几天
public class test1 {
public static void main(String[] args) {
/*
三、从键盘上输入某年的“month”和“day”,要求通过程序 输出输入的日期为2019年的第几天。
*/
Scanner scan = new Scanner(System.in);
System.out.println("请输入年:");
int year = scan.nextInt();
System.out.println("请输入月:");
int month = scan.nextInt();
System.out.println("请输入日:");
int day = scan.nextInt();
//闰年的判断方法:在java代码中使用if语句通过判断年份是否能被4整除,
// 并且不能被100整除或是否能被400整除来判断是否是闰年。
boolean isRun = false;
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){
isRun = true;
}
int count = 0;
switch(month){
case 12:
count += 30;
case 11:
count += 31;
case 10:
count += 30;
case 9:
count += 31;
case 8:
count += 31;
case 7:
count += 30;
case 6:
count += 31;
case 5:
count += 30;
case 4:
count += 31;
case 3:
if(isRun) {
count += 29;
}else{
count += 28;
}
case 2:
count += 31;
case 1:
count += day;
}
System.out.println(year + "年" + month + "月" + day + "日是这一年的第" + count + "天;");
}
}