2016-11-11 108 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 
    } 

    }