2016-08-08 5 views
-3

私は初心者のプログラマですが、これは修正するのが非常に簡単な質問のように思えますが、2時間程度見渡してもかなり詰まっています。私のコードは下に貼り付けられ、唯一の問題はスキャナクラスが入力を要求していないことです。 (ガロンリットルまたはリットルにガロンを使用することを決定するためには)誰かがこれで私を助け場合、私は本当に感謝:Dスキャナクラスを正しく使用していないJava

import java.util.Scanner; 
public class GallonsToLiters { 

public static void main(String[] args) { 
    System.out.println("Do you want liters or gallons? Enter a 10 for gallons to liters, and anything else for liters to gallons."); 

    Scanner test = new Scanner(System.in); 


    if (test.equals (10)){ 
     double gallons, liters; 
      Scanner console = new Scanner(System.in); 

      System.out.println("Enter an amount of gallons --> "); 
      gallons = console.nextDouble(); 
      liters = gallons * 3.785; 
      // "%.2f" means that it rounds to 2 dec places. If you change #, that is how many decimal places it rounds to 
      System.out.printf("Amount in liters " + "%.2f",liters); 
    } 
    else{ 
      double liters01, gallons01; 
      Scanner console01 = new Scanner(System.in); 

      System.out.println("Enter an amount of liters --> "); 
      liters01 = console01.nextDouble(); 
      gallons01 = liters01/3.785; 
      // "%.2f" means that it rounds to 2 dec places. If you change #, that is how many decimal places it rounds to 
      System.out.printf("Amount in gallons (Rounded) " + "%.2f",gallons01); 




      } 
+0

'Scanner'クラスは入力を求めません。なぜあなたは複数の「スキャナ」を構築していますか? –

+0

最初に複数のScannerオブジェクトを作成する必要はありません。また、Scannerオブジェクトの 'tess'が' 10'と等しくなると思いますか? –

+3

[JavaのScannerクラスを使用してコンソールから入力を読み取るにはどうすればできますか?](http://stackoverflow.com/questions/11871520/how-can-i-read-input-from-the-コンソール使用のスキャナクラスのJava) –

答えて

-1

ガロンのためのさまざまな変数を宣言していない、一度だけスキャナ多くのオブジェクトを宣言する必要はありませんし、各条件構造のリットル

Scanner reader = new Scanner(System.in); 
double gallons, liters; 
int op; 
System.out.println("Do you want liters or gallons? Enter a 10 for gallons to liters, and anything else for liters to gallons."); 

op = reader.nextInt(); 

if (op==10){ 

     System.out.println("Enter an amount of gallons --> "); 
     gallons = reader.nextDouble(); 
     liters = gallons * 3.785; 
     // "%.2f" means that it rounds to 2 dec places. If you change #, that is how many decimal places it rounds to 
     System.out.printf("Amount in liters " + "%.2f",liters); 
} 
else{ 


     System.out.println("Enter an amount of liters --> "); 
     liters = reader.nextDouble(); 
     gallons = liters/3.785; 
     // "%.2f" means that it rounds to 2 dec places. If you change #, that is how many decimal places it rounds to 
     System.out.printf("Amount in gallons (Rounded) " + "%.2f",gallons); 
     } 
} 
関連する問題