Java多参数方法问题帮助我完成
我正在尝试完成以下代码。我对多参数方法有问题。感谢您的帮助Java多参数方法问题帮助我完成,java,methods,Java,Methods,我正在尝试完成以下代码。我对多参数方法有问题。感谢您的帮助 import java.util.Scanner; public class Temp { public static void main(String[] args) { Scanner input = new Scanner(System.in); //Creates the scanner for user input System.out.println ("Temperatu
import java.util.Scanner;
public class Temp {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); //Creates the scanner for user input
System.out.println ("Temperature converter");
System.out.println ("Please make a selection using 1 & 2");
System.out.println ("1 for celsius to fahrenheit");
System.out.println ("2 for fahrenheit to celsius");
int choice=input.nextInt();
if (choice < 2 || choice > 1 )
System.out.println ("Error your choice is not valid please select option 1 or 2");
choice=input.nextInt();
System.out.println ("Enter temperature");
double temp=input.nextDouble();
double result=tempChanger(choice,temp);
if (choice = 1)
System.out.println ("The conversion of "+temp+" from celcius to fahrenheit is "+result );
else
System.out.println ("The conversion of "+temp+" from fahrenheit to celcius is "+result );
public static double tempChanger(int choice, double temp)
{
int choice;
if (choice = 1)
double converted=9.0/5.0*temp+32;
else
double converted=5.0/9.0*(temp -32);
Return converted;
}
}
}
import java.util.Scanner;
公共类临时工{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);//为用户输入创建扫描仪
System.out.println(“温度转换器”);
System.out.println(“请使用1和2进行选择”);
System.out.println(“1表示摄氏到华氏度”);
System.out.println(“2表示华氏到摄氏度”);
int choice=input.nextInt();
如果(选项<2 | |选项>1)
System.out.println(“错误您的选择无效,请选择选项1或2”);
choice=input.nextInt();
System.out.println(“输入温度”);
double temp=input.nextDouble();
双结果=温度变换器(选择,温度);
如果(选项=1)
System.out.println(“从celcius到华氏度的“+temp+”转换为“+result”);
其他的
System.out.println(“从华氏到摄氏的“+temp+”转换为“+result”);
公共静态双温度转换器(整数选择,双温度)
{
智力选择;
如果(选项=1)
双转换=9.0/5.0*温度+32;
其他的
双转换=5.0/9.0*(温度-32);
转换收益;
}
}
}
将此方法置于主方法之外。还请注意,我对您的方法做了一些更改。你做了几件错事
1) if (choice = 1) should be if (choice == 1)
2) Return should be return
3) have written a method in main method.
4) no need to define int choice; again in tempChanger method
校验校正法
public static double tempChanger(int choice, double temp)
{
double converted=0.0;
if (choice == 1)
converted=9.0/5.0*temp+32;
else
converted=5.0/9.0*(temp -32);
return converted;
}
查找已完成的编译版本
import java.util.Scanner;
public class Temp {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); //Creates the scanner for user input
System.out.println ("Temperature converter");
System.out.println ("Please make a selection using 1 & 2");
System.out.println ("1 for celsius to fahrenheit");
System.out.println ("2 for fahrenheit to celsius");
int choice=input.nextInt();
if (choice < 2 || choice > 1 )
System.out.println ("Error your choice is not valid please select option 1 or 2");
choice=input.nextInt();
System.out.println ("Enter temperature");
double temp=input.nextDouble();
double result=tempChanger(choice,temp);
if (choice == 1)
System.out.println ("The conversion of "+temp+" from celcius to fahrenheit is "+result );
else
System.out.println ("The conversion of "+temp+" from fahrenheit to celcius is "+result );
}
public static double tempChanger(int choice, double temp)
{
double converted=0.0;
if (choice == 1)
converted=9.0/5.0*temp+32;
else
converted=5.0/9.0*(temp -32);
return converted;
}
}
import java.util.Scanner;
公共类临时工{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);//为用户输入创建扫描仪
System.out.println(“温度转换器”);
System.out.println(“请使用1和2进行选择”);
System.out.println(“1表示摄氏到华氏度”);
System.out.println(“2表示华氏到摄氏度”);
int choice=input.nextInt();
如果(选项<2 | |选项>1)
System.out.println(“错误您的选择无效,请选择选项1或2”);
choice=input.nextInt();
System.out.println(“输入温度”);
double temp=input.nextDouble();
双结果=温度变换器(选择,温度);
如果(选项==1)
System.out.println(“从celcius到华氏度的“+temp+”转换为“+result”);
其他的
System.out.println(“从华氏到摄氏的“+temp+”转换为“+result”);
}
公共静态双温度转换器(整数选择,双温度)
{
双转换=0.0;
如果(选项==1)
转换为9.0/5.0*温度+32;
其他的
转换为5.0/9.0*(温度-32);
转换收益;
}
}
您遇到了哪些问题?除了在main中包含一个方法之外。