2017-03-10 6 views
1

私はこの問題を解決するために奇妙なバグを抱えているようです... .nextLine();を使用して文字列を格納しようとしています。新しい行が作成され、2行目が保存されます。スキャナが正しく文字列を読み取っていない

マイコード: パブリッククラス平均値{

public static void main (String args[]) { 

    String studentName = ""; 
    int score1, score2, score3; 
    float Average = 0f; 
    int numberTests = 0; 
    Scanner scr = new Scanner(System.in); 

    System.out.println("Enter students name"); 
    if(scr.nextLine().equalsIgnoreCase("quit")) { 
     System.exit(0); 
    } else { 
     studentName = scr.next(); 
    } 

    System.out.println("Enter the amount of tests"); 
    numberTests = scr.nextInt(); 
    System.out.println("Enter students scores"); 
    score1 = scr.nextInt(); 
    score2 = scr.nextInt(); 
    score3 = scr.nextInt(); 

    Average = (score1 + score2 + score3)/numberTests; 
    System.out.print(studentName + " " + Average); 
}} 

エラーが保存されていない最初の入力であるが、別の行に移動し、その代わりに保存されます。走っされた場合、これは出力されます。

は、学生を入力し

がテスト

の量を入力し

オーバーフローが

学生のスコアを入力し

スタックに名前を付けます1

私は数学が間違っている理解が、私は誰もが私が間違ってやっているものを私に伝えることができる場合、私は大いにだろう最初の名前の部分を把握する必要がありオーバーフロー2.0

感謝します。

+0

(http://stackoverflow.com/questions/13102045/scanner-is-skipping [スキャナ)は(次、nextInt()または他のnextFoo()メソッドを使用した後nextLine()はスキップされる]を見てください-nextline-after-using-next-nextint-other-nextfoo) –

+0

私はそれを読んだが、理解しているが、nextIntを最初にキャプチャしていないので、 –

答えて

0

公共の静的な無効メイン(文字列引数[])が {

String studentName = ""; 
    int score1, score2, score3; 
    float Average = 0f; 
    int numberTests = 0; 
    Scanner scr = new Scanner(System.in); 

    System.out.println("Enter students name"); 
    String name = scr.nextLine(); 
    if (name.equalsIgnoreCase("quit")) 
    { 
     System.exit(0); 
    } 

    System.out.println("Enter the amount of tests"); 
    numberTests = scr.nextInt(); 
    System.out.println("Enter students scores"); 
    score1 = scr.nextInt(); 
    score2 = scr.nextInt(); 
    score3 = scr.nextInt(); 

    Average = (score1 + score2 + score3)/numberTests; 
    System.out.print(studentName + " " + Average); 
} 

希望Uは、それがstudentNameにユーザ入力を割り当てていなかったので

+0

*うまくできます*多分OPが得られますが、このタイプの答えは初心者やこのコードに慣れていない人にとってはあまり役に立ちません。あなたがしたことを述べてください。 –

+0

@ScaryWombatさて、ここで説明します。java.util.Scanner.nextLine()メソッドは、このスキャナを現在の行よりも先に進め、スキップされた入力を返します。このメソッドは、最後の行区切り文字を除いて、現在の行の残りの部分を返します。位置は次の行の先頭に設定されます。したがって、Scanner.next()は次のエントリを待つでしょう。これが最初の文字列をスキップする理由です – vineeth

1

あなたの最初の名前の文が消費後(ただし、保存されません):あなたがしたい場合は

if(scr.nextLine().equalsIgnoreCase("quit"))

studentName変数に保存されます2番目の名前が

studentName = scr.next();

です最初の名前も保存してください。最初に変数に格納する必要があります。

+1

唯一のまともな答えはこちら –

+0

@ScaryWombat、あなたの励ましの言葉に感謝します。私にSOに貢献するよう動機づけられます。:) – VHS

0

エラーscr.nextLine().equalsIgnoreCase("quit")であり得ます。

以下のコードを使用して確認してください。

public static void main (String args[]) { 

    String studentName = ""; 
    int score1, score2, score3; 
    float Average = 0f; 
    int numberTests = 0; 
    Scanner scr = new Scanner(System.in); 

    System.out.println("Enter students name"); 
    studentName=scr.nextLine(); 
    if(studentName.equalsIgnoreCase("quit")) { 
     System.exit(0); 
    } 

    System.out.println("Enter the amount of tests"); 
    numberTests = scr.nextInt(); 
    System.out.println("Enter students scores"); 
    score1 = scr.nextInt(); 
    score2 = scr.nextInt(); 
    score3 = scr.nextInt(); 

    Average = (score1 + score2 + score3)/numberTests; 
    System.out.print(studentName + " " + Average); 
}} 
関連する問題