【发布时间】: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();
}
}
【问题讨论】:
-
因为您试图在类声明体中而不是在方法或初始化块中运行代码。