如何继续请求用户选择有效选项?

时间:2013-09-25 22:15:17

标签: java

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

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

}

5 个答案:

答案 0 :(得分: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;
}

希望这有帮助。

答案 1 :(得分: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

答案 2 :(得分: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

答案 3 :(得分:0)

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

程序正在运行,因为你正在调用wood()if(选择&gt; 3 || choice&lt; 1)

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

如果您还想清除选择值(而不是-1),则可以将其指定为null

答案 4 :(得分:0)

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

当你再次拨打wood时,选择是休息(在此值为未知值,直到为用户分配值)。

现在问题发生在wood方法存在...每次返回调用者时,它会调用size(choice),其中choice-1(因为这就是你在再次打电话之前设置的内容。)

  1. 您应该使用while-loop而不是递归调用
  2. 除了有效的选择之外,你永远不应该用size(choice)来呼叫
  3. 请查看The while and do-while statement了解详情