gpt4 book ai didi

java - 我的 Java 方法运行不正确导致我的程序编译不正确

转载 作者:行者123 更新时间:2023-12-01 07:49:50 27 4
gpt4 key购买 nike

我正在编写一个程序,它将提示用户输入整数温度,然后提示用户选择该温度的刻度,华氏度、摄氏度或开尔文。然后它会提示用户从 3 个比例选项中选择他们想要转换成的温度,然后计算转换并显示最终温度。我的程序当前运行但不正确,我已经盯着这个问题好几天了,我不确定为什么它会这样编译。我对编码还很陌生,这是我一直在寻找一些建议的类作业。

封装温度转换器;导入java.util.*;

公共(public)类TempConverter{

public static void main(String[] args) 
{
double temp1;
double convertTo;
double finalTemp;
double currScale;
char cont='E';
char process='P';
int scale1=0;
int scale2=0;

do
{
//input
getTemp();
scaleOne(scale1);
toScale(scale2);

//process


System.out.print("Your final temp is: " +convert()+toScale(scale2));

}while(cont=='N');
}
private static double getTemp()
{
double temp;
double currentTemp;
Scanner userIn= new Scanner(System.in);
System.out.print("Enter a temperature to convert ");
while(!userIn.hasNextDouble())
{
System.out.println("That is not a valid temperature!");
System.out.print("Re-enter a temperature: ");
userIn.next();
}
temp=userIn.nextDouble();



return temp;
}

private static String scaleOne(int userChoice)
{

Scanner userIn= new Scanner(System.in);



System.out.print("What scale is your temp currently in?"+
"1: Fahrenheit" +
"2: Celsius" +
"3: Kelvin" +
"Please enter 1, 2, or 3: ");


String scale="";

switch(userChoice)
{
case 1:
{scale= "Fahrenheit";
break;}

case 2:
{scale="Celsius";
break;}

case 3:
{scale="Kelvin";
break;}

default:
{scale= "There was an error in your choice!";}
}

return scale;

}



private static String toScale(int userChoice)
{

Scanner userIn= new Scanner(System.in);



System.out.print("What scale would you like to convert to?"+
"1: Fahrenheit" +
"2: Celsius" +
"3: Kelvin" +
"Please enter 1, 2, or 3: ");


String scale2="";

switch(userChoice)
{
case 1:
{scale2= "Fahrenheit";
break;}

case 2:
{scale2="Celsius";
break;}

case 3:
{scale2="Kelvin";
break;}

default:
{scale2= "There was an error in your choice!";}
}

return scale2;

}



public static double convert()
{
double farToKel;
double farToCel;
double celToKel;
double celToFar;
double kelToFar;
double kelToCel;
int scale1=0;
double converts;

if(scaleOne(scale1)=="Fahrenheit" && toScale(scale1)=="Kelvin")
{
converts=farToKel=(getTemp()+459.67)* 5/9;
}

else if(scaleOne(scale1)=="Fahrenheit" && toScale(scale1)=="Celsius")
{
converts=farToCel=(getTemp()-32)*5/9;
}

else if(scaleOne(scale1)=="Celsius" && toScale(scale1)=="Kelvin")
{
converts=celToKel=getTemp()+273.15;
}

else if(scaleOne(scale1)=="Celsius" && toScale(scale1)=="Fahrenheit")
{
converts=celToFar=getTemp()*9/5+32;
}

else if(scaleOne(scale1)=="Kelvin" && toScale(scale1)=="Fahrenheit")
{
converts=kelToFar=getTemp()*9/5-459.67;
}

else
{
converts=kelToCel=getTemp()-276.15;
}

return converts;
}








}

这是它编译的内容

运行:输入温度以转换为 100您当前的温度是多少单位?1: 华氏度2: 摄氏度3: 开尔文请输入1, 2, 或3: 您想要转换成什么单位?1: 华氏度2: 摄氏度3: 开尔文请输入1, 2, 或3: 什么单位您当前的温度是多少?1: 华氏度2: 摄氏度3: 开尔文请输入1, 2, 或3: 您当前的温度是多少度?1: 华氏度2: 摄氏度3: 开尔文请输入1, 2, 或3: 您当前的温度是多少度?1: 华氏度2: 摄氏度3: 开氏度请输入1, 2, 或3: 您当前的温度是多少度?1: 华氏度2: 摄氏度3: 开尔文请输入1, 2, 或3: 您当前的温度是多少度?1: 华氏度2 :摄氏度3:开尔文请输入1、2或3:输入温度以转换2您想转换成什么温标?1:华氏度2:摄氏度3:开尔文请输入1、2或3:您的最终温度是:-274.15您的选择有误!构建成功(总时间:33秒)

这就像是没有以正确的顺序进行编译,或者以正确的顺序获取正确的信息并一遍又一遍地获取某些信息。一旦您输入数字温度,它会提示您输入比例,但不仅仅是一次打印出来,然后您输入第一个比例的选择,它会一直完成,而无需您输入toScale 的选择。

最佳答案

请参阅下面在代码中插入的注释和所做的更正。

public class TempConverter {

// variables that are needed in more than one method must be declared outside the methods
// (static since the methods are static; not recommended, but let it be for now)
static double temp1;
static String scale1;
static String scale2;

public static void main(String[] args) {
// get rid of unused variables
char cont = 'E';

do {
// input
// save all inputs to variables
temp1 = getTemp();
// Java parameters are pass by value, so to get a value out of the method, use the returned value
// then you don’t need the parameter
scale1 = scaleOne();
scale2 = toScale();

// process
// don’t call toScale() again, or the user will be prompted again,
// instead, use the value already stored in scale2
System.out.print("Your final temp is: " + convert() + ' ' + scale2);

// you should set cont to something if you want the loop to repeat

} while (cont == 'N');
}

private static double getTemp() {
double temp;
Scanner userIn = new Scanner(System.in);
System.out.print("Enter a temperature to convert ");
while (!userIn.hasNextDouble()) {
System.out.println("That is not a valid temperature!");
System.out.print("Re-enter a temperature: ");
userIn.next();
}
temp = userIn.nextDouble();

return temp;
}

private static String scaleOne() {

Scanner userIn = new Scanner(System.in);

// \n is a line break for readability
System.out.print("What scale is your temp currently in?"
+ "\n1: Fahrenheit"
+ "\n2: Celsius"
+ "\n3: Kelvin"
+ "\nPlease enter 1, 2, or 3: ");

// read a value from the user
int userChoice = userIn.nextInt();

String scale = "";

switch (userChoice) {
case 1: {
scale = "Fahrenheit";
break;
}

case 2: {
scale = "Celsius";
break;
}

case 3: {
scale = "Kelvin";
break;
}

default: {
scale = "There was an error in your choice!";
}
}

return scale;

}

private static String toScale() {

Scanner userIn = new Scanner(System.in);

System.out.print("What scale would you like to convert to?" + "1: Fahrenheit" + "2: Celsius" + "3: Kelvin"
+ "Please enter 1, 2, or 3: ");

String scale2 = "";

int userChoice = userIn.nextInt();

// ideally should not accept the same scale as scale1 (from scale)
switch (userChoice) {
case 1: {
scale2 = "Fahrenheit";
break;
}

case 2: {
scale2 = "Celsius";
break;
}

case 3: {
scale2 = "Kelvin";
break;
}

default: {
scale2 = "There was an error in your choice!";
}
}

return scale2;

}

public static double convert() {
// need only one variable for result
double converts;

// don’t call methods again, use variables
// don’t use == for comparing strings, use .equals
// typo: the second scale corrected to scale2
if (scale1.equals("Fahrenheit") && scale2.equals("Kelvin")) {
// don’t call getTemp() again
converts = (temp1 + 459.67) * 5 / 9;
}

else if (scale1.equals("Fahrenheit") && scale2.equals("Celsius")) {
converts = (temp1 - 32) * 5 / 9;
}

else if (scale1.equals("Celsius") && scale2.equals("Kelvin")) {
converts = temp1 + 273.15;
}

else if (scale1.equals("Celsius") && scale2.equals("Fahrenheit")) {
converts = temp1 * 9 / 5 + 32;
}

else if (scale1.equals("Kelvin") && scale2.equals("Fahrenheit")) {
converts = temp1 * 9 / 5 - 459.67;
}

else { // Kelvin to Celsius
converts = temp1 - 276.15;
}

return converts;
}

}

我相信它现在基本上可以工作了。

关于java - 我的 Java 方法运行不正确导致我的程序编译不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40093859/

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