คือ ผมได้รับโจทย์ให้เขียนโปรแกรม รับค่าเกรด x ค่า แล้ว หาค่าเฉลื่ย โดยใช้ Sentinel แล้วที่นี้ ผมอยากให้มันเป็นหน้าต่าง GUI ด้วยผม เห็นว่ามันสวยงามกว่าธรรมดา ซึ่ง ผม ทำแล้ว มัน Error รับค่าอยู่ครั้งเดียวไม่รู้ว่าต้องแก้ ไขตรงไหน วานผู้รู้ช่วย แนะแนวทางนิดหนึง นะคับ ขอบคุณคับ
Code:
import java.util.InputMismatchException;
import java.util.Scanner;
import javax.swing.*;
class sentinel {
public static void main(String args[]) {
int total;
int count;
int score;
double average;
total=0;
count=0;
Scanner input = new Scanner( System.in );
try {
String point = JOptionPane.showInputDialog("Enter your score ");
score=Integer.parseInt(point);
if(score>=101) {
String message = String.format("Please Enter Score 0-100");
JOptionPane.showMessageDialog(null,message);
score = 0;
int nextInt = input.nextInt();
}
while ( score != -1 ) {
total = total + score;
count = count + 1;
JOptionPane.showInputDialog("Enter your score ");
score = input.nextInt();
if(score>=101) {
String message = String.format("Please Enter Score 0-100");
JOptionPane.showMessageDialog(null,message);
score = 0;
int nextInt = input.nextInt();
}
}
if ( count != 0 ) {
average = (double) total / count;
System.out.printf( "\nTotal of %d grades entered is %d\n",count,total);
System.out.printf( "Grade average in this class : %.2f\n",average);
}
else
{
String message = String.format("No Data Score ... Please Enter Score");
JOptionPane.showMessageDialog(null,message);
}
}
catch(InputMismatchException e){
String message = String.format("Error !! : Data is Not Support");
JOptionPane.showMessageDialog(null,message);
}
}
}