我的程序又遇到了同样的问题。
这次的问题是程序在 59 到 20 之间没有输出任何内容。程序只是终止了。据我所知,所有 if 语句的格式都相同,但也许我只需要一些新的眼光,因为我的大脑正在阻止错误
这是代码:
import java.util.Scanner;
public class LazyDaysCamp
{
public static void main (String[] args)
{
int temp;
Scanner scan = new Scanner(System.in);
System.out.println ("What's the current temperature?");
temp = scan.nextInt();
if (temp > 95 || temp < 20)
{
System.out.println ("Visit our shops!");
} else if (temp <= 95)
if (temp >= 80)
{
System.out.println ("It's good weather for swimming");
} else if (temp >=60)
if (temp < 80)
{
System.out.println ("It's good weather for tennis");
} else if (temp >= 40)
if (temp < 60)
{
System.out.println ("It's good weather for golf");
} else if (temp >= 20)
if (temp < 40)
{
System.out.println ("It's good weather for skiing");
}
}
}
}
}
}
}
正如有人指出的那样,我知道 if 语句有点过分,但我必须这样做(级联 if)。否则我会使用逻辑运算符。
这是输出:
----jGRASP exec: java LazyDaysCamp
What's the current temperature?
100
Visit our shops!
----jGRASP: operation complete.
----jGRASP exec: java LazyDaysCamp
What's the current temperature?
85
It's good weather for swimming
----jGRASP: operation complete.
----jGRASP exec: java LazyDaysCamp
What's the current temperature?
70
It's good weather for tennis
----jGRASP: operation complete.
----jGRASP exec: java LazyDaysCamp
What's the current temperature?
50
----jGRASP: operation complete.
----jGRASP exec: java LazyDaysCamp
What's the current temperature?
30
----jGRASP: operation complete.
----jGRASP exec: java LazyDaysCamp
What's the current temperature?
15
Visit our shops!
----jGRASP: operation complete.