1

OK so this is my first post here. (Be kind to me :)) I'm like two weeks old to java and wanted to create an app that can do the jobs of a total, average and average grader. My question here is. How can I return to a specific line of code after one operation has ended Eg: I want the code to runString menu1="Input the number of the operation you desire "; after the total or average has been found. Thanks in advance

import java.util.Scanner;
public class grader
{
    public static void main (String args[])
    {
    Scanner input=new Scanner(System.in);
    System.out.println ("Input your Chemistry marks");
    float a= input.nextFloat();

    System.out.println ("Input your Biology marks");
    float b= input.nextFloat();

    System.out.println ("Input your Physics marks");
    float c= input.nextFloat();

    String menu1="Input the number of the operation you desire ";
    String op1="1. Total ";
    String op2="2. Average ";
    String op3="3. Grade ";
    String op4="4. Exit ";
    System.out.println (menu1+op1+op2+op3+op4);

    Scanner menuselect=new Scanner(System.in);
    int option= input.nextInt();

            float tot=(a+b+c);
            float avg=((a+b+c)/3);
    if (option==1)
        System.out.println ("Your total is "+tot);

    else if (option==2)
        System.out.println ("Your average is "+avg);

    else if (option==3)
        if (avg<0.0)
            System.out.println ("Please input proper data");
        else if (avg<=49.9)
            System.out.println ("Fail");
        else if (avg<=69.9)
            System.out.println ("Pass");
        else if (avg<=100.0)
            System.out.println ("Excellent");
        else 
            System.out.println ("Please input proper data");
    }
}
StackFlowed
  • 6,664
  • 1
  • 29
  • 45
Warren Manuel
  • 51
  • 1
  • 1
  • 5

4 Answers4

5

You should use the do-while loop. How it works is it executes some code, checks to see if a condition evaluates to true, and if so, executes the code again.

do {
    //your code here
}
while (/*some condition*/);
bcsb1001
  • 2,834
  • 3
  • 24
  • 35
Epiglottal Axolotl
  • 1,048
  • 8
  • 17
1

Usually when you want to repeat one or more lines of code, you do so using a loop. In general, a loop is set up like this:

while(loopGuard){

    //code to repeat

}

Where loopGuard is some boolean statement that gets updated inside your loop. The code inside the loop will continue executing until your loopGuard statement is no longer true.

In your case, you may want to loop until the user presses a certain button, until it has run a certain number of times, or any other arbitrary reason you have chosen. The main thing to remember is to make sure that your loop guard will eventually become false, otherwise you'll find yourself in an infinite loop.

Scraniel
  • 103
  • 7
0

In your case you should use a while loop with the usage of continue and break. A second option, you can use a labeled break statement (explained here).

Btw, in java language there a goto keyword. But it's not implemented (or removed). Read here for more details.

Community
  • 1
  • 1
Naili
  • 1,564
  • 3
  • 20
  • 27
0

I would suggest a do - while loop . A do while loop runs at least once , which is necessary as you're program needs to be run at least once . Put all the statements from Input the operation number to the calculating statements under do while loop . It should work out just fine .

  • This solution is already posted by other answerers. Clarify what additional value ad your post is giving – Raju May 30 '16 at 16:04