使用 switch 把小写类型的 char型转为大写【没有写对,我想复杂了】
使用 switch 把小写类型的 char型转为大写。只转换 a, b, c, d, e. 其它的输
出 “other”。
提示:String word = scan.next(); char c = word.charAt(0); switch©{}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入一个字符");
String word = scanner.next();
char c = word.charAt(0);
switch (c){
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;
default:
System.out.println("Other");
}
}
根据用于指定月份,打印该月份所属的季节。
3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入月份:");
int month = scanner.nextInt();
switch (month){
case 3:
case 4:
case 5:
System.out.println("春季");
break;
case 6:
case 7:
case 8:
System.out.println("夏季");
break;
case 9:
case 10:
case 11:
System.out.println("秋季");
break;
case 12:
case 1:
case 2:
System.out.println("冬季");
break;
default:
System.out.println("没有这个月份");
}
}
编写程序:从键盘上读入一个学生成绩,存放在变量score中,根据score的值输出其对应的成绩等级:
score>=90 等级: A
70<=score<90 等级: B
60<=score<70 等级: C
score<60 等级: D
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入学生成绩");
double score = scanner.nextDouble();
if(score < 0 || score > 100){
System.out.println("不合法的输入");
return;
}
int g = (int)score / 10;
switch (g){
case 10:
case 9:
System.out.println("A");
break;
case 8:
case 7:
System.out.println("B");
break;
case 6:
System.out.println("C");
break;
default:
System.out.println("D");
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入学生成绩");
double score = scanner.nextDouble();
if(score < 0 || score > 100){
System.out.println("不合法的输入");
return;
}
if (score >= 90){
System.out.println("A");
}else if(score >= 70 && score < 90){
System.out.println("B");
}else if(score >= 60 && score < 70){
System.out.println("C");
}else{
System.out.println("D");
}
}
中国生肖
编写一个程序,为一个给定的年份找出其对应的中国生肖。中国的生肖基于12年一个周期,
每年用一个动物代表:rat、ox、tiger、rabbit、dragon、snake、horse、sheep、monkey、
rooster、dog、pig。
提示:2019年:猪 2019 % 12 == 3
public static String calZodiac(int year)
{
String zodiac="";
int remainder=year%12;
switch(remainder){//运用switch...case
case 0:
zodiac="申猴";//monkey
break;
case 1:
zodiac="酉鸡";
break;
case 2:
zodiac="戌狗";//2018年属于狗年
break;
case 3:
zodiac="亥猪";
break;
case 4:
zodiac="子鼠";
break;
case 5:
zodiac="丑牛";
break;
case 6:
zodiac="寅虎";//tiger
break;
case 7:
zodiac="卯兔";//rabbit
break;
case 8:
zodiac="辰龙";//dragon
break;
case 9:
zodiac="巳蛇";//snake
break;
case 10:
zodiac="午马";
break;
case 11:
zodiac="未羊";
break;
}
return zodiac;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入学生成绩");
int year = scanner.nextInt();
System.out.println(calZodiac(year));
}
编写程序:2019年的第几天。
从键盘上输入2019年的“month”和“day”,要求通过程序输出输入的日期为2019年的第几天。
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入年:");
int year = scanner.nextInt();
System.out.println("输入月:");
int month = scanner.nextInt();
System.out.println("输入日:");
int day = scanner.nextInt();
int sum = 0;
switch (month - 1) {
case 11:
sum += 30;
case 10:
sum += 31;
case 9:
sum += 30;
case 8:
sum += 31;
case 7:
sum += 31;
case 6:
sum += 30;
case 5:
sum += 31;
case 4:
sum += 30;
case 3:
sum += 31;
case 2:
sum += 28;
case 1:
sum += 31;
case 0:
sum += day;
}
if (month > 2) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
sum++;
}
}
System.out.println("这是这年的第: " + sum + " 天");
}
https://www.cnblogs.com/ys99/p/9196453.html
从键盘分别输入年、月、日,判断这一天是当年的第几天
注:判断一年是否是闰年的标准:
1)可以被4整除,但不可被100整除
或 2)可以被400整除
public static int getDay(int year, int month, int day){
int[] days_of_month = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//一月份加的天数为0
if ((year % 4 == 0) && (year / 100 != 0) || (year % 400 == 0)) {
days_of_month[2] = 29;
}
//参数有效性检查
if (month < 1 || month>12 || day<0 || day>days_of_month[month]) {
return -1;
}
int sum = 0;
for (int i = 0; i < month; i++){
sum += days_of_month[i];
}
sum += day; //加上当月的天数
return sum;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入年:");
int year = scanner.nextInt();
System.out.println("输入月:");
int month = scanner.nextInt();
System.out.println("输入日:");
int day = scanner.nextInt();
int sum = getDay(year, month, day);
System.out.println("这是这年的第: " + sum + " 天");
}
https://www.cnblogs.com/ys99/p/9196453.html
不值一题
1、 对学生成绩大于60分的,输出“合格”。低于60分的,输出“不合格”。
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入学生成绩");
double score = scanner.nextDouble();
if (score >= 60){
System.out.println("合格");
}else{
System.out.println("不合格");
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入学生成绩");
double score = scanner.nextDouble();
int g = (int)score / 10;
switch (g){
case 10:
case 9:
case 8:
case 7:
case 6:
System.out.println("合格");
break;
default:
System.out.println("不合格");
}
}
2、使用switch语句改写下列if语句:
int a = 3;
int x = 100;
if(a==1)
x+=5;
else if(a==2)
x+=10;
else if(a==3)
x+=16;
else
x+=34;
int a = 3;
int x = 100;
switch (a){
case 1:
x+=5;
break;
case 2:
x+=10;
break;
case 3:
x+=16;
break;
default:
x+=34;
}