私は私のプログラムで再び同じ問題を抱えています。
今回の問題は、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.