我编写了一个程序,将数字/整数转换为罗马数字,但我需要是或否选项。请我帮忙
import java.util.*;
public class num2roman
{
public static void main(String...args)
{
int ones, tens, hundred;
int num;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number [1-1000]: ");
num = sc.nextInt();
if (num>0 && num<10)
{
ones = num;
switch(ones)
{
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;
}
}
else if (num>9 && num<100)
{
tens =(num/10);
ones =(num%10);
switch(tens)
{
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;
}
switch(ones)
{
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;
}
}
else if (num>99 && num<1000)
{
hundred =(num/100);
tens =((num/10)%10);
ones =(num%10);
switch(hundred)
{
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;
}
switch(tens)
{
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;
}
switch(ones)
{
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;
}
}
else if (num == 1000)
{
System.out.print("M");
}
else
{
System.out.print("Out of range 1-1000 only!");
}
}
}
答案 0 :(得分:0)
System.out.print("Enter yes or no");
while (!sc.next().equals("yes") && !sc.next().equals("no")){
System.out.print("Enter yes or no...");
}
这基本上就是你想要的=)
答案 1 :(得分:0)
有一个good question to start in stackoverflow。 你应该检查这样的程序参数:
if(args[0].equalsIgnoreCase("YES")) {
// Do whatever you want.
}
答案 2 :(得分:0)
在num = sc.nextInt();
之后添加以下行:
if (JOptionPane.showConfirmDialog(null, "Do you want to convert?", "Convert option", JOptionPane.YES_NO_OPTION) == 1) {
System.out.print(num);
System.exit(0);
}
如果您选择&#34;否&#34;,它只显示数字。 如果你选择&#34;是&#34;它转换它。