【问题标题】:Array, Inheritance, Method?数组,继承,方法?
【发布时间】:2014-03-11 13:25:09
【问题描述】:

为什么会出错?

    import java.util.*;

    public class LabWork1 {
    public static Scanner input = new Scanner(System.in);
    public static int lengthOfList = 10;
    public static int [] gradeList = new int[lengthOfList];
    public static int search;

    class getGrades{//error here
        for(int i = 0 ; i < lengthOfList ; i++){
            System.out.print("Enter your grade: ");
            gradeList[i] = input.nextInt();
        }
    }
    class outputList{//error here
        for(int i = 0 ; i < lengthOfList ; i++){
            System.out.print(gradeList[i] + " ");
        }
    }
    class searchForGrade{//error here
        for(int i = 0 ; i < lengthOfList ; i++){
            if(gradeList[i] == search){
                System.out.println(gradeList[i] + " is located in index " +     i);
            }
            else if(gradeList[i] != search){
                System.out.println("Grade is not in index " + i);
            }
        }
    }
    class replaceGrade{//error here
        for(int i = 0 ; i < lengthOfList ; i++){
            if(gradeList[i] == search){
                System.out.println("Enter another grade: ");
                gradeList[i] = input.nextInt();
            }
            else if(gradeList[i] != search){
                    System.out.println("Grade is not in index " + i);
            }
        }
    }


    public static void main(String[] args){

        System.out.println("Enter the length of the list: ");
        lengthOfList = input.nextInt();

        new getGrades();
        new outputList();

        System.out.println("Enter the grade that you want to search: ");
        search = input.nextInt();
        new searchForGrade();

        new outputList();

        System.out.println("Enter the grade that you want to replace: ");
        search = input.nextInt();
        new replaceGrade();

        new outputList();
    }
}//error here

这个程序要求用户输入数组的长度(成绩的数量)然后询问成绩,然后搜索输入的成绩(例如用户输入 10 程序在数组列表中查找 10),然后询问用户替换等级,用用户输入的新等级替换等级。数组(等级)的列表应该每隔一个方法显示一次。

做了一些修改后还是报错

     import java.util.*;

  public class LabWork1 {
public static Scanner input = new Scanner(System.in);
public static int lengthOfList = 10;
public static int [] gradeList = new int[lengthOfList];
public static int search;

public void getGrades{
    for(int i = 0 ; i < lengthOfList ; i++){
        System.out.print("Enter your grade: ");
        gradeList[i] = input.nextInt();
    }
}
public void outputList{
    for(int i = 0 ; i < lengthOfList ; i++){
        System.out.print(gradeList[i] + " ");
    }
}
public void searchForGrade{
    for(int i = 0 ; i < lengthOfList ; i++){
        if(gradeList[i] == search){
            System.out.println(gradeList[i] + " is located in index " + i);
        }
        else if(gradeList[i] != search){
            System.out.println("Grade is not in index " + i);
        }
    }
}
public void replaceGrade{
    for(int i = 0 ; i < lengthOfList ; i++){
        if(gradeList[i] == search){
            System.out.println("Enter another grade: ");
            gradeList[i] = input.nextInt();
        }
        else if(gradeList[i] != search){
            System.out.println("Grade is not in index " + i);
        }
    }
}


public static void main(String[] args){

    System.out.println("Enter the length of the list: ");
    lengthOfList = input.nextInt();

    LabWork1 labwork = new LabWork1();
    labwork.getGrades();
    labwork.outputList();

    System.out.println("Enter the grade that you want to search: ");
    search = input.nextInt();
    labwork.searchForGrade();

    labwork.outputList();

    System.out.println("Enter the grade that you want to replace: ");
    search = input.nextInt();
    labwork.replaceGrade();

    labwork.outputList();
}

}

非常感谢你们,我刚刚将 () 添加到每个方法中,现在它可以工作了!

 import java.util.*;

public class LabWork1 {
public static Scanner input = new Scanner(System.in);
public static int lengthOfList = 10;
public static int [] gradeList = new int[lengthOfList];
public static int search;

public void getGrades(){
    for(int i = 0 ; i < lengthOfList ; i++){
        System.out.print("Enter your grade: ");
        gradeList[i] = input.nextInt();
    }
}
public void outputList(){
    for(int i = 0 ; i < lengthOfList ; i++){
        System.out.print(gradeList[i] + " ");
    }
}
public void searchForGrade(){
    for(int i = 0 ; i < lengthOfList ; i++){
        if(gradeList[i] == search){
            System.out.println(gradeList[i] + " is located in index " + i);
        }
        else if(gradeList[i] != search){
            System.out.println("Grade is not in index " + i);
        }
    }
}
public void replaceGrade(){
    for(int i = 0 ; i < lengthOfList ; i++){
        if(gradeList[i] == search){
            System.out.println("Enter another grade: ");
            gradeList[i] = input.nextInt();
        }
        else if(gradeList[i] != search){
            System.out.println("Grade is not in index " + i);
        }
    }
}


public static void main(String[] args){

    System.out.println("Enter the length of the list: ");
    lengthOfList = input.nextInt();

    LabWork1 labwork = new LabWork1();
    labwork.getGrades();
    labwork.outputList();

    System.out.println("Enter the grade that you want to search: ");
    search = input.nextInt();
    labwork.searchForGrade();

    labwork.outputList();

    System.out.println("Enter the grade that you want to replace: ");
    search = input.nextInt();
    labwork.replaceGrade();

    labwork.outputList();
}

}

【问题讨论】:

  • 因为您试图在类声明体中而不是在方法或初始化块中运行代码。

标签: java arrays class methods


【解决方案1】:

您正在声明一个新类而不是方法

尝试像这样改变它们:

public void getGrades() {
    //your code.
}

而不是

class getGrades{//error here
    //your code.
}

你还需要从调用方法中删除 new 关键字,你可以像这样调用它们:

LabWork1 labwork = new LabWork1();
labwork.getGrades();

关于 classesmethods 在 java 中的所有内容。

【讨论】:

  • 我把所有的类都改成了 public void 但我得到了一个新的错误
  • 您也应该从方法调用中删除 new 关键字。并使方法也成为静态的。阅读方法/类页面!
  • @MarcoAcierno 感谢您的提示,user3406298 请参阅上面的更新。
  • 添加后LabWork1 labwork = new LabWork1();实验室工作。一切仍然有错误
【解决方案2】:

您收到错误是因为您将代码放在任何方法之外的嵌套类的主体中。然后在 main 方法中调用嵌套类的构造函数。我认为您应该使用普通方法而不是 Salah 建议的嵌套类。另外,请记住删除“新”关键字。

只是好奇,Java 是您的第一种编程语言,还是您从其他语言中获得的?

【讨论】:

  • 这是我的第一个编程语言。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-01
  • 2012-04-25
  • 2011-03-03
  • 2011-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多