[英]Why does this code not wait for userinput?
I just wrote some code in java which is supposed to let the user input data bit by bit.我只是在java中写了一些代码,它应该让用户一点一点地输入数据。 Once all data is captured, it will do its thing and throw out some output.
一旦捕获了所有数据,它就会做它的事情并抛出一些输出。 Now here's the problem: The program does not wait for user input at all.
现在问题来了:程序根本不等待用户输入。 Here's a minimized version of my code:
这是我的代码的最小化版本:
import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
public class MyClass {
// initialize array variable in global namespace
static boolean[][] array;
public static void main(String[] args) {
// Input numbers
Scanner text = new Scanner(System.in);
int[] first_in = new int[2];
System.out.println("Please start data entry: \n");
// Take in first variables (numbers)
for(int i = 0; i<first_in[0]; i++) {
first_in[i] = text.nextInt();
}
int n = first_in[0];
// Initialize reference array
String[] reference = new String[n];
System.out.println("Please give names: \n");
// Ask for reference names
for(int i = 0; i<n; i++) {
reference[i] = text.next();
}
System.out.println("Please give other data: \n");
// Ask for edges
String edgein;
for(int i = 0; i<first_in[1]; i++) {
edgein = text.nextLine();
/* do some stuff */
}
// Be nice and close the scanner
text.close();
System.out.println("Scanner closed.");
The output is simply输出很简单
Please start data entry:
Please give vertices:
Please give edges:
Scanner closed.
Why does the program not wait for user input in either one of the three loops?为什么程序不在三个循环中的任何一个循环中等待用户输入? And how can I fix it?
我该如何解决?
You need to initialize the array first_in
.您需要初始化数组
first_in
。 The default value is [0,0]
so it doesn't go inside the loop since the comparison i<first_in[0]
will be 0<0
.默认值为
[0,0]
因此它不会进入循环,因为比较i<first_in[0]
将为0<0
。
Scanner text = new Scanner(System.in);
int[] first_in = new int[2];
System.out.println("Please start data entry: \n");
// Take in first variables (numbers)
for(int i = 0; i<first_in[0]; i++) {
first_in[i] = text.nextInt();
}
int n = first_in[0];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.