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> : ?????? 10.0 ????? (???? 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[] a = new int[100];
while (in.hasNext()) {
in.next(); in.next(); in.next(); in.next(); in.next(); in.next();
int b = in.nextInt();
for (int n = 0; n <= 99; n++) {
if (n == b)
a[n] = a[n] + 1; }
}
in.close();
System.out.print("เลขท้ายสองตัวที่ไม่เคยออกเลยคือ:");
// แสดงรายการของเลขที่ไม่เคยออก (แสดงต่อ ๆ กันไปบนบรรทัดเดียวกัน แต่ละตัวคั่นด้วยช่องว่าง เช่น 12 41 32)
// ถ้าไม่มีเลขที่ไม่เคยออก ก็ไม่ต้องแสดงอะไร
for (int n = 0; n <= 99; n++) {
if (a[n] == 0)
System.out.print(" " + n); }
System.out.println();
System.out.print("เลขท้ายสองตัวที่ออกบ่อยสุดคือ:");
// แสดงรายการของเลขที่ออกบ่อยสุด (แสดงต่อ ๆ กันไปบนบรรทัดเดียวกัน แต่ละตัวคั่นด้วยช่องว่าง เช่น 69 18)
// เลขที่ออกบ่อยสุด อาจมีมากกว่าหนึ่งตัว
int max = 0;
for (int n = 0; n <= 99; n++) {
if (a[n] > max)
max = a[n]; }
for (int n = 1; n <= 99; n++) {
if (a[n] == max)
System.out.print(" "+n);}
System.out.println();
}
}