user3517591
user3517591

Reputation: 1

How to loop a if else

I have been having trouble with looping my if else statement, could you please give me a hand. What I want to happen is when the numbers have been changed for 'hot' and 'cold' I want it to go back to the start of the if else statement so its able to change the numbers again.

import java.util.Scanner;

public class test {

    public static void main(String args[])
    {
        Scanner gus = new Scanner (System.in);

        double incrd, hot, cold;

        hot = 10;
        cold = 9;

        System.out.println("press 1 for hotter");
        System.out.println("press 2 for colder");
        System.out.println("press 3 for more preasure");
        System.out.println("press 4 for less pressure");

        incrd = gus.nextDouble();

        if (incrd == 1) {
            hot += 2.5;
            cold = cold - 2.5;
        } else if (incrd == 2) {
            hot = hot - 2.5;
            cold += 2.5;
        } else if (incrd == 3){
            hot += 2.5;
            cold += 2.5;
        } else if (incrd == 4){
            hot = hot - 2.5;
            cold = cold - 2.5;
        } else {
            System.out.println ("invalid Number please enter either 1 or 2");
        }

        System.out.println(hot);
        System.out.println(cold);
    }
}

Upvotes: 0

Views: 142

Answers (6)

Sheng
Sheng

Reputation: 3555

You need a while loop like:

import java.util.Scanner;

public class test {

    public static void main(String args[])
    {
        Scanner gus = new Scanner (System.in);

        double hot, cold;
        int incrd;

        hot = 10;
        cold = 9;

        System.out.println("press 1 for hotter");
        System.out.println("press 2 for colder");
        System.out.println("press 3 for more preasure");
        System.out.println("press 4 for less pressure");
        System.out.println("press 5 to finish.");

        while (true) {
            incrd = gus.nextInt();

            if (incrd == 1) {
                hot += 2.5;
                cold -= 2.5;
            } else if (incrd == 2) {
                hot -= 2.5;
                cold += 2.5;
            } else if (incrd == 3){
                hot += 2.5;
                cold += 2.5;
            } else if (incrd == 4){
                hot -= 2.5;
                cold -= 2.5;
            } else if (incrd == 5){
                break;
            } else {
                System.out.println ("invalid Number please enter either 1 or 2");
            }
        }
        System.out.println(hot);
        System.out.println(cold);
    }
}

For the infinite loop, you could also do it like:

while (incrd.hasNextInt()) {
    incrd = gus.nextInt();
    ....
}

In addition, you define the incrd as double number. So you'd better not to compare it with an integer using "==".

Upvotes: 0

I think something like this would conveniently suit your need:
(actually when you input any non-integer value, it will exit the program..)

public class test {

    public static void main(String args[]) {

        Scanner gus = new Scanner(System.in);

        int incrd;
        double hot, cold;

        hot = 10;
        cold = 9;

        System.out.println("press 1 for hotter");
        System.out.println("press 2 for colder");
        System.out.println("press 3 for more preasure");
        System.out.println("press 4 for less pressure");
        System.out.println("press 'x' to Exit the program");

        while (gus.hasNextInt()) {
            incrd = gus.nextInt();

            if (incrd == 1) {

                hot += 2.5;
                cold = cold - 2.5;

            } else if (incrd == 2) {

                hot = hot - 2.5;
                cold += 2.5;
            } else if (incrd == 3) {

                hot += 2.5;
                cold += 2.5;
            } else if (incrd == 4) {

                hot -= 2.5;
                cold -= 2.5;
            } else {

                System.out.println("invalid Number please enter either 1 or 2");

            }

            System.out.println(hot);
            System.out.println(cold);
        }
    }

}

Upvotes: 0

JustGreat
JustGreat

Reputation: 561

Try this !! If gus. returns -1 we stop the code

import java.util.Scanner;

public class test {

    public static void main(String args[])
    {
        Scanner gus = new Scanner (System.in);

        double incrd, hot, cold;

        hot = 10;
        cold = 9;

        int StopValue;
        //If the value is -1 we stop the loop
        StopValue=-1; 

        System.out.println("press 1 for hotter");
        System.out.println("press 2 for colder");
        System.out.println("press 3 for more preasure");
        System.out.println("press 4 for less pressure");

        while (true) {
            incrd = gus.nextDouble();

if (incrd == StopValue)
{
break;
}
else
{

            if (incrd == 1) {
                hot += 2.5;
                cold = cold - 2.5;
            } else if (incrd == 2) {
                hot = hot - 2.5;
                cold += 2.5;
            } else if (incrd == 3){
                hot += 2.5;
                cold += 2.5;
            } else if (incrd == 4){
                hot = hot - 2.5;
                cold = cold - 2.5;
            } else if (incrd == 5){
                break;
            } else {
                System.out.println ("invalid Number please enter either 1 or 2");
            }
        }
        System.out.println(hot);
        System.out.println(cold);
}//else
    }//while
}

Upvotes: 0

Peter Baldwin
Peter Baldwin

Reputation: 44

I would use a while loop to get back to the top of you if/else blocks:

while (1) {
 incrd = gus.nextDouble();



 if (incrd == 1) {

    hot += 2.5;
    cold = cold - 2.5;

 } else if (incrd == 2) {

    hot = hot - 2.5;
    cold += 2.5;
 } else if (incrd == 3){

    hot += 2.5;
    cold += 2.5;
 } else if (incrd == 4){

    hot = hot - 2.5;
    cold = cold - 2.5;
 } else {

    // 3 & 4 are valid options also !
    System.out.println ("invalid Number please enter either 1 or 2");

 }

 System.out.println(hot);
 System.out.println(cold);

}  // end of INFINITE while loop 

Upvotes: 0

NullAmbitions
NullAmbitions

Reputation: 1

One problem is that the way you describe it isn't very clear but If I understand correctly you simply need to put it in a while(true) loop. I'm not a fan of doing homework for others but here's the solution to your question.

import java.util.Scanner;

public class test {

public static void main(String args[])
{
    Scanner gus = new Scanner (System.in);

    double incrd, hot, cold;

    hot = 10;
    cold = 9;


while(true){
    System.out.println("press 1 for hotter");
    System.out.println("press 2 for colder");
    System.out.println("press 3 for more preasure");
    System.out.println("press 4 for less pressure");

    incrd = gus.nextDouble();

    if (incrd == 1) {
        hot += 2.5;
        cold = cold - 2.5;
    } else if (incrd == 2) {
        hot = hot - 2.5;
        cold += 2.5;
    } else if (incrd == 3){
        hot += 2.5;
        cold += 2.5;
    } else if (incrd == 4){
        hot = hot - 2.5;
        cold = cold - 2.5;
    } else {
        System.out.println ("invalid Number please enter either 1 or 2");
    }

    System.out.println(hot);
    System.out.println(cold);
        }
    }
}

Upvotes: 0

tbodt
tbodt

Reputation: 16987

That would be done with an infinite loop. An infinite loop looks like this:

while (true) {
    // do stuff
}

Take the code that you want to loop over and over again, and put it in the loop. You are done.

Well, almost done. That code will never stop. You would add another menu choice to stop the program, and if the user presses that number, you would run this line of code:

break;

That stops the loop, and the program. prints out hot and cold.

Upvotes: 1

Related Questions