如何循环if else

时间:2014-04-10 01:26:55

标签: java loops if-statement repeat

我在循环使用if else语句时遇到了麻烦,请你帮个忙。我想要发生的事情是,当“热”和“冷”的数字被更改时,我希望它返回到if else语句的开头,以便能够再次更改数字。

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);
    }
}

6 个答案:

答案 0 :(得分:1)

这将通过无限循环完成。无限循环看起来像这样:

while (true) {
    // do stuff
}

获取要反复循环的代码,并将其放入循环中。你完成了。

好吧,差不多完成了。该代码永远不会停止。您将添加另一个菜单选项来停止该程序,如果用户按下该数字,您将运行以下代码行:

break;

停止循环和程序。打印出hotcold

答案 1 :(得分:0)

你需要一个while循环,如:

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);
    }
}

对于无限循环,你也可以这样做:

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

此外,您将incrd定义为双号。所以你最好不要使用“==”将它与整数进行比较。

答案 2 :(得分:0)

一个问题是你描述它的方式不是很清楚但是如果我理解正确你只需要把它放在一个while(true)循环中。我不是为别人做作业的粉丝,但这是你问题的解决方案。

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);
        }
    }
}

答案 3 :(得分:0)

如果/ else阻止我会使用while循环返回到你的顶部:

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 

答案 4 :(得分:0)

试试这个!!如果是格斯。返回-1我们停止代码

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
}

答案 5 :(得分:0)

我觉得这样的话可以很方便地满足你的需要:
(实际上当你输入任何非整数值时,它将退出程序..)

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);
        }
    }

}