循环内部的循环,而循环根本不执行。如果没有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所需的输出是:选择您的选择。
按1. (4腿):猴子,(昆虫):蚂蚁,(鸟):乌鸦
按2. (4腿):猫,(昆虫):蜘蛛,(鸟):鸡
按3. (4腿):狗,(昆虫):蚱蜢,(鸟):喜鹊
发布于 2022-08-13 17:31:32
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循环之前使用标志,并在print语句中使用该标志,并在打印值后增加它。
https://stackoverflow.com/questions/73346347
复制相似问题