.........
2006-11-26 20:57:51 UTC
我是要做一個陣列
若輸入為"999"就停止
再把輸入的數做排大小(偽bubble sort)
compile過了 不過執行到一半就會出現怪怪的
例如我輸入了:10 20 999
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:3
at arr.main(arr.java:40)
Press any key to continue...
請各位前輩給我一點提示八..多謝多謝 大感激
以下為我的程式碼
import javax.swing.JOptionPane;
public class arr
{public static void main(String args[])
{
int[] a1=new int[100];
int i;
int b=0;
for(i=0;i<=14;i++)
{String out=JOptionPane.showInputDialog(null,"enter int","window",JOptionPane.QUESTION_MESSAGE);
a1[i]=Integer.parseInt(out);
if(a1[i]==999)
{
b=i;
break;
}
}
int[] a2=new int[b+1];
for(i=0;i<=b;i++)
{ a2[i]=a1[i];
}
String o="the array2 is ";
for(i=0;i<=b;i++)
{ o=o+" "+a2[i];
}
JOptionPane.showMessageDialog(null,o,"win",JOptionPane.INFORMATION_MESSAGE);
int temp=0;
int s=0;
for(s=0;s<=b;s++)
{
for(i=0;i<=b;i++)
{ if(a2[i]>a2[i+1])
{temp=a2[i];
a2[i]=a2[i+1];
a2[i+1]=temp;
}
}
}
JOptionPane.showMessageDialog(null,"第二大為"+a2[b-1]+" "+"第二小為"+a2[1],"win",JOptionPane.INFORMATION_MESSAGE);
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.134.2.243
若輸入為"999"就停止
再把輸入的數做排大小(偽bubble sort)
compile過了 不過執行到一半就會出現怪怪的
例如我輸入了:10 20 999
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:3
at arr.main(arr.java:40)
Press any key to continue...
請各位前輩給我一點提示八..多謝多謝 大感激
以下為我的程式碼
import javax.swing.JOptionPane;
public class arr
{public static void main(String args[])
{
int[] a1=new int[100];
int i;
int b=0;
for(i=0;i<=14;i++)
{String out=JOptionPane.showInputDialog(null,"enter int","window",JOptionPane.QUESTION_MESSAGE);
a1[i]=Integer.parseInt(out);
if(a1[i]==999)
{
b=i;
break;
}
}
int[] a2=new int[b+1];
for(i=0;i<=b;i++)
{ a2[i]=a1[i];
}
String o="the array2 is ";
for(i=0;i<=b;i++)
{ o=o+" "+a2[i];
}
JOptionPane.showMessageDialog(null,o,"win",JOptionPane.INFORMATION_MESSAGE);
int temp=0;
int s=0;
for(s=0;s<=b;s++)
{
for(i=0;i<=b;i++)
{ if(a2[i]>a2[i+1])
{temp=a2[i];
a2[i]=a2[i+1];
a2[i+1]=temp;
}
}
}
JOptionPane.showMessageDialog(null,"第二大為"+a2[b-1]+" "+"第二小為"+a2[1],"win",JOptionPane.INFORMATION_MESSAGE);
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.134.2.243