Jump to content

Java Help (Java language only)


boolean

Recommended Posts

Hey Everyone, How would you program this in simple way?

 

I am trying to Create a program that Asks the user, via JOptionPane, to input a temperature value in Celsius and Convert this value
to an integer. i also want it to Check if the temperature is below -15 degrees. (Use a constant to store this value.) If so,
instruct the user to change out of their shorts and put on a light jacket.
How would you program this?
(new to programming)
Link to comment
Share on other sites

Heres how i did it.. Would you guys do the same?

*/


import javax.swing.*;


public class Temperature2 {

public static void main (String [] args) {


// Declare variables here

String userInput; //In celsius, input of temperature

int temperature1; //integar

final int WINTER_TIME = -15;



userInput = JOptionPane.showInputDialog(null,"Enter the temperature in celsius") ;

temperature1 = Integer.parseInt(userInput);


if (WINTER_TIME < temperature1); //if the temperature input is below -15 degrees

System.out.println("Put on shorts and a light jacket"); //The print the following




// all done, exit

System.out.println("\nEnd of processing.");

}// main

}// Temperature2
Link to comment
Share on other sites

Have you compiled and run the code? Did it work as you expected?

The logic of this statement:

if (WINTER_TIME < temperature1);

is wrong. It should be:

if (temperature1 < WINTER_TIME)

i.e. "if temperature less than WINTER_TIME"

Also, you don't want the semicolon at the end of the line. That will end the statement meaning that the next line (println) will be unconditional.

You might want to get in the habit of always putting braces around conditional statements. It avoids silly problems when you add extra lines. For example:

if (WINTER_TIME < temperature1) {
    System.out.println("Put on shorts and a light jacket");
}

And you want to sort out your indentation to make your code more readable.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.