当前位置: 代码迷 >> Java相关 >> [求助]一个数组的小程序
  详细解决方案

[求助]一个数组的小程序

热度:117   发布时间:2006-12-06 21:28:40.0
[求助]一个数组的小程序
public class Test1
{

public static void main(String[] args)
{
int[] a=new int[10];
for(int i=0;i<10;i++){
int a[i]=(Math.random())*100;
System.out.println(a[i]);
}
}
}
这个程序哪里有问题?编译无法通过:“Test1.java:7: ']' expected”
各位达人帮帮忙啊

----------------解决方案--------------------------------------------------------
以下是引用hyzx332335在2006-12-6 21:28:40的发言:
public class Test1
{

public static void main(String[] args)
{
int[] a=new int[10];
for(int i=0;i<10;i++){
int a[i]=(Math.random())*100;
System.out.println(a[i]);
}
}
}
这个程序哪里有问题?编译无法通过:“Test1.java:7: ']' expected”
各位达人帮帮忙啊
你那里怎么又定义a[],你自己改改看吧。
----------------解决方案--------------------------------------------------------

我帮你改了一下,你自己对比一下哪错了吧
public class test
{

public static void main(String[] args)
{
int[] a=new int[10];
for(int i=0;i<10;i++){
a[i]=(int)(Math.random()*100);
System.out.println(a[i]);
}
}
}


----------------解决方案--------------------------------------------------------
记住 在你已经有定义对象前不要再定义    要不然肯定要出错误。要是强制类型转换,一般的情况下都是在“=”的右边。
----------------解决方案--------------------------------------------------------

查一下java 的API文档就可以知道,Math.radom()返回的是一个Double类型的值,如果用int 的话会损失精度,所以应该不能通过编译,还有,可能是优先级问题吧,程序应该是这样写
public class test
{

public static void main(String[] args)
{
int[] a=new int[10];
for(int i=0;i<10;i++)
{
a[i]=(int)((Math.random())*100); //注意这里哦~~~~
     System.out.println(a[i]);
}
}
}


----------------解决方案--------------------------------------------------------
数据类型不匹配
----------------解决方案--------------------------------------------------------
  相关解决方案