2016-03-08 71 views
0

我試圖重複打印出一定的打印量「轉換#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; 
       } 
      } 
+1

你印刷的要求和代碼,但沒有解釋 - 什麼是不工作?你不明白什麼?你的具體問題是什麼? –

+0

看起來像你的{}後您的for循環失蹤。 –

+0

它會問有多少轉換號碼的用戶,但是當我運行的代碼,它只是忽略轉換問題。 – mvanderk10

回答

-1

按我最瞭解你希望允許用戶選擇每次轉換的類型進入前溫度圖值

你的代碼看起來很好,但下面這行應改爲運行的變量轉換時間值的循環。

System.out.println("Conversion # " + i++); 

to 
System.out.println("Conversion # " + i+1); 


also 

for(int i = 0; i < conversions; i ++);應該更改爲 (int i = 0; i <轉換; i + 1){ 並在 之後加上右括​​號(「Not a valid choice,try agiain」);

以下是完美的代碼

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

,當我將其更改爲,它說,在我 – mvanderk10

+0

無法找到符號,並指出這是合乎邏輯的,即使它可能會給你編譯錯誤。把我的println內++聲明將增加i的值兩次,每次循環。 –

+0

當我運行程序時,它會把我放入轉換的數字而不是轉換#1 ....它輸出3. – mvanderk10