for ( int count = 1; count <= 31 ; count++ )数组越界了把count <= 31 改为 count < stuResult.length
解决方案 »
- 用Math.random()产生13个0~51没有重复的随机数,放入suit数组中
- new Runnable()的问题
- CronExpression中如何配置每个月最后2个工作日?
- 不解: 用数组初始化jTable时,为什么在jTable输出之后,数组的改变还能起作用....
- 求JTREE的一段代码
- AbsHandle handle = new ProductInfo();AbsHandle handle = ProductInfo.getInstance();这两句有什么区别吗?
- 有谁用过二维向量,给我讲讲,有例子的最好!拜托,非常感谢!
- 关于初始化XML的问题~
- 如何在TOMCAT4.0以上加入.JAR包?
- Java新手紧急求助!!!
- 数据库连接问题,各位高手,帮我一把啊~~~~~~~~
- helloworld能通过,其它程序有java.lang.NoSuchMethodError???
这一句有问题,
你把它改为for ( int count = 1; count <= 30 ; count++ )即可!此为数组溢出问题,在JAVA,C,C++等大多数语言中,
数组下标皆从0算起。
如 int [] intArray = new int{31];
intArray数组有31个元素,从 intArray[0]到intArray[30].你这种问题应该用OOP做!
定义一个Student 类,这样才叫JAVA程序!
你也不把错误贴出来~
运行期错误有很多种的!!
所以stuNumber的下标范围是0-30所以
for ( int count = 1; count <= 31 ; count++ )
i=31时就越界了可以这样;int stuResult[] = new int[31] ;
int stuNumber[] = new int[31] ;
改成
int stuResult[] = new int[32] ;
int stuNumber[] = new int[32] ;或者
for ( int count = 1; count <= 31 ; count++ )
改成
for ( int count = 0; count < 31 ; count++ )
for ( int count = 1; count <= 31 ; count++ )这样,第一个数据不就丢了??
import javax.swing.* ;
import java.io.* ;public class ExamResult {
public static void main ( String args[] )
{
int stuResult[] = new int[31] ;
int stuNumber[] = new int[31] ;
String inputName ;
String output = "" ;
int passes = 0 ,
failures = 0 ,
students = 0 ,
total = 0 ,
average = 0 ;
output = "学号\t" + "成绩" ;
JTextArea outputArea = new JTextArea() ;
JScrollPane scrollArea = new JScrollPane( outputArea ) ;
for ( int count = 1; count <= 30 ; count++ )
{ stuResult[count] = 1 + (int)(Math.random()*100) ;
stuNumber[count] = count ; output += "\n" + stuNumber[count] + "\t" + stuResult[count] ; if ( stuResult[count] >= 60 )
passes++ ;
else
{failures++ ;} students++ ;
total = total + stuResult[count] ;
} average = total / students ; output += "\n 通过: " + passes + " \n不通过: " + failures + " \n总成绩:"
+ total + " \n平均成绩 " + average + " \n总人数 " + students ;
if ( passes > 20 )
output = output + " \nCOOL!通过率很高啊! " ;
else
output = output + " \n成绩太差了! " ;
outputArea.setText(output);
JOptionPane.showMessageDialog ( null ,scrollArea ,"最后结果" ,
JOptionPane.INFORMATION_MESSAGE ) ;
System.exit ( 0 ) ;
}
}