2017-12-10 8 views
0
package assign; 

import java.util.Scanner; 

public class File { 
public static void main(String[] args) { 
    Scanner s = new Scanner(System.in); 
    System.out.println("How many chef do we have?"); 
    int x = s.nextInt(); 
    String [] names = new String[x]; 
    if(x <= 0) { 
     do { 
     System.out.println("Please enter a valid number:"); 
     x = s.nextInt(); 
     }while (x <= 0); 
     } 

    for (int m = 0; m <= x-1; m++) { 
     System.out.println("Enter the name of " + m + ":"); 
     String f = s.nextLine(); 
     names [m] = f; 
    } 
    } 
} 

私の目的は、ユーザーの希望に応じてユーザーの名前を取得することです。しかし、このコードから、私の出力は次のようなものです:複数の入力を入力して保存する

How many chef do we have? 
    3 
    Enter the name of 0: 
    Enter the name of 1: 
    John 
    Enter the name of 2: 
    Jack 

私は質問が注文に来int f = s.nextInt();String f = s.nextLine();ラインを変更し、私は数字を書くことができる午前とき。

How many chef do we have? 
    3 
    Enter the name of 0: 
    Bob 
    Enter the name of 1: 
    John 
    Enter the name of 2: 
    Jack 

答えて

0

利用String f = s.next();代わりのString f = s.nextLine();

:私はこのようにそれを作るために何をすべき
関連する問題