我作的是一个随机数产生的小试验,和书上的代码一样啊!为什么会出现一下错误,也检查过了啊!没有什么错误啊!
//随机数的产生
import javax.swing.JOptionPane;
public class RandomIntegers
{
public static void main(String args[])
{
int value;
String output="";
for(int counter=1;counter<=20;counter++)
value=1+(int)(Math.random()*6);
output+=value+"";
if(counter%5==0)
output+="\n";
}
JOptionPane.showMessageDialog(null,output,"20 Random Numbers from 1 to 6",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}--------------------Configuration: j2sdk1.4.0_01 <Default>--------------------
F:\Java临时文件\RandomIntegers.java:15: <identifier> expected
JOptionPane.showMessageDialog(null,output,"20 Random Numbers from 1 to 6",JOptionPane.INFORMATION_MESSAGE);
^
F:\Java临时文件\RandomIntegers.java:16: <identifier> expected
System.exit(0);
^
F:\Java临时文件\RandomIntegers.java:15: cannot resolve symbol
symbol : class showMessageDialog
location: class javax.swing.JOptionPane
JOptionPane.showMessageDialog(null,output,"20 Random Numbers from 1 to 6",JOptionPane.INFORMATION_MESSAGE);
^
F:\Java临时文件\RandomIntegers.java:16: cannot resolve symbol
symbol : class exit
location: class java.lang.System
System.exit(0);
^
F:\Java临时文件\RandomIntegers.java:12: cannot resolve symbol
symbol : variable counter
location: class RandomIntegers
if(counter%5==0)
^
5 errorsProcess completed.
//随机数的产生
import javax.swing.JOptionPane;
public class RandomIntegers
{
public static void main(String args[])
{
int value;
String output="";
for(int counter=1;counter<=20;counter++)
value=1+(int)(Math.random()*6);
output+=value+"";
if(counter%5==0)
output+="\n";
}
JOptionPane.showMessageDialog(null,output,"20 Random Numbers from 1 to 6",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}--------------------Configuration: j2sdk1.4.0_01 <Default>--------------------
F:\Java临时文件\RandomIntegers.java:15: <identifier> expected
JOptionPane.showMessageDialog(null,output,"20 Random Numbers from 1 to 6",JOptionPane.INFORMATION_MESSAGE);
^
F:\Java临时文件\RandomIntegers.java:16: <identifier> expected
System.exit(0);
^
F:\Java临时文件\RandomIntegers.java:15: cannot resolve symbol
symbol : class showMessageDialog
location: class javax.swing.JOptionPane
JOptionPane.showMessageDialog(null,output,"20 Random Numbers from 1 to 6",JOptionPane.INFORMATION_MESSAGE);
^
F:\Java临时文件\RandomIntegers.java:16: cannot resolve symbol
symbol : class exit
location: class java.lang.System
System.exit(0);
^
F:\Java临时文件\RandomIntegers.java:12: cannot resolve symbol
symbol : variable counter
location: class RandomIntegers
if(counter%5==0)
^
5 errorsProcess completed.
public class RandomIntegers
{
public static void main(String args[])
{
int value=0;
String output="";
int counter;
for(counter=1;counter<=20;counter++)//你在这里定义的counter在外面不能用...要定义到外面
value=1+(int)(Math.random()*6);
output+=value+"";
if(counter%5==0)
output+="\n";
JOptionPane.showMessageDialog(null,output,"20 Random Numbers from 1 to 6",JOptionPane.INFORMATION_MESSAGE);//这一句应该写在main函数里
System.exit(0);//这一句也是
}
}//程序的错是改了..但没有看明白你到底要实现个什么功能...for用了有用么...
public class RandomIntegers {
public static void main(String args[]) {
int value;
String output="";
for(int counter=1;counter<=20;counter++) {
value=1+(int)(Math.random()*6);
output+=value+"";
if(counter%5==0)
output+="\n";
JOptionPane.showMessageDialog(null,output,"20 Random Numbers from 1 to 6",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
}