简体   繁体   English

java温度转换程序不工作

[英]java temperature conversion program not working

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);
}

}

The short answer is that you haven't initialized c or f.简短的回答是你还没有初始化 c 或 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.如果用户输入小写的 c 或 f,它将不起作用。 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? ");

Your fields called c and f are not defined.未定义名为cf的字段。 Try:尝试:

static char c = 'c';
static char f = 'f';

Good luck.祝你好运。

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.我有一个关于我正在学习的 class 编程介绍的作业,我没有任何错误,但它仍然无法正常工作。 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);
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM