Search Array Value

create program to search for certain value in an array

a) create an array of string type that contains 10 names

b) use a loo(either for or while) to search through the array for the name specified by the user

c) when the name is found, terminate the loop right away

pseudo-ocode - when you find...

please enter a name to search in my database: Eric

Looking for "Eric"...

Not at index 0...

Not at index 1...

Bingo! I found "Eric" at index 6!!

Search ends

pseudo-ocode - when you cannot find...

please enter a name to search in my database: Eric

Looking for "Linda"...

Not at index 0...

Not at index 1...

Sorry! "Linda" is not in my database!!

Search ends

拜託個位大大幫幫我阿..

我想了好多天都想不出來阿..~“~

在這邊先謝謝大家了..感激不近..

2 Answers

Rating
  • 1 decade ago
    Favorite Answer

    請參考我的做法

    public static void main(String[] args) {

    // 10 names array

    String[] ary = {"James", "John", "Robert", "Michael", "William",

    "Mary", "Patricia", "Linda", "Barbara", "Elizabeth"};

    // user input

    System.out.print("Please enter a name to search in my database: ");

    Scanner scan = new Scanner(System.in);

    String s = scan.next();

    System.out.println("Looking for " + s + "...");

    boolean found = false; // flag for name found

    for (int i = 0; i < ary.length; i++) {

    if (s.equals(ary[i])) {

    System.out.println("Bingo! I found \"" + s +

    "\" at index " + i + "!!");

    found = true; // set the name found to true

    break; // once found, break the loop

    }

    }

    if (!found) {

    System.out.println("Sorry! \"" + s + "\" is not in my database!!");

    }

    }

  • Anonymous
    6 years ago

    * ***** http://*****

    [電子遊戲]

    拉霸、水果盤、7PK、5PK

    [運彩遊戲]

    棒球、籃球、足球、網球、冰球、各種體育經典賽事

    [真人遊戲]

    百家樂、21點、骰寶、輪盤、牌九、三公、輪盤、翻攤、牛牛、二八杠

    [對戰遊戲]

    台灣麻將、德州撲克、骰盅吹牛、四支刀、鬥地主、十三支、二八槓、暗棋、接龍

    [彩球遊戲]

    香港六合、台灣樂透、今彩539、樂合彩、大陸時時彩、基諾彩、北京賽車、賽狗、賽馬、指數

    [優惠活動]

    1. 新舊會員儲值就送500點

    2. 真人百家樂彩金等你拿

    ***** http://*****

    歡迎免費體驗試玩!!

    線上影片直播、正妹圖、討論區免費註冊 ~

Still have questions? Get your answers by asking now.