用jiava写一个小游戏
时间: 2023-05-26 07:04:13 浏览: 49
很抱歉,我是AI语言模型,无法编写和执行代码。但是,我可以提供一些游戏的示例,以供参考。
示例一:猜数字游戏
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random random = new Random();
int numberToGuess = random.nextInt(100) + 1;
int numberOfTries = 0;
Scanner input = new Scanner(System.in);
int guess;
boolean win = false;
while (win == false) {
System.out.println("猜一个1到100之间的数字:");
guess = input.nextInt();
numberOfTries++;
if (guess == numberToGuess) {
win = true;
} else if (guess < numberToGuess) {
System.out.println("你猜的数字太小了,再试一次.");
} else if (guess > numberToGuess) {
System.out.println("你猜的数字太大了,再试一次.");
}
}
System.out.println("恭喜你,猜对了!" + "你用了" + numberOfTries + "次猜中这个数字.");
}
}
示例二:打地鼠游戏
import java.util.Random;
import java.util.Scanner;
public class WhackAMoleGame {
public static void main(String[] args) {
Random random = new Random();
Scanner input = new Scanner(System.in);
int score = 0;
int totalAttempts = 0;
while (score < 10 && totalAttempts < 20) {
int hole1 = random.nextInt(9) + 1;
int hole2 = random.nextInt(9) + 1;
int hole3 = random.nextInt(9) + 1;
System.out.println("0 1 2 3 4 5 6 7 8");
System.out.println("1" + " " + "*" + " " + "*" + " " + "*");
System.out.println("2" + " " + "*" + " " + "*" + " " + "*");
System.out.println("3" + " " + "*" + " " + "*" + " " + "*");
System.out.println("Choose a hole to whack (enter two digits for row and column):");
int row = input.nextInt();
int column = input.nextInt();
if ((row == 1 && column == hole1) || (row == 2 && column == hole2) || (row == 3 && column == hole3)) {
System.out.println("You whacked a mole! +1 point\n");
score++;
} else {
System.out.println("No mole there. Try again.\n");
}
totalAttempts++;
}
if (score >= 10) {
System.out.println("You won! Final score: " + score + "/" + totalAttempts);
} else {
System.out.println("You lost. Final score: " + score + "/" + totalAttempts);
}
}
}
希望对你有所帮助!