簡體   English   中英

如何從傳入的掃描儀中讀取數組元素的值?

[英]How do I read in values for the elements of an array from a Scanner passed in?

對於這個簡單的程序,我們有2個類可以使用MyDateDueDates (當然還有main類)。 初始化MyDateprivate MyDate [] dueDates = new MyDate[10]的對象數組后,一個特定的函數要求我們“從傳入的掃描程序中讀取數組中日期的值”。

我對此非常困惑,因為我 - 一個弱的程序員 - 只使用Scanner來處理像System.in之類的東西。 這不是我不理解的; 我怎么能讀到這些價值觀? 因為我無法使用input.next()或者不能使用類型為Class的數組。

public class DueDates {
    private MyDate[] dueDates ;

    public DueDates() {
        //*****  write the code for this method here
        dueDates = new MyDate [10];
    }

    //set array to have size of parameter passed in
    public DueDates(int max) {
        //*****  write the code for this method here
        dueDates = new MyDate[max];
    }

    /*reads in values for the dates in the array from the Scanner passed in ------having issues here*/
    public boolean inputDueDates(Scanner in) {
        //*****  write the code for this method here
        in = new Scanner(System.in);
        dueDates = in.  // I'm lost at this point
        return true;
    }
}
public class MyDate {
        private int day = 1;
        private int month = 1;
        private int year = 2018;

        public MyDate() {
        }

        public String toString() {   
            return new String ("" + year + "/" + month + "/" + day);
        }

        public boolean inputDate(Scanner in) {
            month = 0;
            day = 0; 
            year = 0;
            do {

                System.out.print ("Enter month - between 1 and 12: ");
                if (in.hasNextInt())
                    this.month = in.nextInt();
                else {
                    System.out.println ("Invalid month input");
                    in.next();
                }
            } while (this.month <= 0 || this.month > 12);

            do {

                System.out.print ("Enter day - between 1 and 31: ");
                if (in.hasNextInt())
                    this.day = in.nextInt();
                else {
                    System.out.println ("Invalid day input");
                    in.next();
                }
            } while (this.day <= 0 || this.day > 31 || (this.month == 2 && this.day > 29) || (this.day > 30 && (this.month == 9 ||this.month == 4 ||this.month == 6 ||this.month == 11) ) );

            do {
                System.out.print ("Enter year: ");
                if (in.hasNextInt())
                    this.year = in.nextInt();
                else {
                    System.out.println ("Invalid day input");
                    in.next();
                }
            } while (this.year <= 0);

            return true;        
        }```
^^^^is what was given to us, im not sure if the prof forgot to complete the constructor or simply it needs to be empty as is, but hope this clears it up more.

I can provide the other class if need be, and/or the sample output, but I'm genuinely just stuck at this point, sorry and thanks.

我將假設MyDate構造函數是: Date(int day, int month, int year){...}

現在您需要初始化10個日期,因此您可以使用for循環:

      for(int i=0;i<10;i++){
        int day = in.nextInt();
        int month = in.nextInt();
        int year = in.nextInt();

        dueDates[i]=new MyDate(day, month, year);
      }

'從傳入的掃描程序中讀取數組中日期的值'只是意味着將已經實例化的掃描程序對象傳入方法並使用它來讀入用戶輸入。

方法簽名可能如下所示:

public boolean doTheThing(Scanner sc){
    //Use a loop to read in user input with sc
    //return true/false 
}

“傳入”部分指的是方法簽名的Scanner sc參數。

我們來看看掃描程序的javadoc:

特別感興趣的是:

  1. hasNextInt()
  2. nextInt()

使用hasNextInt()您可以檢查輸入流掃描程序是否有int。 然后你可以使用nextInt()來抓住它。 這只適用於獲得一個這樣的int,所以你需要將它包裝成一個循環。

我希望這有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM