找到数字的平均值

时间:2012-06-17 15:18:23

标签: java

一个简单的java程序,用于输入学生的姓名,并找到他获得的三个科目的平均分数

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


class MyException extends Exception
{

    MyException(String s)
    {
        super(s);
    }
}

class Student 
{
    String name;

    String inputName()
    {
        return name;
    }

    void average(int [] a)
    {
        int d;

        if ((a.length)==3)
        {
            d = (a[0]+a[1]+a[2])/3;
            if (d>50)
                System.out.println(" Congratulations!!! "+name+ " 
                        you have passed the examination");
            else
            System.out.println(" Oops  " +name+"  Try Later!!");
        }
    }

public static void main(String x[]) throws IOException
{
    Student s= new Student();
    int args[] =new int[3];
    System.out.println("Enter name of the student:");
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    s.name=br.readLine();
    System.out.println("Name of the student is " +s.inputName().trim());
    System.out.println("Marks in Physics = ");
    args[0]=Integer.parseInt(br.readLine().trim());
    System.out.println("Marks in Chemistry = ");
    args[1]=Integer.parseInt(br.readLine().trim());
    System.out.println("Marks in Mathematics = ");
    args[2]=Integer.parseInt(br.readLine().trim());**$$**
    s.average(args);**##**
}
}

代码没有错误。代码执行到 $$ 步骤,但 ## 指示的步骤没有执行。我不知道为什么??

1 个答案:

答案 0 :(得分:2)

以下是测试运行的输出:

Enter name of the student:
hello
Name of the student is hello
Marks in Physics = 
52
Marks in Chemistry = 
52
Marks in Mathematics = 
52
 Congratulations!!! you have passed the examination

因此,该方法实际上正在执行,但是您没有看到任何输出的原因,就像其他人指出的那样,不满足以下条件

if (d > 51)

另外,按照以下方式添加其他部分会很好:

if (d > 51)  {
    System.out.println("Congratulations!!! you have passed the examination");
} else {
    System.out.println("Sorry!!! you have failed the examination");
}