如何在Java中不断提示输入正确的用户? [英] How to keep prompting for the correct user input in Java?

查看:52
本文介绍了如何在Java中不断提示输入正确的用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个非常简单的问题,我似乎无法解决.

Very simple problem that I can't seem to wrap my head around.

我正在编写一个自行车程序,该程序根据自行车上的节奏和齿轮来计算MPH.

I'm writing a bicycle program that calculates MPH based on the cadence and gears set on the bike.

可以设置的档位在1-3之间,步频在1-100之间.

The gears that can be set are between 1-3 and the cadence is between 1-100.

TL; DR 如果用户输入的数字超出该范围,我将无法确定程序如何再次提示他们,直到他们输入正确的范围.我也很难弄清楚一旦完成计算后如何让整个BicycleTest程序循环.

我查询了其他问题,发现很多人都在谈论while循环,尽管我尝试过,但是我一定做错了,因为它没有循环到开始.我将其保留在我原来的if-else循环中,但是对于在这种情况下如何正确使用while循环,我将不胜感激.

I looked up other questions and noticed a bunch of people talking about while loops, which I tried, but I must have done it incorrectly because it did not loop to the beginning. I'm leaving it at my original if-else loop but I would appreciate help on how to properly use a while loop for this situation.

以下代码:

public class Bicycle
{
 // instance variables declared private
    private int gear;
    private int cadence;
    private int speed;


   //accessor (get) method for gear
   public int getGear()
   {
       return gear;
   }

   //set method for gear
    public void setGear(int gear)
       {
          if (gear >=1 && gear <= 3)
          {
              this.gear = gear;
          }

          else
          {
              System.out.println ("Sorry, please enter a number between 1-3");
          }
      }

   //accessor (get) method for cadence
   public int getCadence()
   {
       return cadence;
   }

   //set method for cadence
   public void setCadence(int cadence)
   {
       if (cadence >=1 && cadence <=100)
       {
           this.cadence = cadence;
       }
       else
         {
            System.out.println ("Sorry, please enter a number between 1-100");
         }


   }

   //accessor (get) method for speed
   public int getSpeed()
   {
       if (gear == 1)
       {
           return cadence / 12;
       }
       else if (gear == 2)
       {
           return cadence / 6;
       }
       else if (gear == 3)
       {
           return cadence / 4;
       }

       else
       {
            return 0;
       }


  }

} // end of main

然后是BicycleTest代码:

and then here is the BicycleTest code:

 import java.util.Scanner;
public class BicycleTest
{

public static void main(String[] args)
{

Bicycle bike = new Bicycle();

Scanner input = new Scanner(System.in);

System.out.println("Enter the gear :");
bike.setGear(input.nextInt());

System.out.println("Enter the cadence :");
bike.setCadence(input.nextInt());

int speed = bike.getSpeed();
System.out.println ("Your speed in MPH is: ");
System.out.println(speed);

}

}   

推荐答案

以下是通过 while 循环获取所需内容的方法.秘密之处在于,循环依赖于布尔标志来知道何时退出.仅当用户输入在可接受范围内时,才调整该标志.

Here's how to get what you want with a while loop. The secret sauce is the boolean flag that the loop depends on to know when to exit. That flag is only adjusted when the user input falls within the acceptable range.

请注意,只是为了清楚起见,用户输入已分配给变量.

Take note that the user input is assigned to a variable, just for the sake of clarity.

boolean badAnswer = true;
int gear = 0;

while(badAnswer)
{
    System.out.println("Enter the gear :");
    gear = input.nextInt();

    if(gear > 0 && gear <= 3)
    {
        bike.setGear(gear);
        badAnswer = false;
    }
    else
    {
        System.out.println("Please enter a value for gear between 1 and 3.");

    }
}

如果要在 Bicycle 类中而不是在驱动程序中进行验证,则可以将范围测试放在 setGear()中,并返回一个布尔值在驱动程序类中设置while循环的退出条件.

If you want validation in your Bicycle class rather than in the driver program, you could put the range test inside setGear() and return a boolean value that would serve to set the exit condition of the while loop in your driver class.

这篇关于如何在Java中不断提示输入正确的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆