import java.util.Random;

public class FF

{

   public static void main ( String args[] )

   {

      final int ARRAY_LENGTH = 7;

      int array[] = new int [ ARRAY_LENGTH ];

      for ( int counter = 0 ; counter < array.length ; counter++) //從0到7 共7次

         array[ counter ] = 11 + 2 * counter; //自己導公式

         System.out.printf( "%s%8s\n" , "Index" , "Value" ); //印出最上面的一行

      for ( int counter = 0 ; counter < array.length ; counter++)

         System.out.printf( "%5d%8d\n" , counter , array[ counter ] ); //經過計算顯示出下面的

      int total = 0 ;

      for ( int counter = 0 ; counter < array.length ; counter++)

      total += array[counter];

         System.out.printf("\nTotal = %d\n" , total);

      for ( int counter = 0 ; counter < array.length ; counter++)

      {

         System.out.printf("\n%d: " , counter);

         for ( int i = 0 ; i < array[counter]; i++)//這邊不可以加分號 加了分號就只執行一次

            System.out.printf("%c" , '*');

      }

         System.out.printf("\n");

         Random r = new Random();

         for (int counter = 0 ; counter < 100 ; counter++)

            array[r.nextInt(7)]++;

            // x = r.nextInt(7); x:0~6

            // array[x] = array[x]+1 ;

         for(int counter = 0 ; counter < array.length ; counter++)

            System.out.printf("%5d%8d\n" , counter , array[counter]);

   }

}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 a25561970 的頭像
    a25561970

    萬花筒

    a25561970 發表在 痞客邦 留言(0) 人氣()