如何压缩这些大的if语句

时间:2019-10-22 12:30:31

标签: java if-statement

我已经在这段代码上工作了一段时间,并最终完成了它,但是有什么方法可以压缩大量的if语句?

我已经尝试了所有编码知识,但似乎没有任何效果。

import java.util.Scanner;

public class AidanMRN{

    public static void main(String[] args){

        //Creating Variable "Number"
        int number;

        Scanner keyboard = new Scanner(System.in);

        //Storing Number
        System.out.println("Pick a number, 1 - 10");
        number = keyboard.nextInt();

        //If statements and Outputs
        if (number > 10)
            //Error message
            System.out.print("Error, " + number + " is higher than 10.\nPlease try again");
        if (number == 1)
            System.out.println("Roman Numeral: I");

        if (number == 2)
            System.out.println("Roman Numeral: II");

        if (number == 3)
            System.out.println("Roman Numeral: III");

        if (number == 4)
            System.out.println("Roman Numeral: IV");

        if (number == 5)
            System.out.println("Roman Numeral: V");

        if (number == 6)
            System.out.println("Roman Numeral: VI");

        if (number == 7)
            System.out.println("Roman Numeral: VII");

        if (number == 8)
            System.out.println("Roman Numeral: VIII");

        if (number == 9)
            System.out.println("Roman Numeral: IX");

        if (number == 10)
            System.out.println("Roman Numeral: X");
    }
}

6 个答案:

答案 0 :(得分:5)

使用此代码,您可以使用较短的if语句。

public static void main(String[] args) {
            String[] romanNumbers= {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"};

            int number;
            Scanner keyboard = new Scanner(System.in);
            //Storing Number
            System.out.println("Pick a number, 1 - 10");
            number = keyboard.nextInt();
            //If statements and Outputs
            if (number > 10 || number < 1)
                //Error message
                System.out.print("Error, " + number + " is higher than 10.\nPlease try again");
            else
                System.out.println("Roman Numeral: " + romanNumbers[number -1]);
        }
    }

答案 1 :(得分:3)

使用Map

Map<Integer, String> numbers = new HashMap<>() {{
    put(1, "I");
    put(2, "II");
    put(3, "III");
}};

System.out.println("Roman Numeral: " + numbers.get(number));

答案 2 :(得分:0)

使用

可以这样做

enter a number : 5
userNumber=5
    String [] romanNumbers = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"};
    for(int i=0;i<romanNumbers.length;i++){
if(i==userNumber &&userNumber<=10){
userNumber=userNumber-1;
Console.WriteLine("the roman Number is "+romanNumbers[userNumber]);
}
}

答案 3 :(得分:0)

您可以使用switch语句代替那些if

public static void main(String[] args) {
    // Creating Variable "Number"
    int number;
    Scanner keyboard = new Scanner(System.in);
    // Storing Number
    System.out.println("Pick a number, 1 - 10");
    number = keyboard.nextInt();
    // If statements and Outputs
    switch (number) {
    // error message for number < 1 AND number > 10
    default:
        System.out.print("Error, " + number 
                + " is less than 1 or greater than 10.\nPlease try again");
        break;
    case 1:
        System.out.println("Roman Numeral: I");
        break;
    case 2:
        System.out.println("Roman Numeral: II");
        break;
    case 3:
        System.out.println("Roman Numeral: III");
        break;
    case 4:
        System.out.println("Roman Numeral: IV");
        break;
    case 5:
        System.out.println("Roman Numeral: V");
        break;
    case 6:
        System.out.println("Roman Numeral: VI");
        break;
    case 7:
        System.out.println("Roman Numeral: VII");
        break;
    case 8:
        System.out.println("Roman Numeral: VIII");
        break;
    case 9:
        System.out.println("Roman Numeral: IX");
        break;
    case 10:
        System.out.println("Roman Numeral: X");
        break;
    }
}

答案 4 :(得分:0)

您可以在Main方法中使用此代码。就我个人而言,我将为错误消息使用一个循环,但是这种使用数组的方法有效。

    //Creating Variable "Number"
    int number;
    Scanner keyboard = new Scanner(System.in);

    //Storing Number
    System.out.println("Pick a number, 1 - 10");
    number = keyboard.nextInt();

    String [] romanNumeral = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"};

    if(number > 10)
    {   
        System.out.print("Error, " + number + " is higher than 10.\nPlease try again");
    }
    else
    {   
        System.out.println("Roman Numeral: " + romanNumeral[number-1]);
    }

答案 5 :(得分:0)

答案可能是这样的:

public class RomanNumerals {

    private static final Map<Integer, String> NUMERAL_MAP = new HashMap<>();

    static {
        NUMERAL_MAP.put(1, "I");
        NUMERAL_MAP.put(2, "II");
        NUMERAL_MAP.put(3, "III");
        NUMERAL_MAP.put(4, "IV");
        NUMERAL_MAP.put(5, "V");
        NUMERAL_MAP.put(6, "VI");
        NUMERAL_MAP.put(7, "VII");
        NUMERAL_MAP.put(8, "VIII");
        NUMERAL_MAP.put(9, "IX");
        NUMERAL_MAP.put(10, "X");
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Please input a number [1-10]: ");
        int input = scanner.nextInt();

        Optional<String> found = Optional.ofNullable(NUMERAL_MAP.get(input));
        if (found.isPresent()) {
            System.out.println("Found numeral: " + found.get());
        } else {
            System.out.println("No numeral found for input: " + input);
        }
    }

}

基本上,您可以将整数的键值和数字的字符串值初始化为地图键。然后,通过迭代地图的keySet尝试找到相应的值。