Meg K
Meg K

Reputation: 15

Where to add a do while loop to have the program repeat or terminate?

I'm new to programming, this is my first Java class. I've gotten a code to calculate temperature from celsius to fahrenheit and vice versa, but I cannot figure out where I should have entered a Do While loop to have the program repeat or terminate.

import java.util.Scanner;

public class PracticeTemp {
    private static float f = 0, c = 0;
    private static final Scanner scan = new Scanner (System.in);
    private static int converChoice = 1;

    public static void main(String [] args) {
    System.out.println("Press 1 for C->F or 2 for F->C 3 to Quit");
    converChoice = scan.nextInt();

    if (converChoice == 1) 
        convertCtoFAndPrint();
    else if
    (converChoice == 2) 
        convertFtoCAndPrint();
    else if
    (converChoice == 3)
    System.out.println("The program will now terminate");
    System.exit(0);

    }

    public static void convertFtoCAndPrint() {
    System.out.println("Please enter degrees F");
    f = scan.nextFloat();

    c = (5* (f - 32)) / 9f;

    System.out.println(f + " degrees F is " + c + " degrees C.");
    }

     public static void convertCtoFAndPrint() {
    System.out.println("Please enter degrees C");
    c = scan.nextFloat();

    f = (9 * c + 160) / 5f;

    System.out.println(c + " degrees C is " + f + " degrees F.");
    }
}

Upvotes: 0

Views: 216

Answers (2)

Alexandru Cancescu
Alexandru Cancescu

Reputation: 998

In order for the program to repeat and get input from the console you have to include a

while(true){
    //this is an infinite loop
}

in the main function where the user inputs data:

public static void main(String [] args) {
        while(true) { //this in an infinite loop
            System.out.println("Press 1 for C->F or 2 for F->C 3 to Quit");
            converChoice = scan.nextInt();

            if (converChoice == 1)
                convertCtoFAndPrint();
            else if (converChoice == 2)
                convertFtoCAndPrint();
            else if (converChoice == 3) {
                System.out.println("The program will now terminate");
                System.exit(0);//this stops the program
            }
        }
    }

Or you can add:

do{

}while(converChoice!=3);

This repeats as long as convertChoice (user input) is not equal to 3

public static void main(String [] args) {
        do {
            System.out.println("Press 1 for C->F or 2 for F->C 3 to Quit");
            converChoice = scan.nextInt();

            if (converChoice == 1)
                convertCtoFAndPrint();
            else if (converChoice == 2)
                convertFtoCAndPrint();
            else if (converChoice == 3) {
                System.out.println("The program will now terminate");
                // System.exit(0); is no longer needed since the program exists the loop
            }
        } while (converChoice!=3);
    }

Upvotes: 0

fill͡pant͡
fill͡pant͡

Reputation: 1165

You could put it arround the code inside the main method body such that every time the first execution of the program is complete, it repeats by showing the "Press 1 ..." message, re-accepting input and so on.

public static void main(String[] argz){
   do{
      //Do stuff.
   }while(condition);
}

In your case, the condition should be something along the lines of converChoice != 3 which will essentially stop when converChoice is equal to 3.

Upvotes: 1

Related Questions