如何在Java中重复使用if条件?

时间:2014-05-23 19:21:01

标签: java if-statement

基本上我希望能够输入一个不是一个选项的数字,然后再次选择该选项并重复声明。

Scanner keyboard = new Scanner(System.in);
    double weight;
    int Choice;

    System.out.println("What is your weight in pounds?");
    weight = keyboard.nextDouble();

    System.out.println("Which planet would you like to see your weight on?\n 1. Venus   2. Mars 3. Jupiter\n 4. Saturn  5. Uranus   6. Neptune");
    Choice =keyboard.nextInt();

    if (Choice == 1){
        System.out.println("Your weight on Venus would be " + (weight * 0.78));
    }
    else if (Choice == 2){
        System.out.println("Your weight on Mars would be " + (weight * .39));
    }
    else if (Choice == 3){
        System.out.println("Your weight on Jupiter would be " + (weight * 2.65));
    }
    else if (Choice == 4){
        System.out.println("Your weight on Saturn would be " + (weight * 1.17));
    }

    else if (Choice == 5){
        System.out.println("Your weight on Uranus would be" +(weight * 1.05));
    }
    else if (Choice == 6) {
        System.out.println("Your weight on Neptune would be " + (weight * 1.23));
    }
    else
        System.out.println("This was not a choice, try again!");
        Choice = keyboard.nextInt();
}

3 个答案:

答案 0 :(得分:2)

这是一种更简单的方法,使用do-while循环和switch

也是固定的选择

Scanner keyboard = new Scanner(System.in);
double weight;
int choice;

System.out.println("What is your weight in pounds?");
weight = keyboard.nextDouble();

do {
    System.out.println("Which planet would you like to see your weight on?\n 1. Venus   2. Mars 3. Jupiter\n 4. Saturn  5. Uranus   6. Neptune\n 7. Exit");
    choice = keyboard.nextInt();
    switch(choice) {
        case 1:
            System.out.println("Your weight on Venus would be " + (weight * 0.78));
            break;
        case 2:
            System.out.println("Your weight on Mars would be " + (weight * .39));
            break;
        case 3:
            System.out.println("Your weight on Jupiter would be " + (weight * 2.65));
            break;
        case 4:
            System.out.println("Your weight on Saturn would be " + (weight * 1.17));
            break;
        case 5:
            System.out.println("Your weight on Uranus would be" +(weight * 1.05));
            break;
        case 6:
            System.out.println("Your weight on Neptune would be " + (weight * 1.23));
            break;
        case 7:
            System.out.println("Bye");
            break;
        default:
            System.out.println("This was not a choice, try again!");
            break;
    }
} while (choice != 7);

答案 1 :(得分:0)

你必须学习编程语言的基础...... do..while 切换语句。

以下是示例程序。

public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);
        double weight;
        int choice;

        System.out.println("What is your weight in pounds?");
        weight = keyboard.nextDouble();

        do {

            System.out
                    .println("Which planet would you like to see your weight on?\n 1. Venus   2. Mars 3. Jupiter\n 4. Saturn  5. Uranus   6. Neptune \n7.EXIT");
            System.out.println();
            choice = keyboard.nextInt();

            switch (choice) {
            case 1:
                System.out.println("Your weight on Venus would be "
                        + (weight * 0.78));
                break;
            case 2:
                System.out.println("Your weight on Mars would be "
                        + (weight * .39));
                break;
            case 3:
                System.out.println("Your weight on Jupiter would be "
                        + (weight * 2.65));
                break;
            case 4:
                System.out.println("Your weight on Saturn would be "
                        + (weight * 1.17));
                break;
            case 5:
                System.out.println("Your weight on Uranus would be"
                        + (weight * 1.05));
                break;
            case 6:
                System.out.println("Your weight on Neptune would be "
                        + (weight * 1.23));
            case 7:
                System.out.println("Leaving...");
                break;
            default:
                System.out.println("This was not a choice, try again!");
                break;
            }
        } while (choice != 7);

    }

使用Java命名约定为变量命名。从小写字母开始。

答案 2 :(得分:-1)

这可以通过while循环完成。还要考虑使用数组。

String[] planets = {"Mars", "Jupiter", .... };
double[] factors = {0.45, 1.5, ....};

double mass = keyboard.nextDouble();
int choice = keyboard.nextInt();
while (choice < 0 || choice >= planets.length)
{
    System.out.println("Not a valid option!");
    choice = keyboard.nextInt();
}

double weight = mass * factors[choice];
System.out.println("Your weight on " + planets[choice] + " would be " + weight);

所以,基本上,你礼貌地问过它。然后,在用户给出正确的输入之前,开始大声说它是错误的。