意想不到的结果 - 可以取悦一点错误

时间:2013-12-30 18:36:50

标签: java arrays

花了几个小时尝试不同的东西,我无法弄清楚我的代码有什么问题,这是一个简单的程序:`

public class AssignGrades {

private int ntotal=0;
private int []y;

//constructor to initialize class instances
AssignGrades(int t)
{
    ntotal = t;
    //y = num1;

}   
AssignGrades( int []num1)
{
    y=num1;
    for (int i=0;i<y.length;i++)
    y[i] = num1[i];

}   
//method to sort grades int []num1

void setGrades()
{
    int [] y = new int[ntotal];
    for (int i=0;i<y.length;i++)

    {

        //assign grades 
        if 
            (y[i]<80){
        System.out.println("grade is A" +y[i]);}
            else if (y[i]<70)
            System.out.println("grade is B" +y[i]);
           else if (y[i]<60)
            System.out.println("grade is c" +y[i]);
           else
             System.out.println("FAIL" +y[i]);
      } 
          }

//show student grades - to print array[] values 
void  showGrades()
{
    for (int u: y)
    System.out.println(u);
}

}`

我的客户端程序

`import java.util.Scanner;


 public class AssignGradesDemo {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    int t=0;

    System.out.println("enter no of students" );

    Scanner input = new Scanner(System.in);
    {
        t=input.nextInt();          
    //  input.close();
    }


    int [] num1 = new int[t];
    System.out.println("enter grades");

    Scanner input1 = new Scanner(System.in);
    for (int i=0;i<num1.length;i++)
    {
        num1[i]=input1.nextInt();           
    }
    input1.close();

    AssignGrades ag = new AssignGrades(t);
    AssignGrades ag1 = new AssignGrades( num1);

    ag.setGrades();
    ag1.showGrades();

}


}

输出是:

enter no of students
2
enter grades
78
98
grade is A0
grade is A0
78
98

问题:现在输出'A'和'0' - &gt;问题所在,看起来数组未初始化,所有值都显示为零:而当我单独打印它们时,它们会被初始化。?!

如果需要进一步澄清,请告诉我。感谢

1 个答案:

答案 0 :(得分:3)

你有一个名为y的局部变量和一个名为y的类变量。这似乎是个问题。你正在使用本地y,但我想你想使用y类。

好的,你的代码有很多问题。这是固定版本。

public class AssignGrades {

    private int[] y;

    public AssignGrades(int[] num1) {
        y = num1;
    }

    // method to set grades
    void setGrades() {
        for (int i = 0; i < y.length; i++)
        {
            // assign grades

            if (y[i] < 50)
                System.out.println("FAIL" + y[i]);
            else if (y[i] < 60)
                System.out.println("grade is C" + y[i]);
            else if (y[i] < 70)
                System.out.println("grade is B" + y[i]);
            else if (y[i] < 80) {
                System.out.println("grade is A" + y[i]);
            }

        }
    }

    // method to show student grades
    void showGrades() {
        for (int u : y){
            System.out.println(u);
        }
    }

}




import java.util.Scanner;

public class AssignGradesDemo {

    public static void main(String[] args) {
        int t = 0;
        System.out.println("enter no of students");

        Scanner input = new Scanner(System.in);
        t = input.nextInt();

        int[] num1 = new int[t];
        System.out.println("enter grades");

        for (int i = 0; i < num1.length; i++) {
            num1[i] = input.nextInt();
        }
        input.close();

        AssignGrades ag = new AssignGrades(num1);

        ag.setGrades();
        ag.showGrades();
    }

}