0

我能够分隔字符串输入中的字符,但我的问题是:

Enter an integer: 123

输出是:

Individual digits: 1 Individual digits: 2 Individual digits: 3

它还复制了不打算使用的“单个数字”。这是我的代码

public class gradedExer1A {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter an integer: ");
        String input = sc.nextLine();
        int len = input.length();


        for(int i = 0; i < len ; i++) {

            char in = input.charAt(i);

            System.out.print("Individual digits: " + in + " ");

        }


    }

}
4

1 回答 1

1

尝试

System.out.print("Individual digits: ");
for(int i = 0; i < len ; i++) {

    char in = input.charAt(i);
    System.out.print(in + " ");

}
于 2012-11-14T11:55:01.203 回答