单独的字符串输入

时间:2012-11-14 11:53:24

标签: java loops

我可以将字符串输入中的字符分开,但我的问题是:

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 + " ");

        }


    }

}

1 个答案:

答案 0 :(得分:1)

尝试

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

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

}