数组越界,程序退出了
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
解决方案 »
- JPanel使用GridLayout布局不能显示panel中加入的标签?
- 我在编写一个记事本的java程序,但是无法解决对选定字符内容的字体更改,希望哪位大虾可以帮助解决?
- 继承里的问题
- 如果在一个程序中同时响应多个事件?
- 简单问题,但我不会,急!在线
- 请问这段代码为什么会产生这样的结果(重分相谢)
- 请问jb8正式版和trail版到底有什么区别
- 这种网站是用什么技术做的?http://www.trade-ok.com/cobe/yycnet/index.asp
- 如何给JCheckBox 属性赋值
- 关于jbuilder4.0,谁能解决,85分送上
- 【急急急】关于Java IO流中OutputStreamWriter的用法???
- java,可以实现给PDF文件加密吗?
由于数组a的长度是3,即数组最大为a[2]当i= 2时
14行的 a[i+1] = a[3],此时已经超过了数组的最大长度
for(int i=0;i<3-j-1;i++){
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 3 - j; i++) {
if (a[i] > a[i + 1]) {
int temp = a[i + 1];
a[i + 1] = a[i];
a[i] = temp;
}
}
}
数组越界
import java.util.Scanner;public class Main {
public static void main(String args[]) throws Exception{
Scanner s= new Scanner(System.in);
int a[]=new int[3];
for(int i=0;i<3;i++){
a[i]=s.nextInt();
}
for(int j=0;j<3;j++)
for(int i=0;i<3-j-1;i++){
if(a[i]>a[i+1]){
int temp=a[i+1];
a[i+1]=a[i];
a[i]=temp;
}
}
System.out.println("");
System.out.print(+a[0]+" "+a[1]+" "+a[2]);
}
}写好了 谢谢