2017-04-08 7 views
1

メソッドに引数を渡そうとしていますが、 showKilometer(int num){メソッドすでに引数が渡されていますが、if-else if文が機能しない場合、私はshowKilometer()を変更しようとしています。 showキロメータ(int num); if-else if文の下でどちらでも動作しないようです。メソッドに引数を渡して、if-else if文を使用して特定のメソッドにユーザーを誘導しようとしました

import java.util.Scanner; 

public class a6main { 

public static void main(String[] args){ 

    double distanceMeter; 
    double distanceKilom; 
    double distanceInches; 
    double distanceFeet; 

    System.out.println("Enter a distance in meters:"); 
    Scanner keyboard = new Scanner(System.in); 
    distanceMeter = keyboard.nextDouble(); 

    while (distanceMeter <= 0){ 
    System.out.println("Unable to atain distance less than 0,\n" 
      + "please enter a number grater than 0:\n"); 
    distanceMeter = keyboard.nextInt(); 
    } 
    showKilometer(distanceMeter); 
    menu(); 
    } 

public static void menu(){ 
Scanner keyboard = new Scanner(System.in); 
System.out.println("Enter your choice\n" 
     + "1. Convert to kilometers\n" 
     + "2. Convert to inches\n" 
     + "3. Conveert to feet\n" 
     + "4. Quit the program"); 
    int choice = keyboard.nextInt(); 
    while (choice <= 0 || choice >= 5){ 
System.out.println("Option unavailble, pleace select a choice from 1 - 4:\n" 
     + "1. Convert to kilometers\n" 
     + "2. Convert to inches\n" 
     + "3. Conveert to feet\n" 
     + "4. Quit the program"); 
    choice = keyboard.nextInt(); 
    } 
if(choice == 1) 
    showKilometer();  
else if(choice == 2) 
    showInches(); 
else if(choice == 3) 
    showFeet(); 
else 
    System.out.println("Program terminated."); 
    } 

    public static void showKilometer(int num){ 
    System.out.println(num); 
    } 
    } 
+0

showKilometer(1) –

+0

@ user7790438のようにshowKilometerを呼び出すと何が起こるかを知ることはできません。 –

+0

と宣言されているので、1(1)は1.0として出力されます。むしろ –

答えて

0

引数を指定しないで新しいメソッドを定義するとします。

public static void showKilometer(){ 
    System.out.println("#### no args ####"); 
} 
1

コードは次のようになります。

import java.util.Scanner; 

public class A6Main { 

public static void main(String[] args){ 

    double distanceMeter; 
    double distanceKilom; 
    double distanceInches; 
    double distanceFeet; 

    System.out.println("Enter a distance in meters:"); 
    Scanner keyboard = new Scanner(System.in); 
    distanceMeter = keyboard.nextDouble(); 

    while (distanceMeter <= 0){ 
     System.out.println("Unable to atain distance less than 0,\n" 
       + "please enter a number grater than 0:\n"); 
     distanceMeter = keyboard.nextInt(); 
    } 

    // showKilometer(distanceMeter); 
    menu(distanceMeter); 
    } 

    public static void menu(double distance){ 
     Scanner keyboard = new Scanner(System.in); 
     System.out.println("Enter your choice\n" 
       + "1. Convert to kilometers\n" 
       + "2. Convert to inches\n" 
       + "3. Conveert to feet\n" 
       + "4. Quit the program"); 
     int choice = keyboard.nextInt(); 
      while (choice <= 0 || choice >= 5){ 
     System.out.println("Option unavailble, pleace select a choice from 1 - 4:\n" 
       + "1. Convert to kilometers\n" 
       + "2. Convert to inches\n" 
       + "3. Conveert to feet\n" 
       + "4. Quit the program"); 
     choice = keyboard.nextInt(); 
     } 
     if(choice == 1) 
     showKilometer(distance);  
     else if(choice == 2) 
     showInches(distance); 
     else if(choice == 3) 
     showFeet(distance); 
     else 
     System.out.println("Program terminated."); 
    } 

    public static void showKilometer(double num){ 
     System.out.println(num); 
    } 
    public static void showInches(double num){ 
     System.out.println(num); 
    } 
    public static void showFeet(double num){ 
     System.out.println(num); 
    } 
} 

あなたは、私は、変換を行うための追加のメソッドを追加しましたし、私はメニューに距離を渡す表示されます。次に、その距離を使用して、計算を実行できる変換メソッドに渡します。

希望はあなたを助けます。

+0

はい、ソリューションは正しいようです。 ) –

関連する問題