简体   繁体   English

为什么这段代码不等待用户输入?

[英]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.

相关问题 为什么主线程等待 - why does the main thread wait SELECT为什么要等待锁? - Why does a SELECT wait for a lock? 为什么这段代码等待 1000 毫秒而不是 500 毫秒? - Why does this code wait 1000ms instead of 500ms? 为何程序要等待schedule()完成,而不等待scheduleWithFixedDelay()? - Why does the program wait for the schedule() to finish but doesn't wait for the scheduleWithFixedDelay()? 如何在AsyncTask中使用Runnable.wait()?为什么AsyncTask不等待......? - How to use Runnable.wait() in AsyncTask? Why does the AsyncTask NOT wait…? 为什么没有调用wait方法之后的代码 - Why is the code just after wait method not invoked 要找到给定数字的质因数,为什么要将 for 循环的第二条语句设置为 i * i &lt;= userInput 而不是 i &lt;= userInput? - To find prime factors of a given number, why set the second statement of the for loop to i * i <= userInput instead of i <= userInput? Java wait()/ join():为什么这不会死锁? - Java wait()/join(): Why does this not deadlock? 为什么我的程序不等待Thread中的动画? - Why my program does not wait for animation in Thread? 如何使用 node.js 验证后端用户输入的代码 - How to validate code from a userinput in the backend useing node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM