java程序不等待用户输入

java程序不等待用户输入,java,wait,Java,Wait,我的程序要求用户输入一些数据。我的问题是,当它运行时,它会显示第一个问题,然后立即转到第二个问题,而无需等待用户输入。我的朋友建议放sc.nextLine(),这是它第一次起作用,但当它循环回来时,它给了我一个额外的空间,实际上把空间保存到了我的obj中。 举例来说,当我运行它时,它显示: 原始:输入位置1:有多少个座位?(使用can输入) 使用附加sc.nextLine:输入位置1:(用户可以输入) 有多少个座位: 但第二次显示:输入位置2:(用户可以输入) (然后是空间) 接下来的问题是:有

我的程序要求用户输入一些数据。我的问题是,当它运行时,它会显示第一个问题,然后立即转到第二个问题,而无需等待用户输入。我的朋友建议放sc.nextLine(),这是它第一次起作用,但当它循环回来时,它给了我一个额外的空间,实际上把空间保存到了我的obj中。 举例来说,当我运行它时,它显示:

原始:输入位置1:有多少个座位?(使用can输入)

使用附加sc.nextLine:输入位置1:(用户可以输入) 有多少个座位:

但第二次显示:输入位置2:(用户可以输入) (然后是空间) 接下来的问题是:有多少个座位:

下面是我在主课上的全部代码。提前谢谢

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();
        }
    }
}
package选举;
导入java.util.ArrayList;
导入java.util.Scanner;
导入java.lang.StringBuilder;
公选{
公共静态void main(字符串[]args){
int-maxpos;
整数计数;
int-maxcan;
int cancount;
字符串名;
int-num;
串位置;
int posnum;
内部座位;
弦乐队;
国际投票;
扫描仪sc=新的扫描仪(System.in);
ArrayList=新建ArrayList();
候选人c;
做{
System.out.print(“本次选举的职位数量:”);
maxpos=sc.nextInt();

对于(count=1;count而不是使用扫描仪,可以使用DataInputStream的readLine()

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

无论何时使用
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();
这将使用流中未使用的
\n
字符

或的可能副本