java - for 循环类分配出错

标签 java for-loop

我在大学上课,我们正在学习 Java。这只是我的第四节课,所以我是 super 新人(友善点)。我的问题,希望我唯一的问题是,这实际上会运行,但是在要求用户输入您想要输入的学生成绩数量之后。然后它进入 for 循环并同时询问接下来的两个问题,然后我得到一个错误。我正在尝试弄清楚如何让它单独提出问题,但我没有任何运气。有人建议使用 io.console,但我认为我们不被允许使用它,我们还没有学会它。我遇到了 hasNext 但我不太确定它是如何工作的,而且我读得越多,它就越让我困惑。

非常感谢任何帮助!

 
/*Write a java program that prompts the user to enter the number of students and then each student’s name and score, 
 * and finally displays the student with highest score and the student with the second- highest score. 
 * You are NOT allowed to use ‘Arrays’ for this problem (as we have not covered arrays yet).
 * 
 * HINT: You do not need to remember all the inputs. You only need to maintain variables for max and second max
 *  scores and corresponding names. Whenever you read a new input, you need to compare it to the so far established 
 *  max & second max scores and change things accordingly. */<p></p>

<p>import java.util.Scanner;</p>

<p>public class StudentScore {</p>

public static void main(String[] args) {

    String studentName="", highName="", secondHighName="";
    int score=0, highScore=0, secondHighScore=0;
    int count;
    int classSize;

    Scanner scan = new Scanner(System.in);

    System.out.print("How many students' grades do you want to enter? ");
    classSize = scan.nextInt();

    for (int i = 0; i < classSize.hasNext; i++)  {
        System.out.print("Please enter the students name? ");
        studentName = scan.hasNextLine();

        System.out.print("Please enter the students score? ");
        score = scan.nextInt();
    }
        if (score >= secondHighScore) {
            secondHighScore = highScore;
            secondHighName = highName;
            highScore = score;
            highName = studentName;

        }
    }   

    System.out.print("Student with the highest score: " + highName + " " + highScore);
    System.out.print("Student with the second highest score: " + secondHighName + " " + secondHighScore);

}

}

最佳答案

首先,您需要检查收到的分数是否大于第二个分数,以及该分数是否大于最高分数。其次,将 studentName = scan.hasNextLine() 替换为 studentName = scan.nextLine()。同时创建一个新的扫描仪

代码:

public static void main(String[] args) {

    String studentName="", highName="", secondHighName="";
    int score=0, highScore=0, secondHighScore=0;
    int classSize;

    Scanner scan = new Scanner(System.in);

    System.out.println("How many students' grades do you want to enter? ");
    classSize = scan.nextInt();

    for (int i = 0; i < classSize; i++)  {
        System.out.println("Please enter the student #" + (i + 1) + "'s name? ");
        //new Scanner plus changed to nextLine()
        scan = new Scanner(System.in);
        studentName = scan.nextLine();

        System.out.println("Please enter the student #" + (i + 1) + " score? ");
        score = scan.nextInt();
        if(score >= highScore){
            secondHighName = highName;
            secondHighScore = highScore;
            highName = studentName;
            highScore = score;
        } else if(score >= secondHighScore && score < highScore){
            secondHighName = studentName;
            secondHighScore = score;
        }
    }
    scan.close();   
    System.out.println("Student with the highest score: " + highName + " " + highScore);
    System.out.println("Student with the second highest score: " + secondHighName + " " + secondHighScore);

}

关于java - for 循环类分配出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24876440/

相关文章:

java - Java 的 MD5 哈希问题

Java - 'Finally' 相当于 if 语句

java - if 语句中的条件

python - python中的for循环不操作原始对象,而是执行深复制

javascript - Django按钮类forloop触发javascript

javascript - 定位测验中的按钮组,而不是点击时的所有测验问题

sql - ORA-01427 : single-row subquery returns more than one row in Update Statement

java - JSP 中的 ReCaptcha

c++ - 在 C++ 'for' 循环中声明用户定义的类类型

java - Maven 编译时抛出 IllegalCharsetNameException