簡體   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