2

因此,用户必须在 1 和 3 之间选择一个数字。否则,他们会被告知再试一次。如果用户尝试小于 1 或大于 3 的数字,他们选择的任何数字都会存储在“选择”变量中,并导致程序在应该停止时继续运行。我认为会有一个简单的解决方案,但显然它超出了我作为初学者的范围。对我来说显而易见的事情是在用户输入不成功后以某种方式清除或清空分配给“选择”的值。那可能吗?

import java.util.Scanner;

public class Furniture2Test  {

    public static void main(String[] args) {

        wood();

    } // end main

    public static void wood() {

        int choice;

        int pine = 1;
        int oak = 2;
        int mahogany = 3;

        int pineCost = 100;
        int oakCost = 225;
        int mahoganyCost = 310;

        Scanner keyboard = new Scanner(System.in);

        System.out.println("What type of table would you like?");
        System.out.println("1. pine");
        System.out.println("2. oak");
        System.out.println("3. mahogany");

        choice = keyboard.nextInt();

        if (choice == 1) {
            choice = pineCost;
        } else if (choice == 2) {
            choice = oakCost;
        } else if (choice == 3) {
            choice = mahoganyCost;
        } else if (choice > 3 || choice < 1) {
            System.out.println("Try again.");
            choice = -1;
            wood();
        }

        System.out.println("That will be $" + choice + ".");

        size(choice);

    } // end wood

    public static void size(int choice) {

        int sizeChoice;
        int large = 35;

        Scanner keyboard = new Scanner(System.in);

        System.out.println("What size will that be?");
        System.out.println("1. large");
        System.out.println("2. small");

        sizeChoice = keyboard.nextInt();

        if (sizeChoice == 1)
            System.out.println("That will be $" + (choice + large) + ".");
        else if (sizeChoice == 2)
            System.out.println("That will be $" + choice);
        else
            System.out.println("Please, enter either a 1 or a 2.");

    } // end size

}
4

5 回答 5

1

使用 do...while 循环可以轻松完成您的要求。示例代码如下:

do{
    System.out.println("Choose option between 1 and 3");
    choice = keyboard.nextInt();
}while(!(choice > 3 || choice < 1));

if (choice == 1) {
    choice = pineCost;
} else if (choice == 2) {
    choice = oakCost;
} else if (choice == 3) {
    choice = mahoganyCost;
}

希望这可以帮助。

于 2017-08-16T08:18:09.067 回答
0

他们选择的任何数字都存储在“选择”变量中,并导致程序在应该停止时继续运行//

程序继续运行,因为您正在调用 wood() if(choice > 3 ||choice < 1)

如果你想让它停止删除 wood() 调用

如果您还想清除选择的值(而不是 -1),您可以将其分配给 null

于 2013-09-25T22:26:14.230 回答
0

choicewood是方法 wood 的局部变量,当用户做出错误选择时,您正在递归调用。这是一个有趣的设计选择,在这种情况下可能不是最好的。

当您wood再次调用时,选择是休息(在此未知值,直到它被用户分配值)。

现在,当wood方法存在时会出现问题......每次它返回给调用者时,它都会调用size(choice), where choiceis -1(因为这是你在再次调用 wood 之前设置的)。

  1. 您应该使用 awhile-loop而不是递归调用
  2. 你不应该size(choice)用任何其他的东西来调用,而不是一个有效的选择

查看The while 和 do-while 语句以获取更多详细信息

于 2013-09-25T22:26:28.593 回答
0
//put the menu logic
while(choice > 3 || choice < 1) {
    //put your try again logic.
}
//can only exit the while loop if the number is 1, 2, or 3, so put your output statement down here after the while loop
于 2013-09-25T22:20:31.183 回答
0
import java.util.Scanner;

public class Furniture2Test

{

   public static void main(String[] args)
   {

      wood();

   } // end main


   public static void wood()
   {

      int choice;

      int pine = 1;
      int oak = 2;
      int mahogany = 3;

      int pineCost = 100;
      int oakCost = 225;
      int mahoganyCost = 310;

      Scanner keyboard = new Scanner(System.in);

      System.out.println("What type of table would you like?");
      System.out.println("1. pine");
      System.out.println("2. oak");
      System.out.println("3. mahogany");

      choice = read_range(keyboard, 1, 3);

      if(choice == 1)
      {
         choice = pineCost;
      }
      else
         if(choice == 2)
         {
            choice = oakCost;
         }
         else
            if(choice == 3)
            {
               choice = mahoganyCost;
            }
            else
               if(choice > 3 || choice < 1)
               {
                  System.out.println("Try again.");
                  choice = -1;
                  wood();
               }

      System.out.println("That will be $" + choice + ".");

      size(choice);


   }  // end wood

   public static void size(int choice)
   {

      int sizeChoice;
      int large = 35; 

      Scanner keyboard = new Scanner(System.in);

      System.out.println("What size will that be?");
      System.out.println("1. large");
      System.out.println("2. small");

      sizeChoice = read_range(keyboard, 1, 2);

      if(sizeChoice == 1)
         System.out.println("That will be $" + (choice + large) + ".");
      else
         if(sizeChoice == 2)
            System.out.println("That will be $" + choice);
         else
            System.out.println("Please, enter either a 1 or a 2.");

   } // end size

   private static int read_range (Scanner scanner, int low, int high) {
     int value;
     value = scanner.nextInt();
     while (value < low || value > high) {
       System.out.print("Please enter a value between " + low + " and " + high + ": ");
       value = scanner.nextInt();
     }
     return value;
   }



} // end class
于 2013-09-25T22:23:05.813 回答