我试图让这个“for”语句打印出数组的十进制值,但我无法弄清楚。
for ( int b = 2; b < totals.length; b++ )
{
int percent = totals[ b ] / ( 360000 );
System.out.printf( "%3d%12d%17d\n", b, totals[ b ], percent );
}
我尝试在 printf 语句上执行 %.2f ,但它不起作用。
代码当然还有更多内容,但我不确定您是否需要查看更多内容来帮助我。任何提示都会很棒。
编辑:这是代码:
import java.util.Random;
public class Roll36K
{
public void rollDice()
{
Random randomNumbers = new Random();
int die1; // number on first die
int die2; // number on second die
int totals[] = new int[ 13 ]; // frequencies of the sums
for ( int index = 0; index < totals.length; index++ )
totals[ index ] = 0;
for ( int roll = 1; roll <= 36000000; roll++ )
{
die1 = 1 + randomNumbers.nextInt( 6 );
die2 = 1 + randomNumbers.nextInt( 6 );
totals[ die1 + die2 ]++;
}
System.out.printf( "%3s%12s%17s\n", "Sum", "Frequency", "Sim. Probability" );
for ( int b = 2; b < totals.length; b++ )
{
int percent = totals[ b ] / ( 360000 );
System.out.printf( "%3d%12d%17d\n", b, totals[ b ], percent );
} // end for
} // end method rollDice
} // end class Roll36