我第一次使用方法,我的程序主要工作,除了它不会循环转换语句与它对应的问题...用户输入符合转换语句,所以3次将提示第一次使用温度转换器的方法
转换#1
...
转换#2
...
转换#3
...
也farenheight之间的转换到摄氏温度的作品而不是从摄氏度到farenheight,任何有识之士将低于有益的是我的代码,
import java.util.Scanner;
import java.text.DecimalFormat;
public class TempConverter {
public static void main(String[] args){
DecimalFormat df = new DecimalFormat("#,###.0");
System.out.println("Temperature Converter");
System.out.println("---------------------");
System.out.println();
Scanner input = new Scanner(System.in);
System.out.print("How many conversions would you like to make: ");
int conversions=input.nextInt();
for(int i = 1; i < conversions; i++){
System.out.println("Conversion # " + i++);
System.out.println();
System.out.println ("To convert from celsius to fahrenheit type 1 ");
System.out.print ("To convert from fahrenheit to celsius type 2: ");
int choice=input.nextInt();
switch(choice){
case 1:
System.out.println();
System.out.print ("Enter a celsius temperature: ");
double cels=input.nextDouble();
double result=celsiusToFahrenheit(choice,cels);
System.out.println();
System.out.println ("The conversion of "+cels+" from celcius to fahrenheit is "+df.format(result));
break;
case 2:
System.out.println();
System.out.print("Enter a farenheight temperature: ");
double fahr=input.nextDouble();
double result1=fahrenheitToCelsius(choice,fahr);
System.out.println ("The conversion of "+fahr+" from fahrenheit to celcius is "+df.format(result1));
}
}
}
public static double celsiusToFahrenheit(int choice, double cels)
{
double converted=0.0;
if (choice == 1)
converted=9.0/5.0*cels+32;
return converted;
}
public static double fahrenheitToCelsius(int choice, double fahr)
{
double converted2=0.0;
if (choice ==2)
converted2=5.0/9.0*(fahr-32);
return converted2;
}
}
[Coudln't重现](http://melpon.org/wandbox/permlink/aTkwtJ8QD59Vvfnu):convertsion从摄氏度到farenheight似乎运作良好。 – MikeCAT
是的你的权利,我发布我的代码之前我改变了一些东西! – mvanderk10