When I run the program, no matter what number I put in, I keep getting an output
ID: 3650664 • Letter: W
Question
When I run the program, no matter what number I put in, I keep getting an output of "Outside the data range." What am I doing wrong here?import java.util.Scanner;
public class Temperature
{
public static void main(String[] args)
{
Scanner predict = new Scanner(System.in);
double temp;
String season;
String summer;
String spring;
String fall;
String winter;
//Intro and instructions to the program
System.out.println("I am a magic computer program");
System.out.println("All you have to do is type in the temperature outside");
System.out.println("And I will tell you what season it is");
System.out.println();
//Asking the user to input the temperature data
System.out.println("Check the thermometer, what temperature is it outside?");
temp = predict.nextDouble();
if (temp >= 90) {
season = "summer";
}
if (temp >=70 && temp <=90); {
season = "spring";
}
if (temp >=50 && temp <=70); {
season = "fall";
}
if (temp <=50); {
season = "winter";
}
if (temp >=110); {
season = "Outside the valid range";
}
if (temp <=-5); {
season = "Outside the valid range";
}
System.out.println("It is probably " +season);
System.out.println();
}
}
Explanation / Answer
Remove the ; from the end of if-statements.
import java.util.Scanner;
public class Temperature
{
public static void main(String[] args)
{
Scanner predict = new Scanner(System.in);
double temp;
String season=" ";
String summer;
String spring;
String fall;
String winter;
//Intro and instructions to the program
System.out.println("I am a magic computer program");
System.out.println("All you have to do is type in the temperature outside");
System.out.println("And I will tell you what season it is");
System.out.println();
//Asking the user to input the temperature data
System.out.println("Check the thermometer, what temperature is it outside?");
temp = predict.nextDouble();
if (temp >= 90.0) {
season = "summer";
}
if (temp >=70 && temp <=90){
season = "spring";
}
if (temp >=50 && temp <=70) {
season = "fall";
}
if (temp <=50) {
season = "winter";
}
if (temp >=110) {
season = "Outside the valid range";
}
if (temp <=-5) {
season = "Outside the valid range";
}
System.out.println("It is probably "+season );
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.