Skoosky
Skoosky

Reputation: 13

If statement loop

So I made a program to do different math problems depending on what the user wants, and I don't want the program to end after the user is done. Right now, the loop I have just repeats the same option over and over. The desired effect is to be able to keep choosing which problem you want to do. I am fairly new to coding so I apologize if this is a simple question.

public static void main(String[] args)
{
    System.out.println("What problem do you want to do?");
    System.out.println("1:Celcius to Farenheit");
    System.out.println("2:Slope Formula");
    System.out.println("3:Averages");
    System.out.println("4:Perimeter");
    System.out.println("5:Exit\n\n");
    
    Scanner keyboard = new Scanner(System.in);
    
    int response = keyboard.nextInt();

    
    do
    {
    if (response == 1)
    {
        System.out.println("Degrees Celcius:");
        int degrees = keyboard.nextInt();
        System.out.println(degrees + " degrees Celcius is " + celsToFarenheit(degrees) + " degrees Farenheit.");
    }
    else if (response == 2)
    {
        System.out.println("x1:");
        int x1 = keyboard.nextInt();
        System.out.println("y1:");
        int y1 = keyboard.nextInt();
        System.out.println("x2:");
        int x2 = keyboard.nextInt();
        System.out.println("y2:");
        int y2 = keyboard.nextInt();
        System.out.println("The slope is " + slopeFormula(x1,y1,x2,y2));
    }
    else if (response == 3)
    {
        System.out.println("First number:");
        int num1 = keyboard.nextInt();
        System.out.println("Second number:");
        int num2 = keyboard.nextInt();
        System.out.println("The average is " + average(num1,num2));
    }
    else if (response == 4)
    {
        System.out.println("Length:");
        int length = keyboard.nextInt();
        System.out.println("Width:");
        int width = keyboard.nextInt();
        System.out.println("The perimeter is " + perimeter(length,width));
    }
}
while (response != 5);
    

Upvotes: 1

Views: 129

Answers (2)

erwoods89
erwoods89

Reputation: 43

Declare int response outside of the loop. Inside the loop place response = keyboard.nextInt();

With int response = keyboard.nextInt() outside of the loop, you never reach it again. Also, place the options inside the loop so they will display each time.

public static void main(String[] args) {


    Scanner keyboard = new Scanner(System.in);

    int response;


    do {
        System.out.println("What problem do you want to do?");
        System.out.println("1:Celcius to Farenheit");
        System.out.println("2:Slope Formula");
        System.out.println("3:Averages");
        System.out.println("4:Perimeter");
        System.out.println("5:Exit\n\n");

        response = keyboard.nextInt();
        if (response == 1) {
            System.out.println("Degrees Celcius:");
            int degrees = keyboard.nextInt();
            System.out.println(degrees + " degrees Celcius is " + celsToFarenheit(degrees) + " degrees Farenheit.");
        } else if (response == 2) {
            System.out.println("x1:");
            int x1 = keyboard.nextInt();
            System.out.println("y1:");
            int y1 = keyboard.nextInt();
            System.out.println("x2:");
            int x2 = keyboard.nextInt();
            System.out.println("y2:");
            int y2 = keyboard.nextInt();
            System.out.println("The slope is " + slopeFormula(x1,y1,x2,y2));
        } else if (response == 3) {
            System.out.println("First number:");
            int num1 = keyboard.nextInt();
            System.out.println("Second number:");
            int num2 = keyboard.nextInt();
            System.out.println("The average is " + average(num1,num2));
        } else if (response == 4) {
            System.out.println("Length:");
            int length = keyboard.nextInt();
            System.out.println("Width:");
            int width = keyboard.nextInt();
            System.out.println("The perimeter is " + perimeter(length,width));
        }
    }
    while (response != 5);
}

Upvotes: 0

Andrew Truckle
Andrew Truckle

Reputation: 19207

Change this bit like this:

do
{
int response = keyboard.nextInt();
if (response == 1)

If you move the keyboard.nextInt() call to inside the do loop it will now repeatedly ask the user for input.

You had this line of code before the loop so it only processed once.

Upvotes: 1

Related Questions