7 Replies - 759 Views - Last Post: 11 April 2010 - 09:26 AM Rate Topic: -----

#1 mothan  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 22
  • Joined: 28-February 10

Temperature Conversion

Posted 11 April 2010 - 08:45 AM

Can someone please help me. I'm doing an infinite loop on temperature conversion. I need to allow the user to make a choice at the beginning of the loop. (right after the do) What do I need to add? Here is my source code:

package project2;

import java.util.Scanner;

public class Project2 {



public static void main(String[] args) {
//Create a Scanner
Scanner reader = new Scanner(System.in);

//two variables, undefined.

double usersEnteredTemperature; //the temperature you wish to get from
//the user.
double convertedTemperature; //the converted temperature based on
//the user choices and entered temperature.

//Enter Input
//Give user options and choices.



int menuChoice = 0; 

double celsius; 
double fahrenheit; 

do {

System.out.println("Enter the number your choice");
System.out.println("1. For Farenheit to Celsius");
System.out.println("2. For Celsius to Farenheit");
System.out.println("3.Exit");


if (menuChoice == 1) {
System.out.println("Enter Temperature in Farenheit : ");
usersEnteredTemperature = reader.nextDouble();

//do some conversion based on userinput store output into
//convertedTemperature Celsius
convertedTemperature = (5.0 / 9) * (usersEnteredTemperature - 32);
System.out.println("Your degrees will be in celsius");
System.out.println(convertedTemperature);
} else if (menuChoice == 2) {
System.out.println("Enter Temperature in Celsius: ");
usersEnteredTemperature = reader.nextDouble();

//do some conversion based on userinput store output into
//convertedTemperature Fahrenheit
convertedTemperature = (9.0 / 5) * usersEnteredTemperature + 32;
System.out.println("Your degrees will be in Farenheit");
System.out.println(convertedTemperature);
}

} while (menuChoice != 3);

System.out.println("Good Bye!");

}
}



Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: Temperature Conversion

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Temperature Conversion

Posted 11 April 2010 - 09:04 AM

It's because you never change the value of menuChoice:

...
int menuChoice = 0;
...
if (menuChoice == 1) {
...
} else if (menuChoice == 2) {
...
} while (menuChoice != 3);
...



You need to ask the user for input after you present the menu. :)

This post has been edited by n8wxs: 11 April 2010 - 09:06 AM

Was This Post Helpful? 0
  • +
  • -

#3 mothan  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 22
  • Joined: 28-February 10

Re: Temperature Conversion

Posted 11 April 2010 - 09:05 AM

help please!
Was This Post Helpful? -1
  • +
  • -

#4 mothan  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 22
  • Joined: 28-February 10

Re: Temperature Conversion

Posted 11 April 2010 - 09:12 AM

Thanks, I'm new at this and still confuse. How can I change the value of menuChoice?
Was This Post Helpful? 0
  • +
  • -

#5 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Temperature Conversion

Posted 11 April 2010 - 09:23 AM

Use your Scanner:
menuChoice = reader.nextInt();

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,865
  • Joined: 27-December 08

Re: Temperature Conversion

Posted 11 April 2010 - 09:24 AM

First, please do not bump your thread after 20 minutes; bump only if you still need help and your thread is about to fall off the page. In fact, threads with no replies are more likely to be looked at by our Experts, as when you reply to your own thread in such a short period of time, it implies that you have answered your own question.

As to assign menuChoice a new value, use the Scanner object you created to get the consoloe input. Like so: menuChoice = scan.nextInt();. You will want to get input after prompting the user with the menu.

Also, in the future, please properly indent your code. It makes it easier for us to read, and therefore help you debug, your code.
Was This Post Helpful? 1
  • +
  • -

#7 mothan  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 22
  • Joined: 28-February 10

Re: Temperature Conversion

Posted 11 April 2010 - 09:25 AM

Thanks, where do I insert this in my code? Is it right after the do.
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: Temperature Conversion

Posted 11 April 2010 - 09:26 AM

You simply take an integer input when you're done displaying the menu!

System.out.println("Enter the number your choice");
System.out.println("1. For Farenheit to Celsius");
System.out.println("2. For Celsius to Farenheit");
System.out.println("3.Exit");

// Changing the value of menu.
menuCoice = reader.nextInt();

// Might want to verify any other thing (say if the user entered
// -1 or 56... Have a "catch-all" statement (else).

if (menuChoice == 1) {
System.out.println("Enter Temperature in Farenheit : ");
usersEnteredTemperature = reader.nextDouble();


This post has been edited by Dogstopper: 11 April 2010 - 09:28 AM
Reason for edit:: fix code tags

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1