日期:2014-05-20  浏览次数:20760 次

大家帮忙调式一下这个程序,看那里有问题
一直出现找不到符号,调式了半天还是有问题
import   javax.swing.*;
public   class   Histogram
{
public   static   void   main(String[]   args){
int   array[]   =   {   12,   3,   5,   3,   15,   9,   2,   9,   6,   8   };
String   output   =   "Element\tValue\tHistogram ";

for(   int   counter   =   0;   counter   <   array.length;   counter++   )
output   +=   "\n "   +   counter   +   "\t "   +   array[counter]   +   "\t ";

for(   int   stars   =   0;   stars   <   array[counter];   stars++   )//找不到符号
output   +=   "* ";

JTextArea   outputArea   =   new   JTextArea();
outputArea.setText(output);

JOptionPane.showMessageDialog(   null,   outputArea,
"Histogram   Printing   Program ",  
JOptionPane.INFORMATION_MESSAGE   );
System.exit(0);
}
}

------解决方案--------------------
import javax.swing.*;
public class Histogram
{
public static void main(String[] args){
int array[] = { 12, 3, 5, 3, 15, 9, 2, 9, 6, 8 };
String output = "Element\tValue\tHistogram ";

for( int counter = 0; counter < array.length; counter++ )
{
output += "\n " + counter + "\t " + array[counter] + "\t ";

for( int stars = 0; stars < array[counter]; stars++ )//找不到符号
output += "* ";
}

JTextArea outputArea = new JTextArea();
outputArea.setText(output);

JOptionPane.showMessageDialog( null, outputArea,
"Histogram Printing Program ",
JOptionPane.INFORMATION_MESSAGE );
System.exit(0);
}
}

------解决方案--------------------
import javax.swing.*;
public class Histogram
{
public static void main(String[] args){
int array[] = { 12, 3, 5, 3, 15, 9, 2, 9, 6, 8 };
String output = "Element\tValue\tHistogram ";
int counter;
for(counter = 0; counter < array.length-1; counter++ )
output += "\n " + counter + "\t " + array[counter] + "\t ";

for( int stars = 0; stars < array[counter]; stars++ )//找不到符号
output += "* ";

JTextArea outputArea = new JTextArea();
outputArea.setText(output);

JOptionPane.showMessageDialog( null, outputArea,
"Histogram Printing Program ",
JOptionPane.INFORMATION_MESSAGE );
System.exit(0);
}
}

变量的可见性.counter在for()内可见,在for外不可见,所以找不到符号.
------解决方案--------------------
楼上正解,遇到这样的问题,把变量放在外面声明一下就OK了
------解决方案--------------------
应该是二楼的对,三楼改了之后会报异常. 他的解释是对的.
------解决方案--------------------
三楼才是对的,因为counter 在第一个for中定义,for 一结束,counter 就释放了。因为找不到符号。。。。