簡體   English   中英

Java掃描器跳過迭代

[英]Java scanner skipping iterations

我已經在一個問題上停留了一段時間,該程序沒有執行我認為應該做的事情。

當我運行該程序並到達要求您輸入課程名稱的部分時,該程序將跳過第一次迭代,或跳過其他迭代,具體取決於輸入了多少門課程。 它僅允許在最后一次迭代中輸入。 對於以下for循環,程序將跳過它們,而無需允許輸入。

我的問題是, for循環不正確還是String數組未將信息正確輸入到其下標?

    import java.util.Scanner;           //Needed for Scanner class
    public class StudentRecords
    {
    public static void main(String[] args)
    {
    int courses;
    int students;
    int[] course = new int[5];
    int[] student = new int[5];
    double GPA = 0;
    String[] courseNumber = new String[5];
    double[] creditHours = new double[5];
    String[] letterGrade = new String[5];

    //Scanner object for user input
    Scanner kb = new Scanner(System.in);

    System.out.println("This program will help you determine the GPA \n"
                        + "for each student entered.");
    System.out.println("");

    System.out.println("How many student's GPA are you going to calculate?");
    System.out.print("Enter amount of students (Maximum of 5 students): ");
    students = kb.nextInt();
    student = new int[students];

    System.out.println("");

    for(int index = 0 ; index < student.length; index++)
    {
        System.out.print("Student " + (index + 1) + " information: ");
        System.out.println("");

        System.out.print("How many courses did student " +
                            (index + 1)  + " take? ");
        courses = kb.nextInt();
        course = new int[courses];

        for(int i = 0; i < course.length; i++)
        {
            System.out.println("What is the name of course #" + (i + 1));
            courseNumber[i] = kb.nextLine();
        }

        for(int i = 0; i < course.length; i++)
        {
            System.out.println("How many credit hours is " + courseNumber[i]);
            creditHours[i] = kb.nextDouble();
        }

        for(int i = 0; i < course.length; i++)
        {
            System.out.println("What is the final letter grade for " + courseNumber[i]);
            letterGrade[i] = kb.nextLine();
        }

        for( i = 0; i < student.lenght<
    }
    }
 }

PS:這是我正在研究的問題:

用以下輸入編寫程序,所有輸入都存儲在數組中(大小5)。 首先,該學期學生修了多少門課程(不能超過5門)。 將每個學生的課程號/名稱(例如ICT 435),學分(1-4)和字母成績(AF)存儲在陣列中。 確定該學期的GPA。

nextLine() Scanner方法可能很奇怪。 我參加的一門Java入門課程中提到,在檢索數字的方法(例如, nextDouble() )之后,該行的末尾有一個換行符。 下次使用nextLine() ,它將讀取該新行字符作為輸入,而沒有給您鍵入任何內容的機會。

如果您在循環之前放置nextLine()詢問課程名稱,

kb.nextLine(); // <-- here
for(int i = 0; i < course.length; i++)
{
    System.out.println("What is the name of course #" + (i + 1));
    courseNumber[i] = kb.nextLine();
}

它將通過新行。 任何后續的nextLine()調用實際上都將使您提供輸入。 也就是說,您還需要在字母評分循環之前執行此操作,

kb.nextLine(); // <-- here
for(int i = 0; i < course.length; i++)
{
    System.out.println("What is the final letter grade for " + courseNumber[i]);
    letterGrade[i] = kb.nextLine();
}

因為您也需要在此循環之前輸入數字。

這對我有用。 希望能幫助到你!

嘗試使用:

courseNumber[i] = kb.next();

letterGrade[i] = kb.next();

在掃描字符串時的for循環中。 代替

courseNumber[i] = kb.nextLine();

letterGrade[i] = kb.nextLine();

請參閱此鏈接以獲取更多詳細信息:

暫無
暫無

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

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