I’m having the same troubles with my program again.
The issue this time is that the program doesn’t output anything for between 59 and 20. The program just terminates. As far as I can tell, all the if statements are formatted the same way but maybe I just need some fresh eyes on it because my brain is blocking out the error
Here’s the code:
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");
}
}
}
}
}
}
}
I know the if statements are a bit excessive as someone pointed out but I’m required to do so (cascading if). Otherwise I’d use logical operators.
Here’s the output:
----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.
Note that several of your if statements are unnecessary. If temperature is
>= 80, it cannot be anything other than< 80at the point when you check that it’s< 80. This is done several times.The issue is that your else-if-statements are being placed as alternates to these unnecessary if-statements that will always be true. As a result, the else-if condition is never even evaluated.