gogo Publish time 6-11-2012 05:32 PM

JAVA!

Sape yg tau Java, nk mntk tlg skit :$

I tgh buat java array utk markah students, then calculate average and decide grade. utk scores input, akan ada pop-up box - input error bila user masukkan nombor selain 0-100 or simbol or alphabets. And, program tu kne loop 100 kali.

Ni code yg I buat. I sgt newbie dlm java nih, so hrp tegur klo ada yg salah :$

import java.util.Scanner;

import javax.swing.JOptionPane;



public class gradeavg {

      public static void main(String[] args) {
               
                Scanner input = new Scanner(System.in);
               
                        //Loop 100x
                        for (int counter = 1; counter <= 100; counter++){
                        
                        //Variables
                int score[] = new int;
                for (int a = 0; a < 3; a++) {
               
                        //Asking for student's name and student ID
                        System.out.println("Enter student's name: ");
                        input.next();
                        System.out.println("Enter student ID: ");
                        input.next();
                        
                        //try - input error
                        try {
                                    {
               
                            //Asking for scores
                        System.out.println("Enter Biology score (%): ");
                           score = Integer.parseInt(input.next());
                           
               
                           System.out.println("Enter Science score (%): ");
                  score = Integer.parseInt(input.next());
                           
                           
                  System.out.println("Enter Mathematics score (%): ");
                  score = Integer.parseInt(input.next());
                                 
                  //Calculate & show the average
                        double avg = (score + score + score)/ 3;
                              System.out.print("Average (%): " +avg);

                        //Grades condition      
                              {
                                        if (avg <=100 & avg >=80)
                                                {

                                                JOptionPane.showMessageDialog(null, "Your grade is A. Congratulations! You may proceed to the next stage.");
                                                }
                                        else
                                        {
                                        if (avg <=79 & avg >=60)
                                       {
               
                                                JOptionPane.showMessageDialog(null, "Your grade is B. Congratulations! You may proceed to the next stage.");
                                       
                                       }
                                        else
                                        {
                                       if (avg <=59 & avg >=40)
                                       {

                                                 JOptionPane.showMessageDialog(null, "Your grade is C. You may proceed to the next stage.");
                                                
                                       }
                                       else
                                       {
                                          if (avg <=39 & avg >=20)
                                           {

                                                JOptionPane.showMessageDialog(null, "Your grade is D. You have to reseat the examination. You are not qualified to the next stage.");
                                                         
                                           }
                                          else
                                          {
                                                if (avg <=19 & avg >=0);
                                                {      

                                                          JOptionPane.showMessageDialog(null, "Your grade is E. You have to reseat the examination. You are not qualified to the next stage.");
                                                }
                                                          }
                                                                  }
                                                                         }
                                                                              }
                                                                                        }
                                                                                          }
                                                                                                    }

                                        //catch - input error
                                                catch(Exception e)
                                                 {
                                                         int n = Integer.parseInt(input.next());
                                                         if (n <= 0 & n >= 100)
                                                         System.out.println("Input Error!");
                                                         input.next();
                                                         
                                                               e.getMessage();
                                                   JOptionPane.showInputDialog("Input must be between 0-100 and only numerical characters are allowed.");
                                                   System.exit(1);
                                                 }
                                                
                                                {
                                                 }
                                                          }      
                                                                }
                                                                        {
                                                                        }
                                                                              {
                                                                              }
                                                                                        {
                                                                                        }
                                                                                                {
                                                                                                }
                                                                                                      {
                                                                                                      }
                                                                                                                {
                                                                                                                }
                                                                                                                        }
                                                                                                                              }

                                                                                       
                                                                                       
               
               
                        
               

Time compile program ni without try-catch, sume ok. xde error and input-output sume ok. but, bila masukkan try n catch, after input scores, average n grade x kluar plak:L

Lagi 1, I tau try-catch I buat tu just utk numerical other than 0-100. So cmne nk tmbh alphabets and symbol ek? :sweat3:

And actually, yg output qualified ke next stage ke tidak tu, spatutnya separate from grade. Means, after program tu reveal grade, baru lah reveal qualified ke x. But I guna Eclipse so output (grade) tu revealed kt pop-up box, bkn kt box yg sama dgn input. if xde pop-up box tu, tau la kne tekan Enter jer.

Soklan last :$ Output average tu dlm 1 decimal point tp value tu sume round number. contoh, average sbnrnya 63.333 but yg tunjuk kt output tu 63.0. Then, bila average 63.888, output yg kluar just 63.0. spatutnya output 64.0 (round 63.888). so td try tukar output tu ke 2 decimal points but output kluar 63.00 (average sbnr 63.888) jer. cmne nk tukar ni ek?


harap sgt sape2 kt sini leh tolong :loveliness:



Last edited by gogo on 6-11-2012 05:35 PM

limaunipis03 Publish time 12-11-2012 10:31 PM

dah lama tak tgk java ni.
aku dulu program java tak guna swing pun (tak ada interface, run @ terminal jer)
anyway, harap dpt membantu:
1. kalo nak round kan double value, boleh guna math.round
double avg = (score + score + score)/ 3;
double avg2 = (double) Math.round(avg * 100) / 100;

ada beberapa cara lain.. kalo nak round jadi integer boleh type cast
double avg = (score + score + score)/ 3;
int avg2 = (int) avg;

2. nak check input tu number or not boleh tambah lagi satu catch

catch (NumberFormatException nfe) {
    throw new IllegalArgumentException("Pls enter a valid number.", nfe);
}


harap dpt membantu.
Pages: [1]
View full version: JAVA!


ADVERTISEMENT