嵌套if-else语句的正确格式

问题描述

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,JOptionPane.INFORMATION_MESSAGE);
                task= "End of Program!";
                JOptionPane.showMessageDialog(null,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 ",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 ",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,JOptionPane.INFORMATION_MESSAGE);
                    System.exit(0); 
                }
            }
            else {
                JOptionPane.showMessageDialog(null,"Input " + roman +
                    " is not an\nadmissible Roman numeral ",JOptionPane.INFORMATION_MESSAGE);
                System.exit(0); 
            }
            
            str= String.format("The decimal value for the Roman numeral \""+ roman + "\" is: ....."
                    + "%d" + ".....",decimal);
            JOptionPane.showMessageDialog(null,str,JOptionPane.INFORMATION_MESSAGE);
                
            
            
            yesNo= JOptionPane.showConfirmDialog(null,"Any more Roman Numerals?\n",JOptionPane.YES_NO_OPTION);
                


        } while (yesNo==0);
    
        task= "End of program!";
        JOptionPane.showMessageDialog(null,JOptionPane.INFORMATION_MESSAGE);   

        System.exit(0);
    }

}

该代码应读取罗马数字并将其显示为相应的数值。它适用于所有以“ X”开头的罗马数字,但是每次我输入以“ I”或“ V”开头的数字时,程序都会进入最后的“ else”语句。我在正确格式化嵌套的if-else语句时遇到困难。

解决方法

以一个较小的示例为例:

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

一种解决方法是将“包罗万象”其他语句更改为:

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

代码可以正常工作。

以下代码可以正常工作。我能够打印:

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

}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...