【问题标题】:for loop class assignment gone wrongfor 循环类分配出错
【发布时间】:2014-07-21 23:18:54
【问题描述】:

我在大学上课,我们正在学习 Java。这只是我的第四节课,所以我是超级新手(很好)。我的问题,希望我唯一的问题是这将实际运行,但是在要求用户输入您想要输入的学生成绩后。然后它进入 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. */

import java.util.Scanner;

public class StudentScore {

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

}

【问题讨论】:

  • classSizeintclassSize.hasNext 可能是什么?我认为您需要回到课程所用的书并在那里找到一些代码示例。
  • hasNextLine 不返回另一行,它测试是否还有另一行。所以它返回一个boolean,而不是String。另请参阅我对stackoverflow.com/questions/24871872/… 的回答,因为您即将遇到同样的问题。
  • 哦,好吧,我想我现在明白了。你有没有机会给我一个简单的例子来说明 hasNextLine 是如何工作的?另外,我正在阅读你的另一篇文章,看看你在说什么其他问题。谢谢!

标签: java for-loop


【解决方案1】:

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

代码:

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

}

【讨论】:

  • 好的,所以添加新的 scan=new Scanner(system.in) 将允许单独提出问题?另外,为什么打印语句中的 i + 1 ? for 循环中的 i++ 不会处理吗?此外,这是您唯一需要使用 scan.close(); 的时间吗?也非常感谢大家的帮助!
  • i + 1 是为了更容易阅读输出,因为第一个学生是学生#0,第二个学生是学生#1,这会造成混淆。至于关闭扫描仪,它不应该导致资源泄漏,但比抱歉更安全。所以你应该在 for 循环结束时关闭它。
  • 第二个 if 中的 "&& score if的 else 部分>,所以它不大于或等于highscore,因此必须小于highscore。
  • 这并没有什么不同。
猜你喜欢
  • 1970-01-01
  • 2012-06-01
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-24
  • 2020-12-22
相关资源
最近更新 更多