gpt4 book ai didi

java温度转换程序不工作

转载 作者:行者123 更新时间:2023-12-02 01:16:49 25 4
gpt4 key购买 nike

我有一个关于我正在参加的编程类(class)介绍的作业,我没有任何错误,但它仍然无法正常工作。作业是编写一个程序,将温度从摄氏度转换为华氏度,反之亦然。它还必须以不同的方法完成,然后必须循环一遍又一遍地完成。这部分是我现在遇到问题的地方。线

if(retry == y)

无法正常工作。提前致谢。

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

}

最佳答案

简短的回答是您尚未初始化 c 或 f。它们只是静态 char 变量,但您必须给它们一个值来进行比较。

static char c = 'C';
static char f = 'F';

但请注意,此比较区分大小写;如果用户输入小写的 c 或 f 则不起作用。您应该尝试使您的程序更明确地了解它期望的输入。也许是这样的:

System.out.println("这个值是(C)elsius还是(F)ahrenheit吗?");

关于java温度转换程序不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58435943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com