当前位置: 代码迷 >> Java面试 >> 以下java程序有什么东东?求解答
  详细解决方案

以下java程序有什么东东?求解答

热度:31   发布时间:2016-04-17 19:25:53.0
以下java程序有什么错误?求解答
下面程序有什么错误?请高手解答哈谢了
这道题主旨是输入五位同学成绩(0——100)求总分,平均分之类的,下面这个程序可以输入数据但就是不能用方法求出总数,平均分等等


package homeWork;

import java.util.*;
import javax.swing.JOptionPane;

public class fivestudentgrade {

private static double[] score;

public static void main (String[] args){

System.out.println("请输入五位学生的成绩:");

double [] score=new double[5];
for(int i=0;i<5;i=i+1)
  {
  Scanner scanner=new Scanner(System.in);
  double a=scanner.nextDouble();
if(a<0||a>100)
  System.out.println("Plase enter again");
  else
  score[i]=a;
  }


int choice = mainMenu();
while(choice!= 5)
{
switch(choice)
{
case 1:
sumGrade();
break;

case 2:
averageGrade();
break;

case 3:
highGrade();
break;

case 4:
lowestGrade();

default:
System.out.println("Not a valid option, select again");
}

}
}


public static int mainMenu()
{
 
JOptionPane.showMessageDialog( null,"1. sumgrade"+"\n"+
"2. averagegrade"+"\n"+"3. highestgrade"+"\n"+
"4. lowestgrade"+"\n"+"5. Exit");

/* System.out.println("1. sumgrade");
System.out.println("2. averagegrade");
System.out.println("3. highestgrade");
System.out.println("4. lowestgrade");
System.out.println("5. Exit");
*/

System.out.println("Enter an option ");

Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
return i;
}
 
 
public static void sumGrade()
{
double sumGrade=0.0;
for(int i=0;i<5;i++)
{
sumGrade=score[i]+sumGrade;
}
System.out.println("sumGrade="+sumGrade);
}
 
 
public static void averageGrade()

{
for(int i=0;i<5;i++)
{
double average=0.0;
average=score[i]/5+average;
}
}
 
 
public static void highGrade()
{
double maxgrade=0.0;
for(int i=0;i<5;i++)
{
if(score[i]>maxgrade)maxgrade=score[i];
}
System.out.println("maxgrade="+maxgrade);
}
 
 
public static void lowestGrade()
{
double mingrade=0.0;
for(int i=0;i<5;i++)
{
if(score[i]<mingrade)mingrade=score[i];
}
System.out.println("mingrade="+mingrade);
}
 
}


------解决方案--------------------
Java code
package com.exchange;import java.util.Scanner;import javax.swing.JOptionPane;public class Fivestudentgrade {    private static double[] score;    public static void main(String[] args) {        System.out.println("请输入五位学生的成绩:");        score = new double[5];        for (int i = 0; i < 5; i = i + 1) {            Scanner scanner = new Scanner(System.in);            double a = scanner.nextDouble();            if (a < 0 || a > 100)                System.out.println("Plase enter again");            else                score[i] = a;        }        int choice = mainMenu();            switch (choice) {            case 1:                sumGrade();                break;            case 2:                                averageGrade();                break;            case 3:                highGrade();                break;            case 4:                lowestGrade();                break;            case 5:                System.exit(1);            default:                System.out.println("Not a valid option, select again");            }        }        public static int mainMenu() {        JOptionPane.showMessageDialog(null, "1. sumgrade" + "\n"                + "2. averagegrade" + "\n" + "3. highestgrade" + "\n"                + "4. lowestgrade" + "\n" + "5. Exit","菜单",JOptionPane.PLAIN_MESSAGE);        System.out.println("Enter an option ");        Scanner scanner = new Scanner(System.in);        int i = scanner.nextInt();        return i;    }    public static void sumGrade() {        double sumGrade = 0.0;        for (int i = 0; i < 5; i++) {            sumGrade = score[i] + sumGrade;        }        System.out.println("sumGrade=" + sumGrade);    }    public static void averageGrade()        {        double average = 0.0;        for (int i = 0; i < 5; i++) {            average = score[i] / 5 + average;        }        System.out.println("average="+average);    }    public static void highGrade() {        double maxgrade = 0.0;        for (int i = 0; i < 5; i++) {            if (score[i] > maxgrade)                maxgrade = score[i];        }        System.out.println("maxgrade=" + maxgrade);    }    public static void lowestGrade() {        double mingrade = 0.0;        for (int i = 0; i < 5; i++) {            if (score[i] < mingrade)                mingrade = score[i];        }        System.out.println("mingrade=" + mingrade);    }}
  相关解决方案