有 Java 编程相关的问题?

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

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

基本上,我希望能够输入一个不是选项的数字,然后被赋予再次选择的选项,并让语句重复

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) 个答案

  1. # 1 楼答案

    你必须学习编程语言的基本知识做。。而切换语句

    下面是示例程序

    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. # 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);
    
  3. # 3 楼答案

    这可以通过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);
    

    所以,基本上,你礼貌地问一次。之后,开始大喊这是错误的,直到用户给出正确的输入