我试图重复打印出一定数量的打印件"转换#1" 但是根据用户输入上升,所以如果用户输入了他们想要3个不同的转换,那么它将是:
转换#1
代码。
转换#2
...代码
转换#3
...代码
这是我的代码:
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("---------------------");
Scanner input = new Scanner(System.in);
//this is the conversion promt
System.out.print("How many conversions would you like to make: ");
int conversions=input.nextInt();
for(int i = 0; i < conversions; i++);
System.out.println("Conversion # " + i++);
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();
System.out.print ("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 "+df.format(result) );
else if (choice == 2)
System.out.println ("The conversion of "+temp+" from fahrenheit to celcius is "+df.format(result) );
else
System.out.println ("Not a valid choice, try agiain");
}
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;
}
}
答案 0 :(得分:-1)
根据我的最佳理解,您希望允许用户在输入温度值之前每次选择转换类型
您的代码看起来很好但是应该更改以下行以运行循环以获取可变转换时间的值。
System.out.println("Conversion # " + i++);
to
System.out.println("Conversion # " + i+1);
also
for(int i = 0; i&lt; conversions; i ++);应改为 (int i = 0; i&lt; conversions; i + 1){ 并在之后放置关闭括号 System.out.println(&#34;不是一个有效的选择,试试agiain&#34;);
以下是完美代码
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("---------------------");
Scanner input = new Scanner(System.in);
//this is the conversion promt
System.out.print("How many conversions would you like to make: ");
int conversions=input.nextInt();
for(int i = 0; i < conversions; i++){
System.out.println("Conversion # " + i++);
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();
System.out.print ("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 "+df.format(result) );
else if (choice == 2)
System.out.println ("The conversion of "+temp+" from fahrenheit to celcius is "+df.format(result) );
else
System.out.println ("Not a valid choice, try agiain");
}
}
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;
}
}