Java question

2007-09-09 5:27 am
let say String [] names = {"may","Tom","Sam","Sue","Jim"};

how to write the code like looking "Sue" in this array?
i knoe using for loop but don knoe how to write the code

position = -1;

for{int i=0;i

回答 (1)

2007-09-09 11:24 am
✔ 最佳答案
class JavaTest
{
public JavaTest() {}

public static void main(String[] args)
{
String[] names = {"May", "Tom", "Sam", "Sue", "Jim"};
int pos = -1;
int i;
String str = "Sue";
for (i=0; i < names.length; i++)
{
if (names[i].equals(str))
pos = i;
}
if (pos == -1)
System.out.println("\"" + str + "\" is not found.");
else
System.out.println("\"" + str + "\" is at position " + (pos + 1) + ".");
}

}


收錄日期: 2021-04-13 13:23:39
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070908000051KK05007

檢視 Wayback Machine 備份