我的Java方法运行不正确,导致程序编译错误
我正在编写一个程序,它将提示用户输入一个温度作为整数,然后提示用户选择该温度的刻度,可以是华氏度、摄氏度或开尔文。然后,它会提示用户从3个比例选择中选择要转换为的温度,然后计算转换并显示最终温度。我的程序目前正在运行,但运行不正确,我已经盯着它看了好几天了,我不知道它为什么会这样编译。我对编码还是相当陌生,这是一个课堂作业,我一直在寻找一些建议 封装温度转换器; 导入java.util.* 公共类临时转换器 { 下面是它编译的内容 运行: 输入要转换为100的温度 您当前的体温是多少度?1:Fahrenheit2:Celsius 3:Kelvin请输入1、2或3:您希望转换为多少度?1:Fahrenheit2:Celsius 3:Kelvin请输入1、2或3:您当前的体温是多少度?1:Fahrenheit2:Celsius 3:Kelvin请输入1、2或3:您当前的体温是多少度?1:Fahrenheit2:Celsius 3:KElvin请输入1、2或3:您当前的体温是什么刻度?1:华氏2:Celsius 3:Kelvin请输入1、2或3:您当前的体温是什么刻度?1:华氏2:Celsius 3:Kelvin请输入1、2或3:您当前的体温是什么刻度?1:华氏2:Celsius 3:Kelvin请输入1、2或3:输入温度以转换2 您希望转换为什么刻度?1:Fahrenheit2:Celsius3:Kelvin请输入1、2或3:您的最终温度为:-274.15您的选择有误!生成成功(总时间:33秒) 这就好像它没有按照正确的顺序编译,或者没有按照正确的顺序获取正确的信息,并且一次又一次地获取一些信息。一旦你输入数字温度,它会提示你输入刻度,但不仅仅是一次它打印了很多次,然后你输入你对第一个刻度的选择,它会一直显示到to完成,无需输入您的toScale选择我的Java方法运行不正确,导致程序编译错误,java,Java,我正在编写一个程序,它将提示用户输入一个温度作为整数,然后提示用户选择该温度的刻度,可以是华氏度、摄氏度或开尔文。然后,它会提示用户从3个比例选择中选择要转换为的温度,然后计算转换并显示最终温度。我的程序目前正在运行,但运行不正确,我已经盯着它看了好几天了,我不知道它为什么会这样编译。我对编码还是相当陌生,这是一个课堂作业,我一直在寻找一些建议 封装温度转换器; 导入java.util.* 公共类临时转换器 { 下面是它编译的内容 运行: 输入要转换为100的温度 您当前的体温是多少度?1:Fa
public static void main(String[] args)
{
String scaleOne;
String scaleTwo;
System.out.println("What scale is your temp currently in?\n"+
"1: Fahrenheit\n" +
"2: Celsius\n" +
"3: Kelvin\n" +
"Please enter 1, 2, or 3: \n");
scaleOne = getScale();
System.out.println("What scale would you like to convert to?\n"+
"1: Fahrenheit\n" +
"2: Celsius\n" +
"3: Kelvin\n" +
"Please enter 1, 2, or 3:\n ");
scaleTwo = getScale();
System.out.print("Your final temp is: " +convert(scaleOne,scaleTwo));
}
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.println("Re-enter a temperature: ");
userIn.next();
}
temp=userIn.nextDouble();
return temp;
}
private static String getScale()
{
Scanner userIn= new Scanner(System.in);
String scale="";
switch(userIn.nextInt())
{
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;
}
相应地更改main函数以及scaleone()和toScale()函数。您的代码中存在许多无法单独解决的问题。请随时询问有关答案的任何澄清
相应地更改main函数以及scaleone()和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;
}
}
我相信它现在基本上可以工作。请参阅下面代码中插入的注释和更正
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;
}
}
我相信它现在基本上可以工作。您必须相信编译器的工作是正确的。错误在于您的逻辑。我假设您正在尝试创建一个程序,将温度从3种格式中的任意一种转换为其他格式
scaleOne(scale1);
方法时,您提供了一个硬编码值0
作为参数,并且从未在方法中更改过它。同时,您尝试从用户获取输入。要获取用户输入,您需要在方法体中添加int-userChoice=userIn.nextInt();
。
这将返回一个sting值,您需要将其存储在main方法的sacle1
变量中toScale()
方法,您需要使其非参数化,并从方法内部的用户获取输入。
这将再次返回一个sting值,您需要将其存储在main方法的sacle2
变量中scale1
和scale2
作为参数传递。我不明白调用scaleOne()
和toScale()背后的逻辑是什么
在每个if条件下再次使用方法。这就是代码一再要求选择温度类型的原因。我已在解决方案中使用scale1
和scale2
变量删除了它们。
请始终记住,切勿使用==
运算符进行String
比较。请始终使用字符串类的equals()
或equalsEgnoreCase()
方法 public class TempConverter {
public static void main(String[] args)
{
String scale1;
String scale2;
//input
scale1 =scaleOne();
scale2 = toScale();
//process
System.out.print("Your final temp is: " +convert(scale1, scale2));
}
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);
System.out.print("What scale is your temp currently in?"+
"1: Fahrenheit" +
"2: Celsius" +
"3: Kelvin" +
"Please enter 1, 2, or 3: ");
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();
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(String scale1, String scale2)
{
double converts;
if(scale1.equalsIgnoreCase("Fahrenheit") && scale2.equalsIgnoreCase("Kelvin"))
{
converts=(getTemp()+459.67)* 5/9;
}
else if(scale1.equalsIgnoreCase("Fahrenheit") && scale2.equalsIgnoreCase("Celsius"))
{
converts=(getTemp()-32)*5/9;
}
else if(scale1.equalsIgnoreCase("Celsius") && scale2.equalsIgnoreCase("Kelvin"))
{
converts=getTemp()+273.15;
}
else if(scale1.equalsIgnoreCase("Celsius") && scale2.equalsIgnoreCase("Fahrenheit"))
{
converts=getTemp()*9/5+32;
}
else if(scale1.equalsIgnoreCase("Kelvin") && scale1.equalsIgnoreCase("Fahrenheit"))
{
converts=getTemp()*9/5-459.67;
}
else
{
converts=getTemp()-276.15;
}
return converts;
}}
您必须相信编译器的工作是正确的。错误在于您的逻辑。我假设您正在尝试创建一个程序,将温度从3种格式中的任意一种转换为其他格式
scaleOne(scale1);
方法时,您提供了一个硬编码值0
作为参数,并且从未在方法中更改过它。同时,您尝试从用户获取输入。要获取用户输入,您需要在方法体中添加int-userChoice=userIn.nextInt();
。
这将返回一个sting值,您需要将其存储在main方法的sacle1
变量中toScale()
方法,您需要使其非参数化,并从方法内部的用户获取输入。
这将再次返回一个sting值,您需要将其存储在main方法的sacle2
变量中scale1
和scale2
作为参数传递。我不明白调用scaleOne()
和toScale()背后的逻辑是什么
在每个if条件下再次使用方法。这就是代码一再要求选择温度类型的原因。我已在解决方案中使用scale1
和scale2
变量删除了它们。
请始终记住,切勿使用==
运算符进行字符串比较。请始终使用equals()
或equalsEgnoreCa
package tempconverter;
import java.util.Scanner;
public class TempConverter
{
static double temp1;
static String scale1;
static String scale2;
static double conv;
public static void main(String[] args)
{
char cont='N';
do
{ menueOne();
menueCatch();
cont=contOption();
}while(cont=='Y');
}
private static void menueOne()
{
System.out.print("Please make a selection."
+"\n1: Begin"
+"\n2: Exit"
+"\n");
}
private static void menueCatch()
{
Scanner userIn= new Scanner(System.in);
int userChoice=userIn.nextInt();
String input="";
switch(userChoice)
{
case 1:
{
temp1=getTemp();
scale1=scaleOne();
scale2=toScale();
conv=convert();
displayConversion();
break;
}
case 2:
{
break;
}
}
}
private static double getTemp()
{
double temp;
double currentTemp;
Scanner userIn= new Scanner(System.in);
System.out.print("Enter a temperature to convert as a digit(ex: 100, 32, 10, 0): ");
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);
System.out.print("What scale is your temp currently in?"+
"\n1: Fahrenheit" +
"\n2: Celsius" +
"\n3: Kelvin" +
"\nPlease enter 1, 2, or 3: ");
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?"+
"\n1: Fahrenheit" +
"\n2: Celsius" +
"\n3: Kelvin" +
"\nPlease enter 1, 2, or 3: ");
int userChoice = userIn.nextInt();
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 converts;
if (scale1.equals("Fahrenheit") && scale2.equals("Kelvin")) {
converts = (temp1 + 459.67) * (5.0 / 9.0);
}
else if (scale1.equals("Fahrenheit") && scale2.equals("Celsius")) {
converts = (temp1 - 32) * (5.0 / 9.0);
}
else if (scale1.equals("Celsius") && scale2.equals("Kelvin")) {
converts = temp1 + 273.15;
}
else if (scale1.equals("Celsius") && scale2.equals("Fahrenheit")) {
converts = temp1 * (9.0 / 5.0) + 32;
}
else if (scale1.equals("Kelvin") && scale2.equals("Fahrenheit")) {
converts = temp1 * (9.0 / 5.0) - 459.67;
}
else { // Kelvin to Celsius
converts = temp1 - 276.15;
}
return converts;
}
private static void displayConversion()
{
System.out.print("Your final temp is: " +conv+' '+scale2+' ');
}
private static char contOption()
{
char answer;
Scanner userIn=new Scanner(System.in);
System.out.print("\nDo you wish to convert another temp?(Y/N): ");
answer=userIn.next().toUpperCase().charAt(0);
return answer;
}
}