关于java的难题

来源:百度知道 编辑:UC知道 时间:2024/06/05 16:45:15
import java.io.IOException;

public class sk {

public static void main(String[] args) throws IOException{

char a;
int b=1;
System.out.print("Enter a number(1-12,enter 0 to exit.): ");
a=(char) System.in.read();
while(b!=0){
switch(a){
case '1':
case '3':
case '5':
case '7':
case '8':
case '10':
case '12':
System.out.println("31");break;
case 2:
System.out.println("28 or 29");break;
case '4':
case '6':
case '9':
case '11':
System.out.println("30");break;<

char是字符类型,你想想,'12'、'11'、'10'这种是“字符”吗……

/*不明白你为什么要用字符,还有,会出现死循环。简单修改了一下。不知道,是否偏离了原意。请LZ测试一下。 (输入1-12的数,一直会提示你输入,超过12重新输入,想退出输入0即可);*/

import java.io.*;
import java.util.*;

public class Test {

public static void main(String[] args) throws IOException{

while(true) {
System.out.print("Enter a number(1-12,enter 0 to exit.): ");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();

switch(a){
case 0:System.exit(0);
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println("31");break;
case 2:
System.out.println("28 or 29");break;
case 4:
case 6:
case 9:
case 11:
System.out.println("30");break;
default: continue;

}

}

}

}

至少你应该把a=(char) System.in.read()放在循环里面吧.要不不