gpt4 book ai didi

java - 是什么导致我的循环仅在第一次迭代中忽略此 "\t"?

转载 作者:行者123 更新时间:2023-12-02 15:15:43 26 4
gpt4 key购买 nike

出于某种原因,我的循环仅在 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 比您发布的其他示例短一个字符。因此,您按 Tab 键切换到较早的制表位。

关于java - 是什么导致我的循环仅在第一次迭代中忽略此 "\t"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34307453/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com