Jump to content

Java Help (Java language only)

Featured Replies

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)

How would you program it? How far have you got?

Question: Why would you need to convert the value to an integer?

Question: Why would you need to convert the value to an integer?

I think he means taking an input string from let's say a Scanner object and convert that to a integer value to apply the proper operations upon it.

Ahh.

Well, that makes more sense that what I thought he was up to.

  • Author
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

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.

Archived

This topic is now archived and is closed to further replies.

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.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.