2016-12-27 12 views
1

これは、JavaJavaのコンソールから文字列入力を取る方法は?

import java.util.Scanner; 
class calc 
{ 
public static void main(String[] args) 
{ 
    boolean go=true; 
    Scanner input=new Scanner(System.in); 
    while(go) 
    { 
     int num1; 
     int num2; 
     int total; 
     int choice; 

     System.out.println("\nHi This is Console type Calculator"); 
     System.out.println("1. Addition"); 
     System.out.println("2. Substraction"); 
     System.out.println("3. Multiply"); 
     System.out.println("4. Divisoin"); 
     System.out.print("Enter Your Choice : "); 
     choice=input.nextInt(); 

     switch(choice) 
     { 

      case 1: 
       System.out.println("Enter First Number"); 
       num1=input.nextInt(); 
       System.out.println("Enter Second Number"); 
       num2=input.nextInt(); 
       total=num1+num2; 
       System.out.println("Addition of "+num1+" and "+num2+" are "+total); 
       break; 
      case 2: 
       System.out.println("Enter First Number"); 
       num1=input.nextInt(); 
       System.out.println("Enter Second Number"); 
       num2=input.nextInt(); 
       total=num1-num2; 
       System.out.println("Substraction of "+num1+" and "+num2+" are "+total); 
       break; 
      case 3: 
       System.out.println("Enter First Number"); 
       num1=input.nextInt(); 
       System.out.println("Enter Second Number"); 
       num2=input.nextInt(); 
       total=num1*num2; 
       System.out.println("Multiplication of "+num1+" and "+num2+" are "+total); 
       break; 
      case 4: 
       System.out.println("Enter First Number"); 
       num1=input.nextInt(); 
       System.out.println("Enter Second Number"); 
       num2=input.nextInt(); 
       total=num1/num2; 
       System.out.println("Divistion of "+num1+" and "+num2+" are "+total); 
       break; 
      default: 
       System.out.println("Please Choose right option...Try again"); 
       break; 
     } 

     System.out.println("Do You Want more Calculation...Yes/No"); 
     String str=input.nextLine(); 
     System.out.println("Do You Want more Calculation...Yes/No"); 
     String str1=input.nextLine(); 

     if("no".equals(str1)) 
     { 
      go=false; 
      System.out.println("Thanks For using...Bye"); 
     } 
    } 
} 
} 

で私のコードであると私は入力を取るように、コードの以下の部分で問題を抱えています。これは、ユーザーからの入力を受け付けていないため、この部分をスキップします。このコードに問題はありますか?

 System.out.println("Do You Want more Calculation...Yes/No"); 
     String str1=input.nextLine(); 

     if("no".equals(str1)) 
     { 
      go=false; 
      System.out.println("Thanks For using...Bye"); 
     } 
+0

str1でどのような価値が得られますか? – GurV

+0

[Yoda条件](https://en.wikipedia.org/wiki/Yoda_conditions)を参照してください。 – Guy

+0

@ GurwinderSingh str1には入力がありません。 – Mathur

答えて

-1

あなたの問題は、あなたがあなたのケースでnextInt()を使用していた保存されたので、あなたの文字列では、新しいラインです。

System.out.println("Do You Want more Calculation...Yes/No"); 
    String str1=input.nextLine(); 

    if("no".equals(str1)) 
    { 
     go=false; 
     System.out.println("Thanks For using...Bye"); 
    } 

をによって:

System.out.println("Do You Want more Calculation...Yes/No"); 
    String str=input.nextLine(); 

説明:input.nextLine();

System.out.println("Do You Want more Calculation...Yes/No"); 
    input.nextLine(); // this is what I added 
    String str1=input.nextLine(); 

    if("no".equals(str1)) 
    { 
     go=false; 
     System.out.println("Thanks For using...Bye"); 
    } 

これらの2行は有用な目的を果たしていないとして、あなたは以下を削除することができますあなたがしなければならない置き換えるです私は基本的にあなたがEnterまたはReturnキーを押すと、改行文字を取得します。次に、input.nextLine()に再度電話し、str1に割り当てて、使用する文字列を取得する必要があります。 また、プログラム上の小さなメモ:ユーザーがNoを入力した場合、プログラムは値がYesとみなします。これは、小文字でない場合にのみfalseになります。その解決策は、str1.toLowerCase()を呼び出すことによって、受信した文字列を小文字に変換することです。また、ユーザーがdogを入力する可能性があるため、プログラムはそれをyesと見なすので、明示的なyesの答えをチェックすることもできます。

+1

'if(str1.equalsIgnoreCase(" no ")){}'はどうでしょうか? –

+0

@YoungMillieは特に利点はありません。同じ計算コスト。しかし、+1の提案。 – Ralph

+0

@YoungMillieそうではありません。 toLowerCaseはすべてのASCII文字をlowerCaseに変換しますが、equalsIgnoreCaseはコンパイラレベルでも同じ数の比較を正確に行います。 - >コンパイラ設計コースが完了しました。 – Ralph

3

問題は、先にnextIntを選択したことが原因です。ユーザは、数字と改行文字を入力する。番号を選択すると、改行文字がバッファリングされたままになります。 nextLine()を実行すると、指されているものと次のEOLの間のすべての文字が読み込まれます。それは、バッファ内の空の文字列を\nの前に読み込み、もう1つのnextLine()は、プログラムが入力を待つ必要があります。

String str = input.nextLine()の後にブレークポイントを置くと、実際には空であることがわかりますString""

enter image description here

ので、代わりの:

System.out.println("Do You Want more Calculation...Yes/No"); 
    String str=input.nextLine(); 
    System.out.println("Do You Want more Calculation...Yes/No"); 
    String str1=input.nextLine(); 

あなたが書く必要があります:

input.nextLine(); 
    System.out.println("Do You Want more Calculation...Yes/No"); 
    String str1=input.nextLine(); 
+2

これはスキャナの基本的な誤りです。すべての良いチュートリアルで説明する必要があります... – AxelH

+0

なぜ、 'input.next()'を使用しないのですか?私は、次の行ではなく次の完全なトークンを探していると思います。 – matt

+0

@matt maybe ...それは事実ではない – xenteros

関連する問題