2016-03-08 65 views
0

我第一次使用方法,我的程序主要工作,除了它不会循环转换语句与它对应的问题...用户输入符合转换语句,所以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; 
      } 

      } 
+1

[Coudln't重现](http://melpon.org/wandbox/permlink/aTkwtJ8QD59Vvfnu):convertsion从摄氏度到farenheight似乎运作良好。 – MikeCAT

+0

是的你的权利,我发布我的代码之前我改变了一些东西! – mvanderk10

回答

0

循环代码错误,并且开关case2没有break。 在这里找到纠正代码:

import java.text.DecimalFormat; 
import java.util.Scanner; 

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)); 
       break; 
      } 
     } 
    } 

    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; 
    } 

} 
+0

在案例2中没有'break'并不是一个错误。总是使用'​​break'是一种很好的形式,但它不会影响程序。 – ajb

1

你有两个错误:

(1)此行是错误的:

for(int i = 1; i < conversions; i++) 

这意味着要循环长达i < conversions。如果conversions为3,则表示它将循环使用i==1i==2,但它不会循环使用i==3,因为3<3false。使用<=

(2)上述for语句中的i++每次循环时都会增加i。这是你想要的。但是,您通过在代码中添加另一个i++来击败它,这会增加一个额外的时间。

+0

所以我应该把一个没有++的输出语句放在for循环之下? – mvanderk10

+0

试试看看。 – ajb