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];
int max = 0;
int b;
int c;
String d;
while (in.hasNext()) {
d = in.nextLine();
d = d.trim();
c = d.length();
b = Integer.parseInt(d.substring(c - 2, c));
int i =0;
while (i < 100) {
if (b == i) {
a[i] = a[i] + 1; }
i++;}}
System.out.print("เลขท้ายสองตัวที่ไม่เคยออกเลยคือ:");
// แสดงรายการของเลขที่ไม่เคยออก (แสดงต่อ ๆ กันไปบนบรรทัดเดียวกัน แต่ละตัวคั่นด้วยช่องว่าง เช่น 12 41 32)
// ถ้าไม่มีเลขที่ไม่เคยออก ก็ไม่ต้องแสดงอะไร
int g = 0;
while(g<100){
if (a[g] == 0) {
System.out.print(" " + g);
}
g++; }
max = a[0];
int l = 0;
while (l < 100) {
if (a[l] > max) {
max = a[l]; }
l++; }
System.out.println();
System.out.print("เลขท้ายสองตัวที่ออกบ่อยสุดคือ:");
// แสดงรายการของเลขที่ออกบ่อยสุด (แสดงต่อ ๆ กันไปบนบรรทัดเดียวกัน แต่ละตัวคั่นด้วยช่องว่าง เช่น 69 18)
// เลขที่ออกบ่อยสุด อาจมีมากกว่าหนึ่งตัว
int y = 0;
while (y < 100) {
if (a[y] == max) {
System.out.print(" " + y); }
y++; }
System.out.println();
}
}