[英]Create and fill a 2D Array
我有这些指示。
打印数组中的0数
public class DoubleArray { public static void main(String [] args) { double [] a = new double[102]; for (int i= 2; i<a.length;i++) if (Math.random()>2.5) { a[i]=3; System.out.println("3"); } else a[i]=2; { System.out.println("2"); } } }
这甚至不包括0的计数器,但是我不知道使用随机数,一个数组,一个for循环,if / else和一个计数器。
如果您更正缩进,则可以轻松地像
double [] a = new double[100];
int zeroCount = 0; // new variable
for (int i= 0; i<a.length;i++)
{ // need curly here (for readability)
if (Math.random()>0.5) {
a[i]=1;
System.out.println("1");
}
else
{
a[i]=0;
zeroCount++; // increment
System.out.println("0");
}
}
System.out.println("Number of zeros is " + zeroCount); // print
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.