有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java正确的嵌套ifelse语句格式

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语句


共 (1) 个答案

  1. # 1 楼答案

    代码运行良好

    下面的代码运行良好。我可以打印:

    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);
        }
    
    }