2015-11-15 29 views
0

我一直在努力工作幾個小時,但我似乎無法找到解決方案。代碼基本上是一個預約程序,當它運行時彈出一個框,'祕書'插入名稱和病人的時間。然而,如果我把「瑪麗亞」放在「1200」和「約翰」再次放在「1200」,系統會立即用瑪麗替換約翰。我的代碼如下:如何在處理中進行if語句驗證?

String[] names = new String[2400]; // from 0:00 until 24:00 

void setup() 
{ 
    String name = ""; 
    do 
    { 
    name = input("Name"); 
    if (name.equals("ABORT")) { 
     System.exit(0); 
    } // end the program 
    int time = int(input("Time code (e.g. 840 or 1200)")); 
    names[time] = name; 
    showAllNames(); 
    } 
    while (true); // loop never ends 

} 

void showAllNames()  // shows all the times where there is a name 
{ 
    for (int t = 0; t < 2400; t=t+1) 
    { 
    String name = names[t]; 
    if (name!=null) 
    { 
     println(t + "\t" + name); 
    } 
    } 
    println("================"); 
} 

public String input(String prompt) 
{ 
    return javax.swing.JOptionPane.showInputDialog(null, prompt); 
} 

我如何添加一個如果檢查該位置寫入前是空命令 - 否則警告用戶?

+1

如何在賦值之前檢查if(names [time] == null)? – Eran

+0

我該怎麼做到這一點?因爲我還需要一個彈出框,它需要是否應該更改信息的是/否問題。 @Eran –

回答

0

檢查索引null寫入之前:

if(names[time] == null){ 
    //Add the name 
} else { 
    //If the name isn't null, perhaps go to the next index, or throw an exception 
} 

你想要做什麼,如果名稱是null完全取決於你。

我想補充幾點建議,以及:

  • 讓你的名字變量的ArrayList,這樣你可以添加姓名的任何金額不改變的變量。

  • 不要在循環中調用您的showAllNames()方法,而應在循環結束後調用它。

  • 使用正常的索引而不是時間索引,這樣你就不必做空檢查。