2014-11-17 150 views
-2

我需要填寫我的陣列使用Scanner和10個名稱的循環,我不知道如何。 這是我有個大氣壓:陣列掃描儀for循環

import java.util.Scanner; 

public class qqqqq { 
public static void main (String[] args) { 
    Scanner input = new Scanner(System.in); 
    String[] my_friend_names = {"Foo", "Bar", "Baz"}; 

    for (int i = 0; i < my_friend_names.length; i++) { 
     System.out.println(my_friend_names[i]); 
    } 
} 
} 
+0

您應該從掃描儀獲取輸入並設置陣列位置...... – brso05

+0

指定要添加的多少個朋友for(int i = 0;我<「多少」; i ++)並在for循環中嘗試my_friend_names [i] = input.nextLine(); – pMpC

回答

1
public static void main (String[] args) 

{ 
    Scanner input = new Scanner(System.in); 
    String[] my_friend_names = new String[]; 

    for (int i = 0; i < my_friend_names.length; i++) 
    { 
     my_friend_names[i] = input.nextLine(); 
    } 
    for(int i = 0; i < my_friend_names.length; i++) 
    { 
     System.out.println("Name: " + my_friend_names[i]); 
    } 
} 

你應該使用掃描儀使用nextLine(),將獲得下一行獲取用戶輸入...

0
for (int i = 0; i < 10; i++) 
    my_friend_names[i] = input.nextLine(); 
0

您需要製作尺寸爲10String類型的數組。

String[] my_friend_names = new String[10]; 

你需要做一個for循環執行10次:

for(int i=0; i<10; i++) { 

在這種循環中,您必須得到輸入

String name=input.nextLine(); 

,你需要這個名字添加到陣列。

my_friend_names[i]=name; 

不要忘記關閉for循環這裏

} 

最後但並非最不重要的,以防止垃圾

input.close(); 

讓我知道它是否有效與否
快樂編碼:) -Charlie