Craps赌博游戏
游戏规则:同时扔两颗骰子,第一次扔出的点数数7或则11玩家胜,扔出2、3或则12庄家胜利,否则继续扔骰子。
以后只要扔出和第一次相同的点数玩家胜,扔出7庄家胜。
玩家每次进入有1000的筹码,输完游戏结束!
1 public static void gambleGame() { 2 int a = 0; 3 int money = 1000; 4 for (int j = 0;; j++) { 5 if (money > 0) { 6 System.out.println("余额:"+money); 7 String string=JOptionPane.showInputDialog("请输入下注金额:"); 8 if (string==null) { 9 break; 10 } 11 int outMoney = Integer.parseInt(string); 12 if (outMoney <= 0) { 13 JOptionPane.showMessageDialog(null, "涩家子!"); 14 } else if (outMoney > money) { 15 JOptionPane.showMessageDialog(null, "余额不足!余额:"+money); 16 } else { 17 for (int i = 0; ; i++) { 18 int n = (int) (Math.random() * 6 + 1) 19 + (int) (Math.random() * 6 + 1); 20 System.out.println("第" + (i + 1) + "次结果是" + n); 21 if (i == 0) { 22 a = n; 23 } 24 if (i == 0 && (n == 7 || n == 11)) { 25 System.out.println("玩家胜!"); 26 money += outMoney; 27 break; 28 } else if (i == 0 && (n == 2 || n == 3 || n == 12)) { 29 System.out.println("庄家胜!"); 30 money -= outMoney; 31 break; 32 } 33 if (i > 0) { 34 if (n == a) { 35 System.out.println("玩家胜!"); 36 money += outMoney; 37 break; 38 } 39 if (n == 7) { 40 System.out.println("庄家胜!"); 41 money -= outMoney; 42 break; 43 } 44 } 45 } 46 } 47 }else { 48 JOptionPane.showMessageDialog(null, "你已经输完了!"); 49 break; 50 } 51 } 52 }
百钱白鸡
1 for (int i = 0; i < 100; i+=3) { 2 for (int j = 1; j < 33; j++) { 3 int k=100-i-j; 4 if (5 * k+3 * j+i / 3 == 100 && k >= 0) { 5 System.out.println(i+","+j+","+k); 6 } 7 } 8 }
七星彩选号
public static void randomLottery() { int a = Integer.parseInt(JOptionPane.showInputDialog("请输入随机彩票的组数:")); for (int i = 0; i < a; i++) { System.out.print("第" + (i + 1) + "组彩票是:"); for (int j = 0; j < 7; j++) { System.out.print((int) (Math.random() * 10) + " "); } System.out.println(); } }
抓小偷
A说:“B没有偷,是D偷的”;
B说:“我没有偷,是C偷的”;
C说:“A没有偷,是b偷的”;
D说:“我没有偷”;
1 int[] array = {0,0,0,0}; 2 for (int i = 0; i < array.length; i++) { 3 array[i]=1; 4 if (array[1]+array[2]==1&&array[1]+array[3]==1) { 5 System.out.println(i); 6 } 7 }
21根火柴
int totalMatchNum=21; for (int i = 0; i <= 21/5; i++) { int myTakeMatchNum=Integer.parseInt(JOptionPane.showInputDialog("请输入要拿走的火柴数(1到4根):")); int computerTakematchNum=5-myTakeMatchNum; totalMatchNum-=5; if (totalMatchNum==1) { System.out.println("还剩一根火柴,你已经输了!"); break; }else { System.out.println("我拿走了 "+myTakeMatchNum+" 根火柴。\n电脑拿走了 "+computerTakematchNum+" 根火柴。\n还剩"+totalMatchNum+"根火柴。"); }
10000以内完美数
所有因子(除开自身)的和等于自身的数。
1 for (int i = 1; i < 10000; i++) { 2 int sum = 0; 3 for (int j = 1; j < i; j++) { 4 if (i % j == 0) { 5 sum += j; 6 } 7 } 8 if (sum == i) { 9 System.out.println(i+"是完美数!"); 10 } 11 }
时间: 2024-12-24 19:21:30