Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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 搜索错误_Java_Search - Fatal编程技术网

Java 搜索错误

Java 搜索错误,java,search,Java,Search,我不知道这个代码有什么问题。当我输入匹配的正确nomatric时,它将显示correct,但也会显示一条错误消息 怎么了 public void Search(int nomatric) { for (int i = 0; i < student.length; i++) { if (student[i].matrix == nomatric) { System.out.printf("%-25s%-25s%-25s%-25s%-25s\n"

我不知道这个代码有什么问题。当我输入匹配的正确
nomatric
时,它将显示correct,但也会显示一条错误消息

怎么了

public void Search(int nomatric) {
    for (int i = 0; i < student.length; i++) {
        if (student[i].matrix == nomatric) {
            System.out.printf("%-25s%-25s%-25s%-25s%-25s\n", "Matric", "Name", "Course work", "Final exam", "Grade");
            System.out.printf("%-20d %-20s %-20.2f %-20.2f %-20s", student[i].matrix, student[i].name, student[i].coursework, student[i].finalexam, student[i].grade);
            System.out.println();
        }  
    }
    System.out.println("Cannot find the matric number!!!");
    System.out.println();
}
public void搜索(int nomatric){
for(int i=0;i
您的错误消息
System.out.println(“找不到矩阵号!!!”在代码中是无条件的,因此它将始终显示您返回的错误
void
,因此您不会得到早期有意义的
返回块的便利。您确实需要指出您以某种方式输入了
if
块。您有两个*选项:

  • 使用布尔值表示,如果已输入
    if
    块,则不应打印消息,或
  • 从方法返回
    String
    ,而不是隐式打印,并让调用者打印返回消息
(*:您还可以从if语句中选择
return
ing,但不建议使用这种样式。这意味着,您可以将
return
放在
System.out.println()之后,而不是使用
boolean
变量)

使用选项1重写,您的方法如下所示:

public void search(int nomatric) {
    boolean success = false;
    for (int i = 0; i < student.length; i++) {
        if (student[i].matrix == nomatric) {
            success = true;
            System.out.printf("%-25s%-25s%-25s%-25s%-25s\n", "Matric", "Name", "Course work", "Final exam", "Grade");
            System.out.printf("%-20d %-20s %-20.2f %-20.2f %-20s", student[i].matrix, student[i].name, student[i].coursework, student[i].finalexam, student[i].grade);
            System.out.println();
        }  
    }
    if(!success) {

        System.out.println("Cannot find the matric number!!!");
        System.out.println();
    }
}
public void搜索(int nomatric){
布尔成功=假;
for(int i=0;i

选项二留给读者作为练习。

使用一个标志变量。在for循环之前将其设置为false。在循环内部,只要找到匹配项,就将此标志设置为true。外部循环使用if条件打开标志。如果标志为false,则表示未找到匹配项,则仅打印错误

  public void Search(int nomatric) {
     boolean flag=false;
     for (int i = 0; i < student.length; i++) {
     if (student[i].matrix == nomatric) {
        System.out.printf("%-25s%-25s%-25s%-25s%-25s\n", "Matric", "Name", "Course    work", "Final exam", "Grade");
        System.out.printf("%-20d %-20s %-20.2f %-20.2f %-20s", student[i].matrix,    student[i].name, student[i].coursework, student[i].finalexam, student[i].grade);
        System.out.println();
        flag=true;
    }  
 }
 if( ! flag) {
    System.out.println("Cannot find the matric number!!!");
    System.out.println();
 }
}
public void搜索(int nomatric){
布尔标志=假;
for(int i=0;i
您需要使用一个变量来注意您已经输入了if语句。@RohitJain我想错误消息是
找不到矩阵号。你能让多个学生拥有相同的
矩阵值吗
?你应该将
作业
移动到
if
语句中。很好。我没有意识到我这么做了-这次没有用外部编辑器编辑它。
您返回void,因此您没有提前返回的便利。您可以随时编写
return这是真的。我或许应该澄清“有意义的回报”。从void语句返回基本上是提前休息,这与我使用过的大多数样式指南都不符。好吧,如果有多个匹配项,就不能提前返回。只是一个旁注,请使用一些有意义的名称,而不是
flag
@Vishal编码是即兴进行的。我知道这是一个糟糕的编码练习。谢谢你指出。@Manish你能告诉我哪里错了吗?@Manish。。总是引用你觉得错误的原因,而不是把你的评论无限期地抛在脑后。然后也让他写评论