问题
I have an assignment for an intro to programming class I'm taking, and I don't have any errors but it still isn't working properly. The assignment is to write a program that converts temperatures both from celsius to fahrenheit and vice versa. It also has to be done in different methods and must then loop back through to be done over and over. This part is where I'm now having a problem. the line
if(retry == y)
isn't working properly. Thanks in advance.
package cps101temperatureconverter;
import static
cps101temperatureconverter.CPS101TemperatureConverter.enteredTemp;
import java.util.Scanner;
public class CPS101TemperatureConverter
{
//char inputChoice;
int input;
static double enteredTemp;
static double calculatedTemp;
static char retry = 'y';
static Boolean moreToProcess = true;
static double fahrenheitTemp;
static double celsiusTemp;
Scanner keyboard = new Scanner(System.in);
static char c = 'C';
static char f = 'F';
public static void main(String[] args)
{
char inputChoice;
Scanner keyboard = new Scanner(System.in);
String inputTempString;
String inputTypeString;
String retryString;
//char retry = 'y';
do
{
System.out.println("This program will convert temperatures.");
System.out.println("Please Enter a Temperature: ");
inputTempString= keyboard.nextLine();
enteredTemp = Double.parseDouble(inputTempString);
System.out.println("You have entered " + enteredTemp);
System.out.println("Is this value in (C)elsius or (F)ahrenheit? ");
inputTypeString = keyboard.nextLine();
inputChoice = inputTypeString.charAt(0);
if (inputChoice == c)
{
CelsiusToFahrenheit();
}
else if (inputChoice == f)
{
FahrenheitToCelsius();
}
else
{
System.out.println("You have entered an invalid answer. Please try again.");
}
System.out.println("Would you like to convert another temperature? Enter yes or no. ");
retryString = keyboard.nextLine();
retry = retryString.charAt(0);
if (retry == y)
{
moreToProcess = true;
}
else{
moreToProcess = false;
}
}while(moreToProcess = true);
System.out.println("The Program will terminate now.");
}
static void CelsiusToFahrenheit()
{
calculatedTemp = (9.0/5.0)*enteredTemp + 32;
System.out.println(enteredTemp + " converted to Fahrenheit is " + calculatedTemp);
}
static void FahrenheitToCelsius()
{
calculatedTemp = (enteredTemp - 32)*(9.0/5.0);
System.out.println(enteredTemp + " converted to Celsius is " + calculatedTemp);
}
}
回答1:
The short answer is that you haven't initialized c or f. They are just static char variables but you have to give them a value to compare against.
static char c = 'C';
static char f = 'F';
Note however that this comparison is case sensitive; if the user inputs lower case c or f it won't work. You should try making your program more explicit as to what input it expects. Maybe like this:
System.out.println("Is this value in (C)elsius or (F)ahrenheit? ");
回答2:
Your fields called c
and f
are not defined. Try:
static char c = 'c';
static char f = 'f';
Good luck.
回答3:
There were two issues in your program:
- Not assigning
static char c='c'
andstatic char f='f'
- Not instantiating
keyboard
forinputTypeString
Apart from the above two problems, I also suggest using keyboard.nextDouble()
(which I have already done in the program given below) instead of using Double.parseDouble()
. There is nothing wrong with your approach but the suggested approach is cleaner.
Another suggestion is to convert inputTypeString
into the lower case before getting charAt(0)
out of it. This will allow the user to enter either of small letter or capital letter (e.g. c
or C
). I have already done this as well in the program given below.
Given below is the working program.
import java.util.Scanner;
public class CPS101TemperatureConverter {
int input;
static double enteredTemp;
static double calculatedTemp;
char retry;
static Boolean moreToProcess = true;
static double fahrenheitTemp;
static double celsiusTemp;
Scanner keyboard = new Scanner(System.in);
static char c='c';
static char f='f';
public static void main(String[] args) {
char inputChoice;
Scanner keyboard = new Scanner(System.in);
String inputTypeString="";
System.out.println("This program will convert temperatures.");
System.out.print("Please Enter a Temperature: ");
enteredTemp = keyboard.nextDouble();
System.out.println("You have entered " + enteredTemp);
System.out.print("Is this value in Celsius or Fahrenheit? ");
keyboard = new Scanner(System.in);
inputTypeString = keyboard.nextLine();
System.out.println("Your choice is: "+inputTypeString);
inputChoice = inputTypeString.toLowerCase().charAt(0);
if (inputChoice == c) {
CelsiusToFahrenheit();
} else if (inputChoice == f) {
FahrenheitToCelsius();
} else {
System.out.println("You have entered an invalid answer. Please try again.");
}
}
static void CelsiusToFahrenheit() {
calculatedTemp = (9.0 / 5.0) * enteredTemp + 32;
System.out.println(enteredTemp + " converted to Fahrenheit is " + calculatedTemp);
}
static void FahrenheitToCelsius() {
calculatedTemp = (enteredTemp - 32) * (9.0 / 5.0);
System.out.println(enteredTemp + " converted to Celisu is " + calculatedTemp);
}
}
来源:https://stackoverflow.com/questions/58435943/java-temperature-conversion-program-not-working