出于某种原因,我的循环只在我的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循环不同?感谢您帮助我理解这种行为!
那么TAB实际上就在那里.请注意,这0: none
比您发布的其他示例短一个字符.因此,您可以选择较早的制表位.