回 帖 发 新 帖 刷新版面

主题:请帮忙指出错误!

public class Test{
 public void main(String[] arg){
   bollean isValid=0;
   int scores[5]={65,70,69,98,86};
   if(isValid) then {
     System.out.println(scores[5]);}
   else {
     System.out.println("No informaton");} 
   }
}
请帮忙找出以上程序的错误,题目说有五处错误.

回复列表 (共1个回复)

沙发

public class Test{  
 public void main(String[] arg){   //主函数必须是静态的
   bollean isValid=0;               //bollean拼写错误和赋值错误
   int scores[5]={65,70,69,98,86};  //数组初始化错误
   if(isValid) then {               //if 语句错误
     System.out.println(scores[5]);}  //数组下标越界
   else {
     System.out.println("No informaton");} 
   }
}

//以下是正确的 
public class Test{  
 public static void main(String[] arg){   
   boolean isValid=false;               
   int scores[]=new int[]{65,70,69,98,86};  
   if(isValid)  {               
     System.out.println(scores[4]);}  
   else {
     System.out.println("No informaton");} 
   }
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册