[英]How can I use while loop that represents split() and also for loop to display selection number for user?
For loop inside While loop not executing at all. While 循环中的 For 循环根本不执行。 It would work fine without the for loop but had to add the for loop in order to have selection numbers displayed on the screen.(eg 1.2.3)如果没有 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] );
}
}
The txt file example is: txt文件示例为:
Monkey, Ant, Crow
Cat, Spider, Chicken
Dog, Grasshopper, Magpie
The desired output is: Select your choice.所需的 output 是:Select 您的选择。
Press 1. (4Legged): Monkey, (Insect): Ant, (Bird): Crow按1。(4腿):猴子,(昆虫):Ant,(鸟):乌鸦
Press 2. (4Legged): Cat, (Insect): Spider, (Bird): Chicken按 2。(4 条腿):猫,(昆虫):蜘蛛,(鸟):鸡
Press 3. (4Legged): Dog, (Insect): Grasshopper, (Bird): Magpie按 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 ++;
}
just use a flag before while loop and use that in your print statement and increment it after printing the values.只需在while循环之前使用一个标志并在您的打印语句中使用它并在打印值后将其递增。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.