孤獨者 - 心在黑暗裡旅行 ...... ...
至尊會員
遊蕩的過客 - 枉入紅塵若許年 .. ...
import java.io.*; public class NumGame { public static void main(String[] args) throws IOException { BufferedReader keyin = new BufferedReader(new InputStreamReader(System.in)); String Str1 = new String(); for (int f=0;f<=2 ;f++ ) { //產生亂數 double a = Math.random()*10; int a1 = (int)a; double b = Math.random()*10; int a2 = (int)b; double c = Math.random()*10; int a3 = (int)c; double d = Math.random()*10; int a4 = (int)d; if (a1==a2||a1==a3||a1==a4||a2==a3||a2==a4||a3==a4) { f=0; } else { System.out.println(); System.out.println(); System.out.println("\t猜數字遊戲,請輸入四個數字"); System.out.println(); System.out.println("\t*********遊戲規則*********"); System.out.println(); System.out.println("如數字相同且位置相同則為A,數字相同位置不同則為B,均不同則不顯示"); for (int z=0;z<=4 ;z++) { while(true) { System.out.println(); System.out.print("請輸入四個數:"); Str1 = keyin.readLine(); System.out.println(); int len = Str1.length();//取得字串長度 if (len!=4) { System.out.println("你輸入的數字,不是四位數,請重新輸入!!"); } else { char num1 = Str1.charAt(0); char num2 = Str1.charAt(1); char num3 = Str1.charAt(2); char num4 = Str1.charAt(3); if (num1 > '9'|| num2 > '9'|| num3 > '9'|| num4 > '9') { System.out.println("你輸入的不是數字,請重新輸入!!"); } else { break;//離開while的迴圈 } } } int e = Integer.parseInt(Str1); int e1 =e/1000; int e2 =e%1000/100; int e3 =e%100/10; int e4 =e%10; int y=z=0; if (e1==a1) { z=z+1; } else if (e1==a2 || e1==a3 || e1==a4) { y=y+1; } if (e2==a2) { z=z+1; } else if (e2==a1 || e2==a3 || e2==a4) { y=y+1; } if (e3==a3) { z=z+1; } else if (e3==a1 || e3==a2 ||e3==a4) { y=y+1; } if (e4==a4) { z=z+1; } else if (e4==a1 || e4==a2 || e4==a3) { y=y+1; } System.out.println("你輸入的是: "+e1+e2+e3+e4 + "電腦提示: "+z+"A"+y+"B"); System.out.println(); } for ( f=0; f <= 2 ; f=f+1 ) { System.out.print("妳答對了,是否繼續(y,n)"); char r =(char)System.in.read(); System.in.read();System.in.read(); if (r=='y' || r=='Y') { System.out.println("你輸入繼續"); f=1; break; } else if (r=='n' || r=='N') { System.out.println("結束運算"); f=100; } else if (f==2) { System.out.println("你是豬哦!Y OR N 都搞不清楚,為了加強你的腦部訓練,所以再玩一次!"); f=1; break; } else { System.out.println("你輸入的非y or n;請重新輸入"); } } } } } }
查看詳細資料
TOP
正式會員
試用會員
本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。