Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 数组、继承、方法?_Java_Arrays_Class_Methods - Fatal编程技术网

Java 数组、继承、方法?

Java 数组、继承、方法?,java,arrays,class,methods,Java,Arrays,Class,Methods,为什么我会出错 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;

为什么我会出错

    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
import java.util.*;
公共类实验室1{
公共静态扫描仪输入=新扫描仪(System.in);
公共静态int lengthOfList=10;
公共静态int[]等级列表=新int[lengthOfList];
公共静态整数搜索;
类getGrades{//此处出错
对于(int i=0;i
该程序要求用户输入数组长度(分数),然后要求分数,然后搜索输入的分数(例如,用户输入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.*;
公共类实验室1{
公共静态扫描仪输入=新扫描仪(System.in);
公共静态int lengthOfList=10;
公共静态int[]等级列表=新int[lengthOfList];
公共静态整数搜索;
公开考试成绩{
对于(int i=0;i
}

非常感谢大家,我刚刚在每个方法中添加了(),现在它可以工作了

 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.*;
公共类实验室1{
公共静态扫描仪输入=新扫描仪(System.in);
公共静态int lengthOfList=10;
公共静态int[]等级列表=新int[lengthOfList];
公共静态整数搜索;
公营机构职系({
对于(int i=0;i

}

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

试着像这样改变它们:<
public void getGrades() {
    //your code.
}
class getGrades{//error here
    //your code.
}
LabWork1 labwork = new LabWork1();
labwork.getGrades();