【问题标题】:searching error [closed]搜索错误[关闭]
【发布时间】:2012-12-11 18:06:41
【问题描述】:

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

怎么了?

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();
}

【问题讨论】:

  • 你需要用一个变量来记下你输入了if语句。
  • @RohitJain 我认为错误消息是Cannot find the matric number!!!
  • 你可以让多个学生拥有相同的matrix 值吗?

标签: java search


【解决方案1】:

使用一个标志变量。在 for 循环之前将其设置为 false。在循环内部,只要找到匹配项,就将此标志设置为 true。在循环外使用标志上的 if 条件。如果 flag 为 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();
 }
}

【讨论】:

  • 只是一个旁注,使用一些有意义的名字而不是flag
  • @Vishal 编码是即兴的。我知道这是一种糟糕的编码习惯。感谢您指出。
  • @Manish 你能告诉我我哪里错了吗?
  • @Manish.. 总是引用你认为错误的原因,而不是仅仅把你的 cmets 扔掉。
  • 那也让他也写评论
【解决方案2】:

您的错误消息 System.out.println("Cannot find the matric number!!!"); 在您的代码中是无条件的,因此它会始终显示该错误

【讨论】:

    【解决方案3】:

    您返回void,因此您无法获得早期有意义的return 块的便利。您确实需要以某种方式表明您已进入if 块。你有两个*选项:

    • 使用布尔值表示,如果您已输入 if 块,则不应打印该消息,或者
    • 从方法中返回 String 而不是隐式打印,而是让调用者打印返回消息。

    (*: 您也可以在 if 语句中选择 returning,尽管不建议使用这种样式。这意味着,您可以将 return 紧跟在System.out.println().)

    用选项一重写,你的方法如下所示:

    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();
        }
    }
    

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

    【讨论】:

    • 您应该将 assignment 移动到 if 语句中。
    • 好收获。我没有意识到我这样做了 - 这次没有使用外部编辑器进行编辑。
    • You return void, so you don't get the convenience of an early return --> 错误。你可以随时写return;
    • 这是真的。我可能应该澄清“有意义的回报”。从 void 语句返回本质上是提前中断,这与我一直使用的大多数风格指南背道而驰。
    • 好吧,如果有多个匹配项,您就不能提前返回。
    猜你喜欢
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多