【问题标题】:Make an algorithm start again?让算法重新开始?
【发布时间】:2017-11-13 15:35:32
【问题描述】:

所以我对 Java 编码还是很陌生(昨天开始)。我想要做的是输入一个整数,如果 int c 它高于 1 或低于 0(如果它不是 1 或 0),我希望它开始再次。如果 int c 等于 1 或 0,我希望算法继续。我试图在 if(c > 1 || c 之后插入某种循环,但它似乎不起作用,只会向我的控制台发送垃圾邮件。有什么简单的方法可以让算法重新开始吗?我已经尝试修复此问题超过 2 小时,但我只是一遍又一遍地让我感到困惑。

// more code up here but it is unimportant
int c = sc.nextInt();

    if(c > 1 || c < 0) {
        result = result + wrong;
        System.out.println(result);
    } else if (c == 1) {
        result = result + notImplemented;
        System.out.println(result);
    } else if (c == 0) { //more code follows here but is unimportant

【问题讨论】:

  • "again" - 重用它...把它放在一个方法中并再次调用它
  • @lolo 我不想再次调用整个方法,因为上面还有更多内容。不过谢谢你,应该提到这一点。

标签: java string algorithm if-statement


【解决方案1】:

所以你想再次请求输入,我假设。

一个简单的方法可能是:

int c = sc.nextInt();

while (c > 1 || c < 0) {
    c = sc.nextInt();
}
//code continues

【讨论】:

  • 但后来他不再打印消息了
  • 嘿,是的,我的消息有些问题,但可以通过以下方式解决:if(c &gt; 1 || c &lt; 0) { while (c &gt; 1 || c &lt; 0) { System.out.println("No 0 or 1, try again: "); c = sc.nextInt(); } } 现在可以使用,非常感谢 :)
  • 很高兴您发现它有帮助。请将此标记为正确答案
【解决方案2】:

这种情况下可以使用while,使用break退出:

while (scanner.hasNext()) {
  int c = sc.nextInt();

    if(c > 1 || c < 0) {
        result = result + wrong;
        System.out.println(result);
    } else if (c == 1) {
        result = result + notImplemented;
        System.out.println(result);
        break;
    } else if (c == 0) {
       ...
       break;
    }
}

scanner.close();

【讨论】:

    【解决方案3】:

    你需要使用一个循环,所以你有

    while(true){
        int c = sc.nextInt();
    
        if(c > 1 || c < 0) {
            result = result + wrong;
            System.out.println(result);
            break;
        } else if (c == 1) {
            result = result + notImplemented;
            System.out.println(result);
        } else if (c == 0) { //more code follows here but is unimportant
        ...
    }
    

    既然你说你是新人,那我稍微解释一下: 只要某个条件为真,While 循环就会重复其代码块中的内容(即在{ } 中)。就我的回答而言,我做了while(true) 这意味着它会不断重复,直到某些事情导致它停止。在这种情况下,我使用了 break 来强制循环结束/停止。

    【讨论】:

    • 感谢您的解释。我试图实现一个 while(true) { } 循环,但它并没有按照我想要的方式工作。我设法解决了这个问题。首先我检查,如果 c 不等于 1 或 0,如果不等于,那么只要 c 不等于 1 或 0,我就会循环并将 c 重置为下一个整数作为输入。谢谢!
    【解决方案4】:

    使用hasNextInt()while 循环遍历数据:

    while (sc.hasNextInt()) {
        int aInt = sc.nextInt();
        //logic here
    }
    

    hasNextInt() 方法的文档: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNextInt()

    【讨论】:

      【解决方案5】:

      您可以将您的代码放入一个函数中(我希望已经如此),然后当您没有预期的结果并想再次调用它时,只需在内部调用您的函数即可。
      这称为递归。 你可以了解更多here。 例如 :

      // more code up here but it is unimportant
      public void myFunction(){
          int c = sc.nextInt();
          if(c > 1 || c < 0) {
              result = result + wrong;
              System.out.println(result);
          } else if (c == 1) {
              result = result + notImplemented;
              System.out.println(result);
          } else if (c == 0) { //more code follows here but is unimportant
          }
          //You want to call your function again
          myFunction();
      }
      

      【讨论】:

      • 我不想再次调用整个方法,因为上面还有更多的东西。谢谢,应该提到的。不过,我不知道为什么这被否决了。
      猜你喜欢
      • 2021-10-04
      • 1970-01-01
      • 1970-01-01
      • 2019-07-06
      • 1970-01-01
      • 1970-01-01
      • 2015-01-08
      • 2022-01-23
      • 2011-05-15
      相关资源
      最近更新 更多