Page 52 - PowerPoint Presentation
P. 52

MENULIS ATUR CARA MENGGUNAKAN
          1.4              1.4.4            STRUKTUR KAWALAN ULANGAN YANG


                                                       MELIBATKAN  -BOOLEAN












       •   Satu pemboleh ubah Boolean digunakan untuk mengawal ulangan.


       •   Pengguna ditanya untuk meneruskan atur cara itu lagi atau tidak.




                 MENJANA 20 NOMBOR RAMBANG BAGI DADU


                                       CONTOH ATURCARA


       public class contoh47 {
           public static void main (String [] args) {
               int i ;
               int diceNo;
               int count1 =0, count2 =0, count3 =0, count4 =0, count5 =0, count6 =0;
              Scanner scanner = new Scanner (System.in);
               Boolean flag = true;
                 do {
                    for (i = 1; i <=20 ; i++ ) {
                        diceNo = (int) (Math.random() * 6 + 1) ;
                        System.out.println ( diceNo + “   ” )  ;
                        Switch (diceNo) {
                          case 1 : count1++ ; break;
                          case 2 : count2++ ; break;
                          case 3 : count3++ ; break;
                          case 4 : count4++ ; break;
                          case 5 : count5++ ; break;
                          case 6 : count6++ ;
                         }
                    System.out.println () ;
                    System.out.println (“ * Dice Number 1 =  ” + count1  +  “ % ”);
                    System.out.println (“ * Dice Number 2 =  ” + count2  +  “ % ”);
                    System.out.println (“ * Dice Number 3 =  ” + count3  +  “ % ”);
                    System.out.println (“ * Dice Number 4 =  ” + count4  +  “ % ”);
                    System.out.println (“ * Dice Number 5 =  ” + count5  +  “ % ”);
                    System.out.println (“ * Dice Number 6 =  ” + count6  +  “ % ”);
                    System.out.println () ;
                    System.out.println (“ ---------------------------------”);
                    System.out.println (“ Taip ya untuk teruskan, tidak untuk henti”);
                      if (Scanner.next().equals(“ya”)) {
                         flag = true;
                      else {
                          flag = false;
                      }
               } while ( flag)                                                                    52
           }
   47   48   49   50   51   52   53   54   55   56   57