Java程序过早地保留退出程序

时间:2015-09-23 08:35:44

标签: java java.util.scanner

我需要为我的Automata类构建一个简单的自动机。我正在使用Java,我不知道为什么我的程序过早地退出了我的生活。我已经尝试过调试它,到处都有打印语句来弄清楚它停在哪里,虽然我知道它停在哪里,但是我没有看到任何会使程序停止工作的东西。停止发生在第27行(我在哪里SOP“输入一串数字......” 知道我这可能很简单,但我无法想出这一点。

import java.util.*;
public class hw1 {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.println("Please indicate the number of states");
    int numState = input.nextInt();
    int[] state = new int[numState];
    boolean[] accept = new boolean[numState];
    for (int i = 0; i < numState; i++) {
        System.out.println("Is the state q" + (i + 1) + " a final state? (Answer 1 for yes; 0 for no)");
        int finalState = input.nextInt();
        if (finalState == 1)
            accept[i] = true;
    } // for
    System.out.println("Enter the number of symbols s: ");
    int numSym = input.nextInt();

    int[][] next = new int[numState][numSym];
    for (int i = 0; i < numState; i++) {
        for (int j = 0; j < numSym; j++) {
            System.out.println("What is the number for the next state for q" + i + " when it gets symbol " + j);
            next[i][j] = input.nextInt();
        }//nested for
    }//for

    String digits = input.nextLine();
    System.out.print("Enter a string of digits (0-9) without spaces to test:");

    int[] digitArray = new int[digits.length()];
    for (int i = 0; i < digits.length(); i++) {
        digitArray[i] = digits.charAt(i);
    }
    for (int i = 0; i < digits.length(); i++) {
        System.out.print(digitArray[i] + " ,");
    }
    System.out.println("end of program");
}// main;

}// class

1 个答案:

答案 0 :(得分:0)

将您的代码更改为:

input.nextLine();
System.out.print("Enter a string of digits (0-9) without spaces to test:");
String digits = input.nextLine();

这将在调用nextInt()

后获取并忽略流中留下的换行符