简体   繁体   English

如何使用表示 split() 的 while 循环和 for 循环来为用户显示选择编号?

[英]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.

相关问题 java while循环如何在循环中仅显示用户选择,而不是所有用户选项 - java while loop how to display only the users selection within a loop and not all user options 如何在这段代码中使用while循环? - How can I use a while loop in this code? 当值超过给定数字时,如何使用while或for循环进行计数? - How can I use while or for loop to count when value exceed the given number? 从main方法调用,当值超过给定数字时,如何使用while或for循环进行计数? - Call from main method, how can I use while or for loop to count when value exceed the given number? 如何使用将用户输入存储到 5 作为计数的 while 循环 - How can I use a while loop that stores user input up until 5 as a count 我将如何使用while循环来不断请求用户输入 - How would I use a while loop to keep requesting user input 如何将用户输入用于while循环中的字符串数组? - How do i use user input for a string array that is in a while loop? 如何在java中使用while循环堆叠数字模式? - How can I stack up the number patterns with while loop in java? 如何将此 while 循环更改为 for 循环 - How can I change this while loop to a for loop 我怎么能用最多 100 个扫描仪编号循环并且它们的总和也不会超过 100 - How can i do while loop with scanner numbers up to 100 and their sum also will not be over 100
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM