繁体   English   中英

Do-While循环填充数组

[英]Do-While loop to fill out Array

我目前正在做一个要求我设置数据输入的项目。 在数据输入模式下,将要求用户循环输入科学家数据。 如果用户回答“ y”,将提示他们输入另一位科学家。 我认为最好的方法是使用do-while循环填充数组,直到用户决定结束为止。 我有麻烦

  1. 将名称填充到数组中,以及
  2. 初始循环后,程序将不会提示您输入名称。

这是我所拥有的:

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

总是喜欢使用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();
        }
    }

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

这是你的意思解决方案

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

我发现更简单的另一种方法是使用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;}
    }

暂无
暂无

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

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