当前位置:  开发笔记 > 编程语言 > 正文

是什么导致我的循环只在第一次迭代中忽略这个"\ t"?

如何解决《是什么导致我的循环只在第一次迭代中忽略这个"\t"?》经验,为你挑选了1个好方法。

出于某种原因,我的循环只在我的for循环的第一次迭代后输出"\ t".这是我的循环代码:

input = -1;
private String[] types = {"none", "vanilla", "hazelnut", "peppermint", "mocha", "caramel"};
while ( (input < 0) || (input > (types.length - 1)) ){ //gets user's latte flavor input
        System.out.println("Enter the number corresponding to the latte type you would like:");
        for ( int i = 0; i < types.length; i++ ){
            if ( i <= (types.length - 2) ){ //prints two options per line
                System.out.println(i + ": " + types[i] + "\t" + (i + 1) + ": " + types[i + 1]);
            }
            else if ( i == (types.length - 1) ){
                System.out.println(i + ": " + types[i]);
            }
            else{ //does nothing on odd indices
            }
            i++;
        }
        input = keyboard.nextInt();
    }

这输出如下:

Enter the number corresponding to the latte type you would like:
0: none     1: vanilla
2: hazelnut     3: peppermint
4: mocha        5: caramel

正如我们所看到的,"1:vanilla"的间距与其他行的间距不同.但是,我的Tea类的代码可以正常工作:

input = -1;
private String[] types = {"white", "green", "oolong", "black", "pu-erh", "camomille"};
while ( (input < 0) || (input > (types.length - 1)) ){ //gets user's tea flavor input
        System.out.println("Enter the number corresponding to the tea type you would like:");
        for ( int i = 0; i < types.length; i++ ){
            if ( i <= (types.length - 2) ){ //prints two options per line
                System.out.println(i + ": " + types[i] + "\t" + (i + 1) + ": " + types[i + 1]);
            }
            else if ( i == (types.length - 1) ){
                System.out.println(i + ": " + types[i]);
            }
            else{ //does nothing on odd indices
            }
            i++;
        }
        input = keyboard.nextInt();
    }

这输出如下:

Enter the number corresponding to the tea type you would like:
0: white    1: green
2: oolong   3: black
4: pu-erh   5: camomille

是什么导致我的Latte循环(我的Espresso循环也遇到这个间距问题)输出与我的Tea循环不同?感谢您帮助我理解这种行为!



1> Henry..:

那么TAB实际上就在那里.请注意,这0: none比您发布的其他示例短一个字符.因此,您可以选择较早的制表位.

推荐阅读
kikokikolove
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有