【问题标题】:Why won't my program terminate when i input END?为什么当我输入 END 时我的程序不会终止?
【发布时间】:2018-05-17 12:53:13
【问题描述】:

当“END”被输入到控制台时,我试图终止我的代码,但是我的代码不会公平运行,而且我似乎看不到哪里出错了,顺便说一句,我还没有学会如何调试的,所以这在很大程度上与为什么我无法弄清楚。如果可以的话,请帮忙。

import java.util.*;
class Main 
{
public static void main(String args[])
{
    int j = 0;
    while (j++ <= 3) {    
        // Create Scanner object to take input from command prompt
        Scanner s=new Scanner(System.in); 
        // Take input from the user and store it in st
        String st = "";
        while (!st.equals("END")) {
            {
                st=s.nextLine();
                // Initialize the variable count to 0
                int count=0;
                // Convert String st to char array
                char[] c=st.toCharArray();
                // Loop till end of string
                for(int i=0;i<st.length();i++)

                // If character at index 'i' is not a space, then increment count
                    if(c[i]!=' ') count++;
                // Print no.of spaces
                System.out.printf("[%4d] spaces in ", +(st.length()-count));
                // Print no.of spaces
                System.out.println('"' + st + '"');

            }
            j++;
        }
    }
}

【问题讨论】:

标签: java string break bluej


【解决方案1】:

由于您有while (j++ &lt;= 3) { ... },如果您输入“END”两次,您的程序将结束。

【讨论】:

  • 哦,对了,我只是假设它会循环我的代码三遍。
【解决方案2】:

因为您有两个嵌套的 while 循环。您在第二个 while 循环中输入“END”,然后第二个循环结束,这是正确的。但是正如你所看到的,在它结束后它将开始第一个循环,即while (j++ &lt;= 3) {,并且在这个while循环中它等待来自用户的输入3次,对应于Scanner s=new Scanner(System.in);,因此你的程序不这样做是正常的出口。如果您希望您的程序在一些输入后完成,您可能需要使用System.exit(-1); 命令。我已经根据你的 cmets 添加了代码。

import java.util.*;

class Main {

public static void main(String args[]) {
int j = 0;
while (j++ <= 3) {
  // Create Scanner object to take input from command prompt
  Scanner s = new Scanner(System.in);
  // Take input from the user and store it in st
  String st = s.nextLine();
    if (!st.equals("END")) {
    // Initialize the variable count to 0
    int count = 0;
    // Convert String st to char array
    char[] c = st.toCharArray();
    // Loop till end of string
    for (int i = 0; i < st.length(); i++)

    // If character at index 'i' is not a space, then increment count
    {
      if (c[i] != ' ') {
        count++;
      }
    }
    // Print no.of spaces
    System.out.printf("[%4d] spaces in ", +(st.length() - count));
    // Print no.of spaces
    System.out.println('"' + st + '"');
  } else {
    System.exit(-1);
  }
}
}
}

【讨论】:

  • 他还有一个额外的j++,所以我认为它只会出现两次 - 不过我还没有通过它。
  • 你好,你知道我将如何以及在哪里集成 System.exit(-1);命令。输入“END”时?
  • 我不知道你有while (j++ &lt;= 3) { 这个while循环的理由,但根据你的第二个while循环while (!st.equals("END")) {你可能想在结束后添加System.exit(-1);命令.因为那个while循环告诉我你可能想在用户输入“END”后完成你的循环,而那个时间就是循环结束的时候。但是我又不知道你为什么有while (j++ &lt;= 3) {
  • 我只使用了 while (j++
  • 我添加了一个我认为对你有用的示例代码。您不需要第二个 while 循环,您可以使用 if 情况。
【解决方案3】:

我只是在没有使用循环的情况下添加了一个 if 条件。

public static void main(String args[]) {
    int j = 0;
    while (j++ <= 3) {
        // Create Scanner object to take input from command prompt
        Scanner s = new Scanner(System.in);
        // Take input from the user and store it in st
        String st = "";

        st = s.nextLine();

        if (st.equalsIgnoreCase("END")) {
            j = 5;
        }
        // Initialize the variable count to 0
        int count = 0;
        // Convert String st to char array
        char[] c = st.toCharArray();
        // Loop till end of string
        for (int i = 0; i < st.length(); i++) // If character at index 'i' is not a space, then increment count
        {
            if (c[i] != ' ') {
                count++;
            }
        }
        // Print no.of spaces
        System.out.printf("[%4d] spaces in ", +(st.length() - count));
        // Print no.of spaces
        System.out.println('"' + st + '"');
        j++;
    }
}

当我得到输入端时,我基本上所做的只是将 j 的值更改为 5,以便终止第一个循环。并停止服用 inpu。

您想要这种类型的解决方案还是需要输入 3 次?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 2013-04-22
    • 1970-01-01
    相关资源
    最近更新 更多