package lab04_AnnaStineburg;
//import java.util.Scanner;
import javax.swing.JOptionPane;
public class RomanNumerals {
public static void main(String[] args) {
// TODO Auto-generated method stub
String task;
String title;
String roman;
int yesNo;
int decimal;
String str;
task= "Enter a Roman Numneral between \"I\" and \"XX\"";
title= "Conversion of Roman Numerals";
do {
roman= JOptionPane.showInputDialog(null, task, title,
JOptionPane.QUESTION_MESSAGE);
if(roman==null) {
task= "You pressed Cancel Button";
JOptionPane.showMessageDialog(null, task, title,
JOptionPane.INFORMATION_MESSAGE);
task= "End of Program!";
JOptionPane.showMessageDialog(null, task, title,
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
roman= roman.toUpperCase();
decimal =0;
if (roman.charAt(0)== 'I') {
if (roman.equals("I")) {
decimal= 1;
}
else if(roman.equals("II")) {
decimal= 2;
}
else if(roman.equals("III")) {
decimal=3;
}
else if(roman.equals("IV")) {
decimal= 4;
}
else if(roman.equals("IX")) {
decimal= 10;
}
else {
JOptionPane.showMessageDialog(null, "Input " + roman +
" is not an\nadmissible Roman numeral ", title,
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
if(roman.charAt(0)== 'V') {
if (roman.equals("V")) {
decimal= 5;
}
else if(roman.equals("VI")) {
decimal= 6;
}
else if(roman.equals("VII")) {
decimal= 7;
}
else if(roman.equals("VIII")) {
decimal=8;
}
else {
JOptionPane.showMessageDialog(null, "Input " + roman +
" is not an\nadmissible Roman numeral ", title,
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
if(roman.charAt(0)=='X') {
if(roman.equals("X")) {
decimal= 10;
}
else if(roman.equals("XI")) {
decimal=11;
}
else if(roman.equals("XII")) {
decimal=12;
}
else if(roman.equals("XIII")) {
decimal=13;
}
else if(roman.equals("XIV")) {
decimal=14;
}
else if(roman.equals("XV")) {
decimal=15;
}
else {
JOptionPane.showMessageDialog(null, "Input " + roman +
" is not an\nadmissible Roman numeral ", title,
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
else {
JOptionPane.showMessageDialog(null, "Input " + roman +
" is not an\nadmissible Roman numeral ", title,
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
str= String.format("The decimal value for the Roman numeral \""+ roman + "\" is: ....."
+ "%d" + "....." , decimal);
JOptionPane.showMessageDialog(null, str, title,
JOptionPane.INFORMATION_MESSAGE);
yesNo= JOptionPane.showConfirmDialog(null, "Any more Roman Numerals?\n",
title, JOptionPane.YES_NO_OPTION);
} while (yesNo==0);
task= "End of program!";
JOptionPane.showMessageDialog(null, task, title,
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}代码应该读取罗马数字,并将它们显示为相应的数值。它适用于所有以"X“开头的罗马数字,但每次我输入一个以"I”或"V“开头的数字时,程序就会转到最后一个"else”语句。我在正确格式化嵌套的if-else语句时遇到了困难。
发布于 2020-10-08 04:19:16
举个小一点的例子:
String roman = "IV";
int decimal = 0;
if (roman.charAt(0) == 'I') {
if (roman.equals("IV") {
decimal = 4;
} else {
decimal = 1;
}
}
if (roman.charAt(0) == 'V') {
if (roman.equals("VI") {
decimal = 6;
} else {
decimal = 5;
}
} else {
System.out.println("Error incorrect roman numeral entry");
System.exit(0);
}
System.out.println("Roman numeral: " + roman + " = " + decimal);您可能希望这段代码输出Roman numeral IV = 4,但实际上它输出的是Error incorrect roman numeral entry。
原因如下:
// 1 - Start Here
String roman = "IV";
int decimal = 0;
// 2 - roman starts with 'I' so enter 'if'
if (roman.charAt(0) == 'I') {
// 3 - roman equals "IV" so enter 'if'
if (roman.equals("IV")) {
// 4 - set decimal to 4
decimal = 4;
} else {
decimal = 1;
}
}
// 5 - roman does not start with 'V' don't enter 'if'
if (roman.charAt(0) == 'V') {
if (roman.equals("VI") {
decimal = 6;
} else {
decimal = 5;
}
// 6 - enter the catch-all 'else'
} else {
// 7 - output error message
System.out.println("Error incorrect roman numeral entry");
// 8 - exit program
System.exit(0);
}
System.out.println("Roman numeral: " + roman + " = " + decimal);解决此问题的一种方法是将catch-all else语句更改为:
// earlier code ...
if (roman.charAt(0) == 'V') {
if (roman.equals("VI") {
decimal = 6;
} else {
decimal = 5;
}
// if decimal still equals 0 then no proper roman numeral was read
} else if (decimal == 0) {
System.out.println("Error incorrect roman numeral entry");
System.exit(0);
}发布于 2020-10-08 04:23:37
代码运行良好..
下面的代码运行良好。我能够打印出:
inside IV if
The decimal is: 4//import java.util.Scanner;
//import javax.swing.JOptionPane;
public class RomanNumerals {
public static void main(String[] args) {
// TODO Auto-generated method stub
String roman = "IV";
int decimal = 0;
roman = roman.toUpperCase();
if (roman.charAt(0) == 'I') {
if (roman.equals("I")) {
System.out.println("inside I if");
decimal = 1;
} else if (roman.equals("II")) {
decimal = 2;
} else if (roman.equals("III")) {
decimal = 3;
} else if (roman.equals("IV")) {
System.out.println("inside IV if");
decimal = 4;
} else if (roman.equals("IX")) {
decimal = 10;
} else {
System.out.println("Input is not an admissible Roman numeral 1 ");
System.exit(0);
}
}
else if (roman.charAt(0) == 'V') {
if (roman.equals("V")) {
decimal = 5;
}
else if (roman.equals("VI")) {
decimal = 6;
} else if (roman.equals("VII")) {
decimal = 7;
} else if (roman.equals("VIII")) {
decimal = 8;
}
else {
System.out.println("Input is not an admissible Roman numeral 2");
System.exit(0);
}
}
else if (roman.charAt(0) == 'X') {
if (roman.equals("X")) {
decimal = 10;
}
else if (roman.equals("XI")) {
decimal = 11;
} else if (roman.equals("XII")) {
decimal = 12;
} else if (roman.equals("XIII")) {
decimal = 13;
} else if (roman.equals("XIV")) {
decimal = 14;
} else if (roman.equals("XV")) {
decimal = 15;
}
}
else
System.out.println("Input is not an admissible Roman numeral 3");
System.out.println("The decimal is: "+ decimal);
}
}https://stackoverflow.com/questions/64251399
复制相似问题