简体   繁体   English

BlueJ试图永远运行我的Java程序

[英]BlueJ is trying to run my Java program forever

I've just started learning Java (I am a C#.NET programmer as well). 我刚刚开始学习Java(我也是C#.NET程序员)。 I am trying to get multiple user inputs and add them to an array. 我试图获取多个用户输入并将其添加到数组。 After this, I calculate the average from the given values. 之后,我根据给定的值计算平均值。

For some reason, BlueJ will try to run my Java program forever. 出于某种原因,BlueJ将尝试永远运行我的Java程序。 Meaning, It will keep showing the progress bar and will not open any console window. 意思是,它将继续显示进度条,并且不会打开任何控制台窗口。

I'm not sure if it's something wrong with my code, or BlueJ, because I've never encountered a problem such as this one before. 我不确定我的代码或BlueJ是否有问题,因为我之前从未遇到过此类问题。

Here is my code: 这是我的代码:

import java.util.Scanner;

public class Problem22 {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    int inputs = 2;
    int[] values = new int[3];

    while (inputs > -1) {
      values[inputs] = scanner.nextInt();

      inputs--;
    }

    System.out.println(averageValue(values));
  }

  private static int averageValue(int[] values) {
    int sum = 0;

    for (int i : values) {
      sum += i;
    }

    return (sum / values.length);
  }
}

Please help me try and find a solution. 请帮助我尝试找到解决方案。

It seems that in BlueJ, you have to supply output before you ask for input. 似乎在BlueJ中,您必须先提供输出,然后再要求输入。 It's quite a weird bug. 这是一个很奇怪的错误。

More info: 更多信息:

http://www.bluej.org/help/faq.html#hangoninput http://www.bluej.org/help/faq.html#hangoninput

Your code worked for me in Eclipse, but I had to realize what I was supposed to do, enter three ints. 您的代码在Eclipse中为我工作,但是我必须意识到我应该做的事情,输入三个整数。

It is generally better to prompt the user for input. 通常最好提示用户输入。 This may be a bug in BlueJ, but it's not too bad to have to output a prompt before asking for input. 这可能是BlueJ中的错误,但在要求输入之前必须输出提示也不错。 It's just generally a good thing to do. 通常这只是一件好事。

Link to my version of the code with prompts: 通过提示链接到我的代码版本:

https://gist.github.com/kaydell/6552282 https://gist.github.com/kaydell/6552282

I believe that the only reason not to prompt for input is if you are reading input from a file or something. 我认为不提示输入的唯一原因是从文件或其他内容读取输入。 When your program is interactive with the user, your programs should prompt the user for input. 当您的程序与用户交互时,您的程序应提示用户输入。

The code compiles just fine for me in IntelliJ IDEA, and also runs fine. 该代码对我来说可以在IntelliJ IDEA中很好地编译,并且可以正常运行。 so I would assume it's a BlueJ bug. 所以我认为这是一个BlueJ错误。

Here is an example input and output after running it (pressing enter after each input line) 这是运行后的输入和输出示例(在每个输入行之后按Enter)

3 
4
5
4

(which means by the way your code works correctly, 4 is the average of 3,4,5...) (这意味着您的代码可以正常工作,4是3,4,5的平均值...)

Which version of BlueJ are you using? 您正在使用哪个版本的BlueJ? I assume restart to BlueJ or even your machine didn't work? 我以为重新启动到BlueJ甚至您的机器都无法正常工作?

Terminal window opens only when there is an output. 终端窗口仅在有输出时打开。 Th program has asked only for input. 该程序仅要求输入。 Therefore it is terminal window isn't opening. 因此,终端窗口没有打开。 Replace your snippet by this one: 用以下内容替换您的代码段:

`while (inputs > -1) 
 {
 System.out.println("Input number - "+inputs);
  values[inputs] = scanner.nextInt();
  inputs--;
 }`

I hope you will see the terminal window. 希望您会看到终端窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM