Nest如果没有输出

时间:2014-02-17 03:45:39

标签: java

编译运行时,我没有得到任何输出。当成本低于100并且评级为4时,尝试获取建议的输出。如果打印保存您的钱

System.out.print("Enter cost: ");
double cost = input.nextDouble();

System.out.println("Enter a Star value between 1-4");
int rating = input.nextInt();

if (cost < 100){
    if (rating == 4 )
        System.out.println("Recommended");
}
else
    System.out.println("Save your money");

2 个答案:

答案 0 :(得分:2)

如果您只希望在评级为4且成本为&lt; 4时打印'推荐'。 100然后你可以这样做:

if (cost < 100 && rating == 4)
    System.out.println("Recommended");
else
    System.out.println("Save your money");

答案 1 :(得分:0)

它根本不需要给出两个if循环。你可以拥有&amp;&amp;如果循环,那么一次性制作的算子。

if(cost < 100 && rating == 4)

这是理想的解决方案,如果不需要两次,给予的意义是什么。

你的方法仍然需要相同的ans使用它。

if (cost<100) {
            if (rating==4) {
                System.out.println("Recomanded");
            }   


        }else {
            System.out.println("Save your money");
        }
相关问题