在列表末尾打印值

时间:2018-11-06 02:23:14

标签: java

我坚持做家庭作业,我希望有人能指出我正确的方向。我创建了一个用户名和密码数组,但说明是

”使用传统的“ For”循环遍历数据,然后将信息打印到屏幕上。 搜索史蒂夫·罗杰斯(Steve Rogers),然后在列表末尾再次打印他的信息。”

我能够使代码将列表打印到屏幕上,但是我不知道如何使它最后打印一个值。有人可以指导我如何实现这一目标吗?下面列出的代码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

/**
 *
 * @author Administrator
 */
public class JavaApplication1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String names[] = {"Admin","Vale.Vicky","Lane.Lois","Kent.Clark","Wayne.Bruce","Parker.Peter","Rogers.Steve","Luther.Lex","Osborn.Harry","Prince.Diana","Linda.Zoel"};
        String password[] = {"Password1","ZZZZZZZZ","VVVVVVVV","AAAAAAAA","FFFFFFFF","RRRRRRRR","QQQQQQQQ","GGGGGGGG","YYYYYYYY","LLLLLLLL","PPPPPPPP"};
        for (int i = 0; i < names.length; i++)
            System.out.println(names[i]+ "  "+password[i]);
    }

}

1 个答案:

答案 0 :(得分:0)

遍历数组时,寻找Rogers.Steve并找到存储索引的地方。

int index = -1;
for (int i = 0; i < names.length; i++) {
        System.out.println(names[i] + "  " + password[i]);
        if (names[i].equals ("Rogers.Steve")) index = i;
}

//现在打印它

if (index != -1)
    System.out.println(names[index] + "  " + password[index]);
else
    System.out.println ("Not found");