2016-11-11 109 views
-4

你能告訴我如何解決這個問題嗎?在java中打印名字?

Scanner scanner = new Scanner(System.in);

package Exercise2501; 

    import java.util.Scanner; 

    public class Friends 
    { 

     public static void main(String[] args) 
     { 

      Scanner scanner = new Scanner(System.in); 
      System.out.println("How many names do you want to enter?"); 
      int number = scanner.nextInt(); 
      for (int i = 0; i < number; i++) 
      { 
      System.out.println("Type a few names. "); 
      String names = scanner.nextLine(); 
      } 
      String names = scanner.nextLine(); 
      System.out.println(names); 


     } 

    } 
+0

什麼ürequire..Your問題不是clear.can你給詳細 –

+0

當我嘗試運行它要求我的代碼做我想做多少名打印,然後就打印「鍵入幾個名字「,多次 – 3au0

+0

在循環前移動行 – Venkat

回答

0
package Exercise2501; 
    import java.util.Scanner; 

    public class Friends { 

    public static void main(String[] args) { 

     Scanner scanner = new Scanner(System.in); 
     //Get the number of entries 
     System.out.println("How many names do you want to enter?"); 
     int number = scanner.nextInt(); 
     //create an array with that number 
     String names[] = new names[number]; 
     //Ask the user to enter the Names 
     System.out.println("Type a few names. "); 
     //Store the names in the array 
     for (int i = 0; i < number; i++) 
     { 
      names[i] = scanner.nextLine(); 
     } 
     //Do the remaining you want 
    } 

    }