我再次遇到與我的程序相同的問題。某一範圍內沒有輸出
這次的問題是程序在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.
這不一定是答案,但是再一次,OP並沒有真正清楚問題究竟是什麼。 – Vulcan
@Vulcan我會考慮答案的這一部分A.如果有大括號,他們和我們就會更容易找出他們正在嘗試做的事情。如果實施該建議,解決方案的第二部分B部分可能是顯而易見的。 –
我同意你的看法(我更喜歡括號),但這裏的問題實際上是簡單的物流,並且位於OP使用括號的區域內。 – Vulcan