1. 程式人生 > >編寫一個程序,模擬扔硬幣的結果

編寫一個程序,模擬扔硬幣的結果

多看 and com coin style 14. ice cti 編寫一個程序

 1 import java.util.*;
 2 public class practice {
 3     public static void main(String[] args) {
 4         Random random=new Random();
 5         for (int i = 0; i < 6; i++) {//這個循環只是我想多看幾次結果,可以不用。
 6             int coin=random.nextInt();
 7             System.out.println("coin="+coin);
 8             if
(coin%2==0) System.out.println("正面");//coin%2的意思是,任何正整數處2的余數不是1就是0. 9 else System.out.println("反面"); 10 } 11 12 } 13 }

運行結果如下:

技術分享圖片

編寫一個程序,模擬扔硬幣的結果