繁体   English   中英

Java循环的一个简单程序问题

[英]A simple program issue with java loops

我对我的代码有一个简单的问题。 我对Java很陌生,尝试自我学习,但是我现在陷入了循环。 在我看来,这应该可行。 问题也是要问一些学生,然后让用户输入每个学生的姓名和分数。 然后,它应该显示得分最高的学生和第二名的学生。 出于某种原因,我的代码仅显示了我输入的第一名和第二名的名字和分数。 我可能犯了一个大错误,但也许有人可以指出我正确的方向吗? 抱歉,这看起来像是一团糟。 :(

public class Chapter4_9 {

  public static void main(String[] args) {

    //scanner for input
    Scanner input = new Scanner(System.in);

    //ask user for number of students
    System.out.print("Enter the number of students: ");
    int numberStudents = input.nextInt();

    //declare variables
    double highestScore = 0;
    double tempScore = 0;
    double secondHighestScore = 0;
    String firstStudent = "";
    String tempStudent = "";
    String secondStudent = "";

    for (int i = 0; numberStudents != i; ++i) {
        System.out.print("Enter the students name followed by his score: ");
        String studentName = input.next();
        double studentScore = input.nextDouble();

        if (i == 0){
            firstStudent = studentName;
            highestScore = studentScore;
        }

        else if (studentScore > highestScore) {
            tempStudent = firstStudent;
            studentName = firstStudent;
            secondStudent = tempStudent;
            tempScore = highestScore;
            studentScore = highestScore;
            secondHighestScore = tempScore;
        }


    }   
    System.out.println("The highest scoring student is " + firstStudent + " with a " + highestScore);
    System.out.println("The second highest scoring student is " + secondStudent + " with a " + secondHighestScore);

}
}

这个块似乎有点混乱:

else if (studentScore > highestScore) {
    tempStudent = firstStudent;
    studentName = firstStudent;
    secondStudent = tempStudent;
    tempScore = highestScore;
    studentScore = highestScore;
    secondHighestScore = tempScore;
}

此阻止的预期结果是什么? 当您再也无法再次读取studentNamestudentScore的值时(在从用户那里读取新值之前),为什么要覆盖它们的值?

大概的目的是用最高的分数/名称替换第二个分数/名称,然后用当前输入替换最高的分数/名称-但这根本不是代码所要做的。 这样做:

secondStudent = firstStudent;
secondScore = highestScore;
firstStudent = studentName;
highestScore = studentScore;

完全不需要临时变量。

但是,仅此更改是不够的。 需要考虑新分数不高于当前最高分数,但高于当前第二最高分数的情况。 我让你去解决需要什么...

顺便说一句,如果为“名称/分数”组合引入了单独的类,例如Student ,则代码可能会更简单。 然后,您将没有并行变量-只需担心topStudentsecondStudentcurrentStudent即可。

当您发现分数更高时,您的代码是错误的。

secondStudent = fistStudent; // what used to be high score is now 2nd
firstStudent = studentName;
// score adjustment left for you to do ;)

代码中有一个错误,并且您没有涵盖所有内容。

在for循环中,您必须具有以下这些:

else if (studentScore > highestScore) {
        secondStudent = firstStudent;
        firstStudent = studentName;
        secondHighestScore = highestScore;
        highestScore = studentScore;
    }
else if (studentScore < highestScore && studentScore > secondHighestScore) {
        secondStudent = studentName;
        secondHighestScore = studentScore;
    }

做完了

您的逻辑不正确。 您没有处理所有方面。 如果检查您的代码,它将仅能正确处理第一个输入,而这完全取决于您提供输入的方式。 您的逻辑有待改进。 不需要那么多的临时变量。

最好在调试模式下运行您的应用程序,然后逐步进入它,以便知道它出了什么问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM