简体   繁体   中英

How would I get my program to loop?

I am trying to get my code to repeat, and I try to enter "while" but it says:

illegal start of expression.

Any help would be appreciated as to where to enter this.

import java.util.Scanner;

public class GasMileage
{
    public static void main(String[ ] args)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("How many miles were driven?");
        int miles;
        miles = keyboard.nextInt();

        System.out.print("How many gallons were used?");
        int gallons;
        gallons = keyboard.nextInt();

        int mpg;
        mpg = miles / gallons;

        System.out.println(" The Miles-Per-Gallon used in this trip are " + mpg );
    }
}

Example of what you might be trying to do (details below)

import java.util.Scanner;

public class GasMileage
{
    public static void main(String[ ] args)
    {
        Scanner keyboard = new Scanner(System.in);
        boolean stillInLoop = true;

        while (stillInLoop)
        {
            System.out.print("How many miles were driven? ");
            int miles;
            miles = keyboard.nextInt();

            System.out.print("How many gallons were used? ");
            int gallons;
            gallons = keyboard.nextInt();

            int mpg;
            mpg = miles / gallons;

            System.out.println(" The Miles-Per-Gallon used in this trip are " + mpg);

            stillInLoop = false;
        }
    }
}

A few tips:

1) If you use a while condition, you are running that loop until something no longer remains true, in which case java will automatically exit the loop. These conditions are either true or false (boolean). I notice that you didn't specify what you'd like in your while loop. It would really help if you could provide us with more information as to what else you're trying to do.

2) Be sure to include the space in between your question and quotation marks in your print statements or your output will get bunched up like this:

Bad practice:

System.out.print("How many miles were driven?");

Output: How many gallons were used? 5

Good practice:

System.out.print("How many miles were driven? ");

Output: How many gallons were used? 5

Notice the spacing.

3) The code I gave you might seem vague but it's because I didn't have a condition to work with specifically. Figure out which section(s) of your code need to continue running until a certain condition has been met. I called the boolean variable "stillInLoop" in my example but generally this is extremely unhelpful to anyone reading your code and you'd probably be better off naming it something more helpful.

Hopefully this helped out a bit. Good luck!

You probably didn't use the right syntax with your while loop, it should look something like this:

//in your main method
while(true){
   //here you ask the user questions
}
//end of your program

So when you fill this in your program you get:

public class GasMileage{
   public static void main(String[ ] args){

      Scanner keyboard = new Scanner(System.in);

      while(true){
      System.out.print("How many miles were driven?");
      int miles;
      miles = keyboard.nextInt();

      System.out.print("How many gallons were used?");
      int gallons;
      gallons = keyboard.nextInt();

      int mpg;
      mpg = miles / gallons;

      System.out.println(" The Miles-Per-Gallon used in this trip are " + mpg );
   }  
}

This will loop your program until you force it to stop, to let the program stop otherwise you will need to change the 'true' in the while loop to something which is triggered by something in your code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM