Do-While循环填充数组

时间:2016-11-25 15:02:01

标签: java arrays do-while

我目前正在开展一个项目,要求我设置数据输入。在数据输入模式中,将要求用户循环输入科学家数据。如果用户回答'y',他们将被提示进入另一位科学家。我能想到的最佳方法是使用do-while循环来填充数组,直到用户决定结束。

我遇到了麻烦
  1. 将名称填入数组,
  2. 程序在初始循环后不会提示输入名称。
  3. 这就是我所拥有的:

    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();
        }
    }
    

4 个答案:

答案 0 :(得分:1)

总是喜欢使用nextLine()读取输入,然后解析字符串。

使用next()只会返回空格之前的内容。返回当前行后,nextLine()会自动将扫描仪向下移动。

用于解析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();
        }
    }

答案 1 :(得分:0)

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

答案 2 :(得分:0)

这是你的意思吗

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();

答案 3 :(得分:0)

另一种方法,我发现更简单的是使用arraylist,以及在更改布尔值后断开的常规while循环。请参阅以下示例:

    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;}
    }