【问题标题】:Searching an array for a course在数组中搜索课程
【发布时间】:2016-08-31 19:13:10
【问题描述】:

这是算法,但我在破译过程中遇到了困难 :

set flag equal to false
set index equal to 0

WHILE(index is less than number of courses in array AND flag == false)
    extract substring
    IF(string1.equals(string2) == true) THEN //course found
        set flag to true
    ELSE
        increment index
    END IF
END WHILE

IF flag == false THEN
    display message the course name was not found
ELSE
    course name found at position index
END IF

这是我的代码:

public void searchCourse(String courseName)
{
    int index;
    String extract;
    boolean notFound = false;
    index = 0;
    while(index < SIZE)
    {
        extract = schedule[index].charAt(0,6);
        if (courseName == extract){

        }
        else {
            index ++;
        }
        if ( notFound == false){
            System.out.println("Course Not Found");
        } 
        else{
            System.out.println("Course Found at: " + index);
        }
    } 
}    

【问题讨论】:

  • 你在哪里更新 notFound 值?在您的代码中,它始终设置为 false 并且永远不会更改。
  • scheduleString[] 吗? charAt 没有采用 2 个整数的重载。
  • 是的 schedule 是一个 String[] 所以我应该使用 .substring(0,6) 吗? @Jorn Vernee
  • @Cuse 是的,我认为这会满足你的要求。

标签: java arrays string substring


【解决方案1】:

您的代码必须与伪代码相似。

  • 在伪代码中,while 循环有两个条件,你的代码只有一个。你在java中使用&&做一个AND,所以它变成&amp;&amp; ! notFound
  • 在您的第一个 if 条件中,您必须将标志设置为 true。很简单的做作notFound = true

顺便说一句,标志应该是found 而不是notFound,但它不会改变任何东西,除了可读性。

【讨论】:

  • 这让我感到困惑的是如何编写带有两个条件的while循环你使用a还是a;分开索引
  • 我已经升级了我的答案,因此您拥有对代码进行两次修改所需的一切。
【解决方案2】:

这里是正确的代码

 public void searchCourse(String courseName)
 {
    int index;
    String extract;
    boolean notFound = false;
    index = 0;
    while(index < SIZE)
    {
        notFound = false;
        extract = schedule[index].charAt(0,6);
        if (courseName == extract){
               notFound = true;
        }        
        if ( notFound == false){
            System.out.println("Course Not Found");
        } 
        else{
            System.out.println("Course Found at: " + index);
        }
        index ++;
    } 
}

【讨论】:

  • 您在 while 循环中错过了一个测试。所以你的代码会遍历所有数组而不是尽快停止。
  • 只有在没有找到字符串的情况下才需要增加索引。所以你的索引将会比它应该的更大(大约 1)。
【解决方案3】:

伪代码看起来不必要的复杂。

这就足够了:

public void searchCourse(String courseName) {
    for(int i = 0; i < schedule.length; i++) {
        if(courseName.equals(schedule[i].substring(0, 6))) {
            System.out.println("Course Found at: " + i);
            return;
        }
    }

    System.out.println("Course Not Found");
}

无论如何,正确的翻译应该是这样的:

public void searchCourse(String courseName) {    
    boolean flag = false;
    int index = 0;

    while(index < schedule.length && !flag) {
        String extract = schedule[index].substring(0,6);
        if (courseName.equals(extract)){
            flag = true;
        } else {
            index++;
        }
    }

    if (!flag){
        System.out.println("Course Not Found");
    } else {
        System.out.println("Course Found at: " + index);
    }
}  

【讨论】:

  • 感谢@John Vernee 的澄清。
猜你喜欢
  • 2014-02-23
  • 2012-12-18
  • 1970-01-01
  • 1970-01-01
  • 2014-04-26
  • 2013-04-15
  • 2020-02-01
  • 2017-03-26
相关资源
最近更新 更多