Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在处理过程中进行if语句验证?_Java_Arrays_Validation_If Statement_Processing - Fatal编程技术网

Java 如何在处理过程中进行if语句验证?

Java 如何在处理过程中进行if语句验证?,java,arrays,validation,if-statement,processing,Java,Arrays,Validation,If Statement,Processing,我一直在努力解决这个问题,但我似乎找不到解决办法。代码基本上是一个预约程序,当它运行时,弹出一个框,“秘书”插入患者的姓名和时间。然而,如果我把玛丽亚放在1200,约翰放在1200,系统会立即用约翰取代玛丽亚。我的代码如下: String[] names = new String[2400]; // from 0:00 until 24:00 void setup() { String name = ""; do { name = input("Name"); i

我一直在努力解决这个问题,但我似乎找不到解决办法。代码基本上是一个预约程序,当它运行时,弹出一个框,“秘书”插入患者的姓名和时间。然而,如果我把玛丽亚放在1200,约翰放在1200,系统会立即用约翰取代玛丽亚。我的代码如下:

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);
}
如何添加一个if命令,在写入之前检查位置是否为空,并警告用户?

在写入之前检查索引是否为空:

if(names[time] == null){
    //Add the name
} else {
    //If the name isn't null, perhaps go to the next index, or throw an exception
}
如果名称为空,您想做什么完全取决于您自己

我还想补充几点建议:

将您的名称变量设置为ArrayList,这样您就可以在不更改变量的情况下添加任意数量的名称

不要从循环中调用showAllNames方法,而是在循环结束后调用它

使用普通索引而不是时间索引,这样就不必进行空检查


在分配给它之前,检查names[time]==null如何?我该怎么做呢?因为我还需要一个弹出框,该框要求回答是否应更改信息的是/否问题@埃兰