统计信息

  • 访问数:11206
  • 博客数:142
  • 建立时间:2008-01-05
  • 更新时间:2008-05-21
我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!

P3Q9

2008-01-31 10:00:00

天气: 晴朗 心情: 高兴

/*P3Q9
 * Wang Chaoran
Description:9 (Finding the two highest scores)
Write a program that prompts the user to enter the number of students
and each student's name and score, and finally displays the student with
the highest score and the student with the second-highest score.
*/
import java.util.Scanner;
public class P3Q9
{
  public static void main(String[] args)
  {
    //Create a scanner
    Scanner scan = new Scanner(System.in);
    System.out.println("Please enter the number of students: ");
    int number = scan.nextInt();
    double highestScore=-1,secondHighestScore=-1;
    double score=-1;
    String name1="",name2="",name3="";
    //Read in name and score
    for(int i=1; i<number+1;i++)
    {
    System.out.println("Please enter the students name: ");
    name1 = scan.next();
    System.out.println("Please enter the score of "+ name1+": ");
     score = scan.nextDouble();
    if(score >=highestScore)
    {
     secondHighestScore = highestScore;
     name3 = name2;
     highestScore = score;
     name2 = name1;
        
    }
    else if (score >=secondHighestScore)
    {
      name3 = name1;
      secondHighestScore=score;
    }
     }
    //Display the answer
     System.out.println(name2+" is the one with the highest score of "+highestScore+"\n"
                          +name3+" is the one with the second highest score of "+secondHighestScore);
  }
}

加入收藏 编辑 审核

TAG: computing

我来说两句

OPEN

Powered by X-Space 1.2 © 2001-2006 Comsenz Technology Ltd