user3444913
user3444913

Reputation: 17

Asking the user to run the program again using a simple while loop

I am trying to run this program. After the user inputs the values, the program will output the values. my question is that the program is supposed to ask the user to enter any number to run the program again or -1 to quit, but I couldn't figure where to place the while loop in the code. I need to know where to place the while loop exactly. Should it be at the beginning of the java code or at the end. The code is below.

import java.util.Scanner;
public class Program4 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System. in );

        //Declare variables
        double numb;
        double absolute, meanNum = 0, medianNum = 0;
        double num1, num2, num3;
        double m;
        double me;
        double ab;

        System.out.println("Enter a number"); //prompting the user to enter information
        numb = input.nextDouble();

        while (numb != -1) {

            ab = absolute(numb);
            System.out.printf("The absolute number is %f\n", ab); //prompting the user to enter information

            System.out.println("Enter three numbers"); //prompting the user to enter information
            num1 = input.nextDouble();
            num2 = input.nextDouble();
            num3 = input.nextDouble();

            m = meanNum(num1, num2, num3); // Calling the method Mean Number
            System.out.printf("The Mean Number is: %f\n", m);

            me = medianNum(num1, num2, num3); // Calling the method Median Number
            System.out.printf("The median Number is %f\n", me);
        }
    }
    //// Creating another method for finding Mean Number
    public static double meanNum(double num1, double num2, double num3) {
        double meanNumb = 0;
        meanNumb = (num1 + num2 + num3) / 3; // Calculating the mean number by summing the numbers entered divided by three

        return (meanNumb);
    }
    // Creating another method for finding Median Number
    public static double medianNum(double num1, double num2, double num3) {
        //Using the IF Statement to find the median value of three numbers entered
        double medianNum = 0;
        if (num1 < (num3) && (num1 > num2))
            medianNum = num1;
        if ((num2 < num1) && (num2 > num3))
            medianNum = num2;
        if ((num3 < num2) && (num3 > num1))
            medianNum = num3;
        if ((num1 < num2) && (num1 > num3))
            medianNum = num1;
        if ((num2 < num3) && (num2 > num1))
            medianNum = num2;
        if ((num3 < num1) && (num3 > num2))
            medianNum = num3;

        return (medianNum);
    }
    // Creating a separated method for finding the absolute value of a number
    public static double absolute(double numb) {
        double ab = 0;
        if (numb < 0) {
            ab = numb * -1;
        } else {
            ab = numb;


        }

        return ab;
    }
}

Upvotes: 1

Views: 8950

Answers (4)

Nico
Nico

Reputation: 3489

do-while loops are what you are looking for. It would be something like this:

do {
// whatever
} while(numb != 1);

do-while loops make sure the loop runs at least once and then depending on your while statement, they would either keep going or exit. So You can place the following lines within the do-while loop:

System.out.println("Enter a number"); //prompting the user to enter information
numb = input.nextDouble();

And after this simply include everything your while loop already has in it. Every time the loop runs, it will ask the user to Enter a number and depending on what number they enter, your program will either keep running or quit.

You can look up Oracle's tutorial for that here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

Update:

There is a chance you will run into some exceptions upon running this program. I would suggest you do the following to avoid any problems:

do {
     System.out.print("Enter a number (Enter -1 to exit): "); //prompting the user to enter information
     numb = input.nextDouble();


     if (numb != -1) {
         ab = absolute(numb);
         System.out.printf("The absolute number is %f\n", ab); //prompting the user to enter information

         System.out.println("Enter three numbers"); //prompting the user to enter information
         num1 = input.nextDouble();
         num2 = input.nextDouble();
         num3 = input.nextDouble();

         m = meanNum(num1, num2, num3); // Calling the method Mean Number
         System.out.printf("The Mean Number is: %f\n", m);

         me = medianNum(num1, num2, num3); // Calling the method Median Number
         System.out.printf("The median Number is %f\n", me);
     }
} while (numb != -1);

The if-statement makes sure the rest of the code only runs if the user entered a number other than -1.

Upvotes: 2

nashcheez
nashcheez

Reputation: 5183

You can use the do.. while loop!

do {
    System.out.println("Enter a number"); //prompting the user to enter information
    numb = input.nextDouble();

    ab = absolute(numb);
    System.out.printf("The absolute number is %f\n", ab); //prompting the user to enter information

    System.out.println("Enter three numbers"); //prompting the user to enter information
    num1 = input.nextDouble();
    num2 = input.nextDouble();
    num3 = input.nextDouble();

    m = meanNum(num1, num2, num3); // Calling the method Mean Number
    System.out.printf("The Mean Number is: %f\n", m);

    me = medianNum(num1, num2, num3); // Calling the method Median Number
    System.out.printf("The median Number is %f\n", me);
} while (numb != -1);

Upvotes: 0

Jay Dharmendra Solanki
Jay Dharmendra Solanki

Reputation: 579

You can use a DO While loop, which is exit controlled, unlike while loop which is entry controlled

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System. in );

    //Declare variables
    double numb;
    double absolute, meanNum = 0, medianNum = 0;
    double num1, num2, num3;
    double m;
    double me;
    double ab;



    do {
        System.out.println("Enter a number"); //prompting the user to enter information
        numb = input.nextDouble();
        ab = absolute(numb);
        System.out.printf("The absolute number is %f\n", ab); //prompting the user to enter information

        System.out.println("Enter three numbers"); //prompting the user to enter information
        num1 = input.nextDouble();
        num2 = input.nextDouble();
        num3 = input.nextDouble();

        m = meanNum(num1, num2, num3); // Calling the method Mean Number
        System.out.printf("The Mean Number is: %f\n", m);

        me = medianNum(num1, num2, num3); // Calling the method Median Number
        System.out.printf("The median Number is %f\n", me);
    } while (numb != -1);
}

Upvotes: 0

anirudh
anirudh

Reputation: 4176

Just put a message at the end of the while loop that you have to ask the user for another input and read that input into numb again as below.

System.out.println("Enter a number");
numb = input.nextDouble();

Upvotes: 0

Related Questions