佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 716|回复: 2

请大家帮忙(java)

[复制链接]
发表于 28-1-2007 05:43 PM | 显示全部楼层 |阅读模式
以下是我做的,可是都compile不到。。。。请大家帮我纠正。。。。谢谢!!请大家帮帮忙。。。。明天就要交了


import java.io.*;
public class can
{
  
String candidate;
int vote;
double percentage;

public void readData()throws IOException
{
  InputStreamReader read=new InputStreamReader(System.in);
  BufferedReader input=new BufferedReader(read);
  
  System.out.print("Candidate";
  candidate=input.readLine();
  System.out.print("Votes Receive";
  vote=Integer.parseInt(input.readLine());
}

public void printInfo()
{
  System.out.println(candidate + "\t"+vote+"\t\t"+percentage);
}

public double calculateAverage()
{
  percentage=(vote/19300)/5;
}

public static void main(String[]args)throws IOException
{
  Student[] studDegree;
  studDegree=new Student[5];
  
  for(int i=0;i<studDegree.length;i++)
  {
   studDegree=new Student();
   studDegree.readData();
  }
  
  for(int i=0;i<studDegree.length;i++)
  {
   studDegree.calculateAverage();
   studDegree.printInfo();
  }
}
}


***********************************************************
以下是老师的问题和要的答案:


  • Write a program that allows the user to enter the names of five candidates in a local election and votes received by each candidates. The program should then output each candidate’s name, the votes received by that candidate, and the percentage of the total votes received  by the candidate. Your program should also output the winner of the election. A sample output is:

Candidates     Votes Received    % of Total Votes
   Asmah         5000             25.91
   Johan         4000             20.72
   Daud          6000             31.09
   Roslan        2500             12.95
   Ali           1800             9.33
   Jumlah        19300
   Pemenang ialah   Daud
回复

使用道具 举报


ADVERTISEMENT

发表于 28-1-2007 09:58 PM | 显示全部楼层
  1. import java.io.*;

  2. public class can
  3. {
  4.    
  5.     public static void main(String [] args) throws IOException
  6.     {
  7.         Student [] studDegree = new Student[5];
  8.         
  9.         for( int i = 0; i < studDegree.length; i++ )
  10.         {
  11.             studDegree[i] = new Student(i);
  12.         }
  13.         
  14.         System.out.println("");
  15.         System.out.println("Candidates\tVotes Received\t% of Total Votes");
  16.         
  17.         for ( int i = 0; i < studDegree.length; i++ )
  18.         {
  19.             studDegree[i].printInfo();
  20.         }
  21.         System.out.println("  Jumlah\t  " + Student.total);
  22.         System.out.println("  Pemenang ialah " + studDegree[Student.winner].candidate);
  23.     }
  24.    
  25. }

  26. class Student
  27. {
  28.     String candidate;
  29.     int vote;
  30.     double percentage;
  31.     static int total = 0;
  32.     static int max = 0;
  33.     static int winner;
  34.    
  35.     public Student(int i) throws IOException
  36.     {
  37.         InputStreamReader read = new InputStreamReader(System.in);
  38.         BufferedReader input = new BufferedReader(read);
  39.         
  40.         System.out.print("Candidate: ");
  41.         candidate = input.readLine();
  42.         System.out.print("Votes Receive: ");
  43.         vote = Integer.parseInt(input.readLine());
  44.         total += vote;
  45.         if ( vote > max )
  46.         {
  47.             max = vote;
  48.             winner = i;
  49.         }
  50.     }
  51.    
  52.     public void printInfo()
  53.     {
  54.         percentage = (double) vote / total * 100;
  55.         System.out.printf("  %-12s\t  %-6d\t  %.2f\n", candidate, vote, percentage);
  56.     }
  57.    
  58. }
复制代码
回复

使用道具 举报

 楼主| 发表于 28-1-2007 11:31 PM | 显示全部楼层
原帖由 MaokeJackson 于 28-1-2007 09:58 PM 发表
import java.io.*;

public class can
{
   
    public static void main(String [] args) throws IOException
    {
        Student ;
        
        for( int i = 0; i < studDegree.lengt ...



你真的好厉害。。。。如果我像你就好。。。。你真的是我的大恩人。。。。谢谢你
小妹我感激不尽。。。。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT


本周最热论坛帖子本周最热论坛帖子

ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 26-8-2025 08:23 PM , Processed in 0.148862 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表