简体   繁体   English

Do-While循环填充数组

[英]Do-While loop to fill out Array

I am currently working on a project that asked me to set up data entry. 我目前正在做一个要求我设置数据输入的项目。 In the data entry mode the user will be asked to enter the Scientists data in a loop. 在数据输入模式下,将要求用户循环输入科学家数据。 if the user responds with 'y' they will be prompted to enter another scientist. 如果用户回答“ y”,将提示他们输入另一位科学家。 The best way I can think to do this is with a do-while loop to fill the array until the user decides its over. 我认为最好的方法是使用do-while循环填充数组,直到用户决定结束为止。 I am having trouble with 我有麻烦

  1. filling the names into the array, and 将名称填充到数组中,以及
  2. the program won't prompt for a name after the initial loop. 初始循环后,程序将不会提示您输入名称。

Here is what I have: 这是我所拥有的:

public class Scientist {  
    private String name; 
    private String field;
    private String greatIdeas;

    public static void main(String[] args) {
        String scientists[] = new String[100];
        int scientistCount = 0;
        Scanner input = new Scanner(System.in);    

        do{
            String answer;
            System.out.println("Enter the name of the Scientist: ");          
            scientists[scientistCount]=input.nextLine();

            System.out.println(scientistCount);
            System.out.println("Would You like to add another Scientist?");
            scientistCount++;
        }

        while(input.next().equalsIgnoreCase("Y"));
        input.close();
    }
}

always prefer to read input using nextLine() and then parse the string. 总是喜欢使用nextLine()读取输入,然后解析字符串。

Using next() will only return what comes before a space. 使用next()将仅返回空格之前的内容。 nextLine() automatically moves the scanner down after returning the current line. 返回当前行后, nextLine()自动将扫描仪向下移动。

A useful tool for parsing data from nextLine() would be str.split("\\\\s+") . 用于解析nextLine()数据的有用工具将是str.split("\\\\s+")

public class Scientist {  
        private String name; 
        private String field;
        private String greatIdeas;

        public static void main(String[] args) {
            String scientists[] = new String[100];
            int scientistCount = 0;
            Scanner input = new Scanner(System.in);    

            do{
                String answer;
                System.out.println("Enter the name of the Scientist: ");          
                scientists[scientistCount]=input.nextLine();

                System.out.println(scientistCount);
                System.out.println("Would You like to add another Scientist?");
                scientistCount++;
            }

            while(input.nextLine().equalsIgnoreCase("Y"));
            input.close();
        }
    }

Change while(input.next().equalsIgnoreCase("Y")); 更改while(input.next().equalsIgnoreCase("Y")); to while(input.nextLine().equalsIgnoreCase("Y")); while(input.nextLine().equalsIgnoreCase("Y"));

Is this the solution that you mean 这是你的意思解决方案

String scientists[] = new String[100];
    int scientistCount = 0;
    Scanner input = new Scanner(System.in);    
    boolean again = true;

    while(again){
        System.out.println("Enter the name of the Scientist: "); 
        scientists[scientistCount]=input.nextLine();
        scientistCount++;
        System.out.println(scientistCount);
        System.out.println("Would You like to add another Scientist? y/n");
        if(!input.nextLine().equalsIgnoreCase("y")){
            again = false;
        }
    }
    input.close();

Another way to do it, which i find simpler is with an arraylist, and a regular while loop that breaks after you change a boolean. 我发现更简单的另一种方法是使用arraylist,并在更改布尔值后中断常规的while循环。 See following example: 请参见以下示例:

    ArrayList<String> scientists = new ArrayList<String>();
    Scanner input = new Scanner(System.in);
    boolean keepGoing = true;

    while(keepGoing){
        System.out.println("Enter the name of the Scientist: ");
        scientists.add(input.nextLine());
        System.out.println("Would You like to add another Scientist? (y/n)");

        if(input.nextLine().toLowerCase().equals("y")){continue;}
        else{keepGoing = false;}
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM