boolean Posted September 25, 2013 Share Posted September 25, 2013 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 More sharing options...
pears Posted September 26, 2013 Share Posted September 26, 2013 How would you program it? How far have you got? 1 Link to comment Share on other sites More sharing options...
Greg H. Posted September 26, 2013 Share Posted September 26, 2013 Question: Why would you need to convert the value to an integer? 1 Link to comment Share on other sites More sharing options...
Unity+ Posted September 26, 2013 Share Posted September 26, 2013 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. Link to comment Share on other sites More sharing options...
Greg H. Posted September 26, 2013 Share Posted September 26, 2013 Ahh. Well, that makes more sense that what I thought he was up to. Link to comment Share on other sites More sharing options...
boolean Posted September 26, 2013 Author Share Posted September 26, 2013 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 More sharing options...
Strange Posted September 26, 2013 Share Posted September 26, 2013 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. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now