简体   繁体   English

Java程序不等待用户输入

[英]java Program does not wait for the user input

My program requires users to input some data. 我的程序要求用户输入一些数据。 My problem is, when it runs, it shows the first question and then immediately moves to the second question, without waiting for the user input. 我的问题是,当它运行时,它会显示第一个问题,然后立即移至第二个问题,而无需等待用户输入。 My friend suggested putting sc.nextLine(), it did work for the first time, but when it looped back it gave me an additional space and actually saved the space to my obj. 我的朋友建议将sc.nextLine()第一次使用,但是当它循环播放时,它给了我额外的空间,并实际上将空间保存到了obj中。 To illustrate, when i run it, it shows: 为了说明,当我运行它时,它显示:

Original: Enter position no 1: How many seats?(use can input) 原稿:进入位置1:有几个座位?(可以输入使用)

With the additional sc.nextLine: Enter position no 1: (user can input) How many seats: 使用附加的sc.nextLine:输入位置1 :(用户可以输入)多少个座位:

BUT second time it shows: Enter position no 2: (user can input) (and then space) and then the succeeding question: How many seats: 但第二次显示:输入位置2 :(用户可以输入)(然后输入空格),然后出现下一个问题:多少个座位:

Below is my whole code in my main class. 下面是我的主类中的全部代码。 Thanks in advance. 提前致谢。

package election;
import java.util.ArrayList;
import java.util.Scanner;
import java.lang.StringBuilder;

public class Election {

    public static void main(String[] args) {
        int maxpos;
        int count;
        int maxcan;
        int cancount;
        String name;
        int num;
        String position;
        int posnum;
        int seat;
        String party;
        int vote;

        Scanner sc = new Scanner(System.in);
        ArrayList<Candidate> list = new ArrayList<Candidate>();
        Candidate c;

        do{
            System.out.print("How many positions for this election: ");
            maxpos = sc.nextInt();
            for(count = 1; count<=maxpos; count++){
                System.out.print("Enter position no. " + count + ": ");

                position = sc.nextLine();
                sc.nextLine();
                posnum = count;
                System.out.print("How many seats: ");
                seat = sc.nextInt();
                System.out.print("Enter number of candidates: ");
                maxcan = sc.nextInt();

                for(cancount = 1; cancount<=maxcan; cancount++){
                    System.out.print("Enter candidate no. " + cancount + " name: " );
                    name = sc.nextLine();

                    num = cancount;
                    System.out.print("Enter candidate no. " + cancount + " party: " );
                    party = sc.nextLine();
                    c = new Candidate(name, num, position, posnum, seat, party);
                    list.add(c);
                }
            } 
        }while(!(count > maxpos));

        for(int i = 0; i<list.size(); i++){
            list.get(i).displayInfo();
        }
    }
}

Instead of using the Scanner, you can use readLine() of DataInputStream 可以使用DataInputStream的readLine()代替使用Scanner。

 DataInputStream din=new DataInputStream(System.in);
 System.out.print("Enter number of candidates: ");
 int maxcan =Integer.parseInt(din.readLine());

Whenever you use nextInt() , you need to put nextLine() after it 每当使用nextInt() ,都需要在nextLine()放置nextLine()

 System.out.print("How many seats: ");
                    seat = sc.nextInt();
                    sc.nextLine();
                    System.out.print("Enter number of candidates: ");
                    maxcan = sc.nextInt();
                    sc.nextLine();

This will consume the unconsumed \\n character from stream 这将消耗流中未使用的\\n字符

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

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