这是一个允许用户输入年份并以罗马数字返回相应年份的程序。我可以编译它而没有任何错误,但是当我运行它时,它会出错。
该程序的代码是
import java.io.*;
public class RomanCal {
public static void main(String args[])throws IOException{
int year;
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a year to convert : ");//asking for input
year = input.read();
start(year);
}
public static void start(int year){
String full = Integer.toString(year);//full year is converted to a String
String check = full.substring(0,1);
switch (check){//checking the first number
case "1" : System.out.println("M");
break;
case "2" : System.out.println("MM");
break;
default : System.out.println("Invalid Year");
}
check = full.substring(1,2);
switch (check){//checking the second number
case "0" :
check = full.substring(2,3);
switch (check){//check the third number if second is zero
case "0" :
check = full.substring(3,4);
switch (check){//check the fourth number if third is zero
case "1" : System.out.print("I");
break;
case "2" : System.out.print("II");
break;
case "3" : System.out.print("III");
break;
case "4" : System.out.print("IV");
break;
case "5" : System.out.print("V");
break;
case "6" : System.out.print("VI");
break;
case "7" : System.out.print("VII");
break;
case "8" : System.out.print("VIII");
break;
case "9" : System.out.print("IX");
break;
}
break;
case "1" : System.out.print("X");
break;
case "2" : System.out.print("XX");
break;
case "3" : System.out.print("XXX");
break;
case "4" : System.out.print("XL");
break;
case "5" : System.out.print("L");
break;
case "6" : System.out.print("LX");
break;
case "7" : System.out.print("LXX");
break;
case "8" : System.out.print("LXXX");
break;
case "9" : System.out.print("XC");
break;
}
break;
case "1" : System.out.print("C");
break;
case "2" : System.out.print("CC");
break;
case "3" : System.out.print("CCC");
break;
case "4" : System.out.print("CD");
break;
case "5" : System.out.print("D");
break;
case "6" : System.out.print("DC");
break;
case "7" : System.out.print("DCC");
break;
case "8" : System.out.print("DCCC");
break;
case "9" : System.out.print("CM");
break;
}
check = full.substring(3,4);
switch (check){//check the fourth number
case "1" : System.out.print("I");
break;
case "2" : System.out.print("II");
break;
case "3" : System.out.print("III");
break;
case "4" : System.out.print("IV");
break;
case "5" : System.out.print("V");
break;
case "6" : System.out.print("VI");
break;
case "7" : System.out.print("VII");
break;
case "8" : System.out.print("VIII");
break;
case "9" : System.out.print("IX");
break;
}
}
}
两个主要问题:
break
在大小写/切换中使用语句考虑以下代码:
public static void main(final String args[]) throws java.io.IOException {
final String[] m = {"", "M", "MM"};
final String[] c = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
final String[] x = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
final String[] i = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
try (final java.util.Scanner scanner = new java.util.Scanner(System.in)) {
System.out.print("Enter a year to convert : ");
final int year = scanner.nextInt();
System.out.println(m[year / 1000] + c[(year / 100) % 10] + x[(year / 10) % 10] + i[year % 10]);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句