編寫個小程序,游戲,木有思路,希望大神求教⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯求源代碼
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 產生一個隨機數
int number = (int) (Math.random() * 100) + 1;
// 加入count
int count = 0;
// 在這裡加入最大值,和最小值
int max = 100;
int min = 1;
while (true) {
// 鍵盤錄入數據
Scanner sc = new Scanner(System.in);
System.out.println("請輸入你要猜的數據:(" + min + "~" + max + ")");
try {
count++;
int guessNumber = sc.nextInt();
// 判斷
if (guessNumber > number) {
max = guessNumber;
System.out.println("你猜大了");
} else if (guessNumber < number) {
min = guessNumber;
System.out.println("你猜小了");
} else {
System.out.println("恭喜你,花了" + count + "次就猜中了");
// 問是否繼續
System.out.println("請問還要繼續嗎?(yes)");
sc = new Scanner(System.in);
String str = sc.nextLine();
if ("yes".equals(str)) {
// 重寫賦值隨機數
number = (int) (Math.random() * 100) + 1;
count = 0;
max = 100;
min = 1;
} else {
break;
}
}
} catch (InputMismatchException e) {
System.out.println("你輸入的數據有誤");
}
}
}
}