How to keep prompting for the correct user input in Java?

一个人想着一个人 提交于 2021-01-29 04:34:34

问题


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

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

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

TL;DR If a user enters a number outside of that range, I'm having trouble figuring out how to have the program prompt them again until they enter the correct range. I'm also having trouble figuring out how to get the entire BicycleTest program to loop over once the calculations are finished.

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.

Code below:

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

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);

}

}   

回答1:


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.");

    }
}

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.




回答2:


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 boolean setGear(int gear)
       {
          if (gear >=1 && gear <= 3)
          {
              this.gear = gear;
              return false;
          }

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

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

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


   }

   //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

public class BicycleTest {

public static void main(String[] args) {
    Bicycle bike = new Bicycle();

    Scanner input = new Scanner(System.in);
    int intGear = 0;
    int intCadence=0;
        do {
            System.out.println("Enter the gear :");
            intGear = input.nextInt();

        } while(bike.setGear(intGear));
        do {
            System.out.println("Enter the cadence :");
            intCadence = input.nextInt();
        } while(bike.setCadence(intCadence));
            int speed = bike.getSpeed();
            System.out.println("Your speed in MPH is: ");
            System.out.println(speed);

    }

}




回答3:


Use a while loop with a condition false loop through the fragment, to update the value store it in a variable, and return the variable once the condition for while loop is met!



来源:https://stackoverflow.com/questions/59023186/how-to-keep-prompting-for-the-correct-user-input-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!