如何在Java上使用for循环显示数组的输出

时间:2013-10-20 08:58:13

标签: java

String [] name = {"IIman bt Zulkifli", "Ismaalif b Ismail", "Katherine Kong", "M.Mirunaaleeni", "Khalish b Ismail", "Younis b Ahmad", "Syifa Hani bt Shahizam", "Atiyyah bt Fahmi", "Furqan b Hafizi", "Qarnur Asraf b Ali"};

    for(int no = 1; no<=10; no ++)
    System.out.println(no + ".");

    for(int i = 0; i < name.length; i++)
    System.out.println(name[i] + " ");

这是输出的一个例子:

1

2

3

4

5

6

7

8

9

10

IIman bt Zulkifli

Ismaalif b Ismail

凯瑟琳孔

M.Mirunaaleeni

Khalish b Ismail

Younis b Ahmad

Syifa Hani bt Shahizam

Atiyyah bt Fahmi

Furqan b Hafizi

Qarnur Asraf b Ali

我如何在数字旁边显示这些名字?

3 个答案:

答案 0 :(得分:2)

试试这种方式

for(int i = 0; i < name.length; i++)
System.out.println((i+1) +". "+ name[i] );

<强>更新

注意:请确保名称和标记数组长度相等

for(int i = 0; i < name.length; i++)
    System.out.println((i+1) +". "+ name[i] +" : " +marks[i] );

答案 1 :(得分:1)

 for(int i = 0; i < name.length; i++)
    System.out.println( (i+1) + "." + name[i] );

应该

答案 2 :(得分:1)

for(int i = 0; i < name.length; i++) {
    System.out.printf("\d: %s\n", i+1, name[i]);
}