Java:用于循环和布尔编码

时间:2014-03-12 02:42:17

标签: java loops for-loop boolean-expression

我正在尝试编写一个Java程序,要求用户输入学生人数和他们的每个姓名和分数,以及打印出分数最高的两个学生。我几乎全部,但我无法获得第二高分。我所拥有的将打印出最高分和第三高分(以5名学生作为输入)。

这是我的代码的一部分(我不想延长它,所以我省略了首先说明方法的开始部分,以及导入扫描仪。我也遗漏了最后一个打印声明打印出两个学生和他们的分数。 我认为问题在于if语句接近结束时,比较分数是什么,但我不知道如何使它成为第二高而不是第三高。

//Prompt user to enter in number of students, each student's name and score
  System.out.println("Enter the number of students: ");
  int numberstudents = input.nextInt();

System.out.println("Enter the student's name: ");
String name1 = input.next();
System.out.println("Enter the student's score: ");
double score1 = input.nextDouble(); 

String name2 = " ";
double score2 = 0;

//Use a for loop
  for (int data = 0; data < numberstudents - 1; data++){
    System.out.print("Enter the student's name: ");
    String name = input.next();
    System.out.print("Enter the student's score: ");
    double score = input.nextDouble();

//Find which scores are the highest
    if (score > score1) {
    name1 = name;
    score1 = score;
}
    else if (score > score2 && score1 > score) {
    name2 = name;
    score2 = score;
} 

如果你能帮助我,非常感谢你,即使你不能,也要感谢你花时间阅读这篇文章。

4 个答案:

答案 0 :(得分:0)

用以下内容替换你的双倍:

if (score > score1) {
    name2 = name1; // store previous max score/name as second
    score2 = score1;

    name1 = name; // store current max score/name as first
    score1 = score;
} else if (score > score2) {
    name2 = name; // current score/name is not max, but can be second max
    score2 = score;
}

答案 1 :(得分:0)

class Student {
    String name;
    double score;
}

LinkedList<Student> students = new LinkedList<Student>();

//then you can have
students.add(new Student(input.next(), input.nextDouble());

Student max;
Student max2;
double score = 0.0;
for (Student s : students) if (s.score >= score) max = s;
students.remove(max);
score = 0.0;
for (Student s : students) if (s.score >= score) max2 = s;

//The top 2 scores are max.score and max2.score

答案 2 :(得分:0)

您可能希望更改逻辑以首先检查它是否大于得分2,如果是,则存储它。在那之后,你可能应该检查它是否大于score1,如果是,那么你应该交换score1和score2。你现在这样做的方式如果出现新的高分,就会丢掉最高分,即使旧的高分应该被“降级”到第二高。

由于这是作业,我只会给伪代码......

if score > score2:
  score2 = score
if score > score1:
  oldScore1 = score1
  score1 = score
  score2 = oldScore1

如果你愿意的话,你可以对它进行改进并使其更加优雅。

答案 3 :(得分:0)

试试这个!你可以获得任何学生分数,并打印第一和第三高分。

    package testing;

import java.util.Scanner;

public class Jpro {


@SuppressWarnings("resource")
public static void main(String args[])
{
    Scanner in=new Scanner(System.in);
    System.out.println("Enter the no. of Students");
    int noOfStudents=in.nextInt();
    String name[]=new String[noOfStudents];
    Double scores[]=new Double[noOfStudents];
    for(int i=0;i<noOfStudents;i++)
    {
            System.out.println("Enter the Student Name:");
            name[i]=in.next();
            System.out.println("Enter the Score of Student:");
            scores[i]=in.nextDouble();
    }
    for(int i=0;i<noOfStudents;i++)
    {
        for(int j=0;j<noOfStudents;j++)
            if(scores[i]>scores[j])
            {
                Double temp=scores[i];String tName=name[i];
                scores[i]=scores[j];name[i]=name[j];
                scores[j]=temp;name[j]=tName;
            }
    }
    System.out.println("1st Highest Scores Name:"+name[0]+"\tScore: "+scores[0]);
    System.out.println("3rd Highest Scores Name:"+name[2]+"\tScore: "+scores[2]);
}

}