Lab5-Lottery
5530438021 น.ส.ภาดา โพธิ์สอาด (COM22432) (7/13/2012 (11:35:28 AM))
| MiniQuiz + TestScript |
JLab>javac Lottery.java
JLab>
JLab>java Selftest
JLab> testLottery : ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok (10.0/10.0)
JLab> : -----------------------------
JLab> : You got 10.0 points (out of 10.0)
JLab> : -----------------------------
JLab> :<POINT>10.0</POINT> (<TOTAL>10.0</TOTAL> )
JLab>
ได้ 10 คะแนน |
| Source Code |
import java.util.Scanner;
import java.io.*;
public class Lottery {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(new File("lottery.txt"));
int[] count = new int[100];
int max = 0;
while (in.hasNext()) {
String number = in.nextLine();
number = number.substring(number.length() - 2, number.length());
int num = Integer.parseInt(number);
count[num] += 1;
}
for (int i = 0; i < 100; i++) {
if (count[i] >= max) {
max = count[i];
}
}
System.out.print("เลขท้ายสองตัวที่ไม่เคยออกเลยคือ:");
// แสดงรายการของเลขที่ไม่เคยออก (แสดงต่อ ๆ กันไปบนบรรทัดเดียวกัน แต่ละตัวคั่นด้วยช่องว่าง เช่น 12 41 32)
// ถ้าไม่มีเลขที่ไม่เคยออก ก็ไม่ต้องแสดงอะไร
for (int j = 0; j < 100; j++) {
if (count[j] == 0) {
if (j < 10) {
System.out.print("0" + j + " ");
} else {
System.out.print(j + " ");
}
}
}
System.out.println();
System.out.print("เลขท้ายสองตัวที่ออกบ่อยสุดคือ:");
// แสดงรายการของเลขที่ออกบ่อยสุด (แสดงต่อ ๆ กันไปบนบรรทัดเดียวกัน แต่ละตัวคั่นด้วยช่องว่าง เช่น 69 18)
// เลขที่ออกบ่อยสุด อาจมีมากกว่าหนึ่งตัว
for (int y = 0; y < 100; y++) {
if (max == count[y]) {
if (y < 10) {
System.out.print("0" + y + " ");
} else {
System.out.print(y + " ");
}
}
}
System.out.println();
}
}
|
SM=3, CM=5, ST=6, KY=632, TR=60:00
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
|
|