是什么导致我的循环仅在第一次迭代中忽略此“\t”?
What is causing my loop to ignore this "\t" only in the first iteration?
出于某种原因,我的循环仅在 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 class 代码可以正常工作:
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
比您发布的其他示例短一个字符。所以你切换到更早的制表位。
none
与其他词相比是一个小词。为了解决这个问题,你可以在types
数组中的单词最后用空格填充以具有相同的长度。
制表符很棘手,因为它们受线上位置和制表符宽度的影响。
最好使用空格填充。使用 Apache Commons:
的便捷方式
StringUtils.rightPad(types[i], COLUMN_WIDTH)
(根据您最长的文字调整COLUMN_WIDTH
)
这是由于数组中第一个字符串的长度造成的。尝试使用 "nonee" 而不是 "none" 看看。
如果你想正确地做到这一点,你应该使用某种填充。
以下是格式
的修正
System.out.println(i + ": " + String.format("%-5s\t", types[i]) + (i + 1) + ": " + types[i + 1]);
这将解决问题。
选项卡如上所述存在。至于为什么事情不一致。可以参考这个link
既然还有none,我会用printf
来提供解决方案。您可以只使用格式化程序(如 System.out.printf()
来格式化字符串):
System.out.printf("%d: %-12s%d:%-12s\n", i, types[i], i+1, types[i+1]);
%d
允许您输入整数类型。
%-12s
允许您输入字符串(最小长度为 12 左对齐)...这将替换您的制表符!
出于某种原因,我的循环仅在 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 class 代码可以正常工作:
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
比您发布的其他示例短一个字符。所以你切换到更早的制表位。
none
与其他词相比是一个小词。为了解决这个问题,你可以在types
数组中的单词最后用空格填充以具有相同的长度。
制表符很棘手,因为它们受线上位置和制表符宽度的影响。
最好使用空格填充。使用 Apache Commons:
的便捷方式StringUtils.rightPad(types[i], COLUMN_WIDTH)
(根据您最长的文字调整COLUMN_WIDTH
)
这是由于数组中第一个字符串的长度造成的。尝试使用 "nonee" 而不是 "none" 看看。
如果你想正确地做到这一点,你应该使用某种填充。
以下是格式
的修正System.out.println(i + ": " + String.format("%-5s\t", types[i]) + (i + 1) + ": " + types[i + 1]);
这将解决问题。
选项卡如上所述存在。至于为什么事情不一致。可以参考这个link
既然还有none,我会用printf
来提供解决方案。您可以只使用格式化程序(如 System.out.printf()
来格式化字符串):
System.out.printf("%d: %-12s%d:%-12s\n", i, types[i], i+1, types[i+1]);
%d
允许您输入整数类型。%-12s
允许您输入字符串(最小长度为 12 左对齐)...这将替换您的制表符!