31
十二月

程式設計一程式 12/31: Ex9Class.java

   Posted by: admin   in 103(上)程式設計(一)

import java.io.*;  //#include <stdio.h>
import java.util.*;//#include <stdlib.h> 
public class Ex9Class {
	public static void main(String[] args) {
		int N=1000;
	    int i, j, k;
	    int max, index;
	    int[] a = new int[6];//int a[6];
	    int[] ball = new int[49];//int ball[49];	    
 
	    Random rnd = new Random();//建立亂數的物件
 
	    try//FILE* fout;
	    {
		    FileWriter writer = new FileWriter("lotto.txt"); //fopen("lotto.txt" "w");
		    PrintWriter pw = new PrintWriter(writer);
 
		    for (k = 0; k < N;  k++)
		    {
		      //給每一顆球不同的重量 
		      for (i=0; i<49; i++)
		      {
		          //ball[i] = rand();
		    	  ball[i] = rnd.nextInt();////產生1個亂數
		      }
		      //取出最重的球(6次) 
		      for (i=0; i<6; i++)
		      {
		          max = 0; index = 0;
		          for (j=0; j<49; j++)
		          {
		              if (ball[j] > max)
		              {
		                  max =ball[j];
		                  index = j;
		              }
		          }
		          a[i] = index + 1;//最重的球編號 
		          ball[index] = 0; //把最重的球重量歸零 
		       }
		       System.out.printf("The number is %2d %2d %2d %2d %2d %2d\n", a[0], a[1], a[2], a[3], a[4], a[5]);
               pw.printf("%2d %2d %2d %2d %2d %2d", a[0], a[1], a[2], a[3], a[4], a[5]); pw.println();
		    }
		    writer.close();
	    }
	    catch (FileNotFoundException fe)
	    {
	    	System.out.printf(fe.getMessage());
	    }
	    catch (IOException fe)
	    {
	    	System.out.printf(fe.getMessage());
	    }
	}
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , , ,

This entry was posted on 星期三, 十二月 31st, 2014 at 08:58:34 and is filed under 103(上)程式設計(一). You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a reply

Name (*)
Mail (will not be published) (*)
URI
Comment