当前位置: 代码迷 >> Java相关 >> [求助]为什么这个程序运行时出现错误,但是我改了之后,就一切正常了
  详细解决方案

[求助]为什么这个程序运行时出现错误,但是我改了之后,就一切正常了

热度:239   发布时间:2006-09-21 12:34:01.0
[求助]为什么这个程序运行时出现错误,但是我改了之后,就一切正常了

为什么这个程序运行时出现错误,但是我改了之后,就一切正常了,(红笔处):( 这还是书中的一个例题)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Bubble extends JApplet
{
public void init()
{
JTextArea outputArea = new JTextArea();
Container container = getContentPane();
container.add ( outputArea );

int array[] = {2,45,23,54,65,43,23,76,47,12,37,85,77};
String ouptut ="原数据顺序是:\";

for ( int i=0; i<array.length; i++ )
output += " " + array[i];

bubble ( array );

output +="\n\n 排序后的数据是:\n";

for ( int i=0; i<array.length; i++ )
output += " " + array[i];

outputArea.setText ( output );
}

public void Bubble ( int array2 )
{
for ( int i=0; i<array2.length; i++ )
{
for ( int j=0; j<array2.length; j++ )//改为:for (int j=i;j<array2.length;j++);
{
if ( array2[i]>array2[i+1]//改为if (array2[i]>array2[j];
swap ( array2,i,i+1 );//swap ( array2,i,j );
}
}
}

pbulic void swap ( array2[],first,second )
{
int hold ;
hold = array2[first];
array2[first] = array2[second];
array2[second] = hold ;
}
}

搜索更多相关的解决方案: 运行  

----------------解决方案--------------------------------------------------------
public void Bubble ( int array2 )// 明显有错误,怎么可能是传int
{
for ( int i=0; i<array2.length; i++ )
{
for ( int j=0; j<array2.length; j++ )//改为:for (int j=i;j<array2.length;j++);
{
if ( array2[i]>array2[i+1]//改为if (array2[i]>array2[j];
swap ( array2,i,i+1 );//swap ( array2,i,j );
}
}
}

----------------解决方案--------------------------------------------------------

对不起,这个地方是错了,应该是array2[],但是就是把这个改过来之后,也是不行的,我用红色改过的地方,只有用我在注释改过的地方,才能运行得过去,不然怎么也过不去,这本是书中的一个例子,难道例子也会出错吗


----------------解决方案--------------------------------------------------------

错误还不少呢
/*<applet code="Bubble.class"width=250 height=200></applet>*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Bubble extends JApplet
{
public void init()
{
JTextArea outputArea = new JTextArea();
// Container container = getContentPane();
this.add ( outputArea );
this.setSize(360,200);

int array[] = {2,45,23,54,65,43,23,76,47,12,37,85,77};
String output ="orange date is:";

for ( int i=0; i<array.length; i++ )
output += " " + array[i];

bubble ( array );

output +="\n\n 排序后的数据是:\n";

for ( int i=0; i<array.length; i++ )
output += " " + array[i];

outputArea.setText ( output );
}

public void bubble ( int array2[] )
{
int temp=0;
for ( int i=0; i<array2.length-1; i++ )
{
for ( int j=0; j<array2.length-i-1; j++ )//改为:for (int j=i;j<array2.length;j++);
{

if(array2[j]>array2[j+1])
swap(array2,j,j+1);

}
}
}

public void swap ( int array2[],int first,int second )
{
int hold ;
hold = array2[first];
array2[first] = array2[second];
array2[second] = hold ;
}
}
//这个是改好的 你的bubble sort 写的有问题

[此贴子已经被作者于2006-9-21 23:06:18编辑过]


----------------解决方案--------------------------------------------------------
[QUOTE] {
JTextArea outputArea = new JTextArea();
// Container container = getContentPane();
this.add ( outputArea );
this.setSize(360,200);
[/QUOTE]
用这是什么意思呀
----------------解决方案--------------------------------------------------------