![](/img/trans.png)
[英]java while loop how to display only the users selection within a loop and not all user options
[英]How can I use while loop that represents split() and also for loop to display selection number for user?
While 循环中的 For 循环根本不执行。 如果没有 for 循环,它可以正常工作,但必须添加 for 循环才能在屏幕上显示选择编号。(例如 1.2.3)
try
{
BufferedReader br = new BufferedReader(new FileReader(path));
while((line = br.readLine()) != null) {
String[] values = line.split(",");
for (int i = 0; i < zoo.size(); i++) {
System.out.println("Select your choice.\nPress" + (i + 1) + ". (4Legged): " + values[0] + ", (insect):" + values[1] + ", (bird): " + values[2] );
}
}
txt文件示例为:
Monkey, Ant, Crow
Cat, Spider, Chicken
Dog, Grasshopper, Magpie
所需的 output 是:Select 您的选择。
按1。(4腿):猴子,(昆虫):Ant,(鸟):乌鸦
按 2。(4 条腿):猫,(昆虫):蜘蛛,(鸟):鸡
按 3。(4 腿):狗,(昆虫):蚱蜢,(鸟):喜鹊
try
{
BufferedReader br = new BufferedReader(new FileReader(path));
int index = 1;
while((line = br.readLine()) != null) {
String[] values = line.split(",");
System.out.println("Select your choice.\nPress" + index + ". (4Legged): " + values[0] + ", (insect):" + values[1] + ", (bird): " + values[2] );
index ++;
}
只需在while循环之前使用一个标志并在您的打印语句中使用它并在打印值后将其递增。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.