1.电话上的国际标准字母数字映射如下所示,
编写一个程序。提示用户输入一个小写或大写字母,然后显示对应的数字,对于非字母的输入,提示非法输入。
import java.util.Scanner;
public class Letterdigit{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter a letter:");
String a = input.next();
char letter = a.charAt(0);
if ((letter>='a'&&letter<='z')||(letter>='A'&&letter<='Z'))
{
switch (letter)
{ case 'a':case 'b':case 'c':case 'A':case 'B':case 'C':
System.out.println("The corresponding number is 2");
break;
case 'd':case 'e':case 'f':case 'D':case 'E':case 'F':
System.out.println("The corresponding number is 3");
break;
case 'g':case 'h':case 'i':case 'G':case 'H':case 'I':
System.out.println("The corresponding number is 4");
break;
case 'j':case 'k':case 'l':case 'J':case 'K':case 'L':
System.out.println("The corresponding number is 5");
break;
case 'm':case 'n':case 'o':case 'M':case 'N':case 'O':
System.out.println("The corresponding number is 6");
break;
case 'p':case 'q':case 'r':case 's':case 'P':case 'Q':case 'R':case 'S':
System.out.println("The corresponding number is 7");
break;
case 't':case 'u':case 'v':case 'T':case 'U':case 'V':
System.out.println("The corresponding number is 8");
break;
case 'w':case 'x':case 'y':case 'z':case 'W':case 'X':case 'Y':case 'Z':
System.out.println("The corresponding number is 9");
break;
}
}
else
System.out.println("Wrong input!");
}
}
笨方法:就是一个一个列举
2.编写一个程序,提示用户输入一个年份和一个月份名称前的前三个字母(第一个字母使用大写形式),显示该月中的天数,如果输入的月份是非法的,显示出一条出错信息。
import java.util.Scanner;
public class Yearmonth{
public static void main(String[] args){
Scanner input =new Scanner(System.in);
System.out.println("Enter a year:");
int year =input.nextInt();
System.out.println("Enter a month:");
String month= input.next();
switch(month)
{
case "Jan":case "Mar":case "May":case "Jul":case "Aug":case "Oct":case "Dec":
System.out.println(month +" "+ year +" has 31 days");
break;
case "Feb":
if((year%4==0 && year%100!=0)||year%400==0)
System.out.println("Feb "+ year +" has 29 days");
else
System.out.println("Feb "+ year +" has 28 days");
break;
case "Apr":case "Jun":case "Sep":case "Nov":
System.out.println( month +" "+ year +" has 30 days");
break;
default:
System.out.println(month +" is not a correct month name");
}
}
}
在编写过程中,对于switch中case后的内容要求格式,会单独写一篇文章说明。



