出于某种原因,我的循环只在我的for循环的第一次迭代后输出“\ t”。 这里是我的循环代码:什么导致我的循环只在第一次迭代中忽略这个“ 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:香草”不以同样的方式在其他行的间隔。我对我的茶类代码,然而,正常工作:
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
是什么原因导致我的拿铁循环(我的咖啡循环也受到这个间距问题)来输出不同比我的茶圈?感谢您帮助我理解这种行为!
我已经为你的问题提出了一个'printf()'解决方案...这可能是最简单的方法来处理你的问题 – ParkerHalo