2017-05-20 53 views
-1

我有一個Java數組(基於控制檯),我想在用戶輸入時打印出存儲在其中一個位置的詳細信息。例如,他們在控制檯應用程序中輸入「3」,然後程序以匹配請求的詳細信息作出響應。在Java中打印特定的陣列位置(控制檯)

在此先感謝!

if(input.matches("S")){ 
// If user input matches with S, then the user willSSN be asked to enter a array position and then the corresponding information will be shown 



    KeyStroke[] patientsDetails = new KeyStroke[5]; 
    patientsDetails[0] = new KeyStroke(9,"OX5BJM","Peter",2039489); 
    patientsDetails[1] = new KeyStroke(12,"OX1BOL","Kim",2434587); 
    patientsDetails[2] = new KeyStroke(67,"OX2VBN","Patrick",2233842); 
    patientsDetails[3] = new KeyStroke(34,"OX2XHB","Liam",2432340); 
    patientsDetails[4] = new KeyStroke(54,"OX3BUN","Bob",2234098); 

    System.out.println("Enter an array postion from 0 to 4 to show paitients corresponding postion and details"); 

    number1 = enterNumber0.nextInt(); 
    if (input.matches("0")){ 

     System.out.println(patientsDetails[0]); 
    } 
+0

它是程序的一部分。不確定提問具體到我的工作的問題是什麼? –

+0

它是一個非常基本的問題,它表明沒有努力學習或研究 –

回答

1

如果我正確理解你的問題,您打印"Enter an array position from 0 to 4..."到控制檯後問題坐落。

去替代

number1 = enterNumber0.nextInt(); 
    if (input.matches("0")){ 

     System.out.println(patientsDetails[0]); 
    } 

number1 = enterNumber0.nextInt(); // is enterNumber0 the name of your Scanner object? 
System.out.println(patientsDetails[number1]); // I assume that number1 is declared as an `int` somewhere in your code? 

如果我沒有得到你的問題的全貌,請原諒我,並試圖澄清的問題會好一點。

+0

嗨!這是非常有用的,但是當我運行該程序時,它會打印出一些我無法理解的東西。 –

+0

「輸入從0到4的數組位置以顯示對應的位置和詳細信息」在此之後,我輸入「0」,它給了我: [email protected] –

+1

這可能是您看到的對象的哈希碼。它看起來像'code.KeyStroke @ 2f92e0f4'嗎?如果是這樣,您將不得不重寫KeyStroke類的'.toString()'方法或創建自己的方法,以便顯示各種患者詳細信息。 – steven