java游戏,有多种选择

java游戏,有多种选择,java,Java,我正在用Java制作一个猜谜游戏,我需要添加一个选项来计算猜谜的次数,但是如果玩家多次给出相同的答案,则将其计算为1次尝试 我不知道如何继续。任何帮助都将不胜感激:) 以下是我当前的脚本: import java.util.Scanner; public class GuessTheNumber { public static void main(String args[]) { Scanner keyboard = new Scanner(System

我正在用Java制作一个猜谜游戏,我需要添加一个选项来计算猜谜的次数,但是如果玩家多次给出相同的答案,则将其计算为1次尝试

我不知道如何继续。任何帮助都将不胜感激:)

以下是我当前的脚本:

import java.util.Scanner;

public class GuessTheNumber {
        public static void main(String args[]) {
            Scanner keyboard = new Scanner(System.in);

            int count = 0;
            int a = 1 + (int) (Math.random() *9);
            int guess = 0;

            System.out.printf("Guess the number from 1 - 10: ");

            while (guess != a) {
                guess = keyboard.nextInt();
                count++;
                if (guess > a) {
                    System.out.printf("Lower!: ");
                } else if (guess < a) {
                    System.out.printf("Higher!: ");
                }
            }
            System.out.println("Congratulations! You guessed the number with "
                + count + " tries.");
        }
}
import java.util.Scanner;
公共类猜测编号{
公共静态void main(字符串参数[]){
扫描仪键盘=新扫描仪(System.in);
整数计数=0;
int a=1+(int)(Math.random()*9);
int guess=0;
System.out.printf(“从1-10猜数字:”);
while(猜!=a){
guess=keyboard.nextInt();
计数++;
如果(猜测>a){
System.out.printf(“Lower!:”);
}else if(猜测
您需要使用列表跟踪所有用户的答案,以便在递增之前,如果存在类似的答案,您可以迭代到该列表

给你,朋友

public class GuessTheNumber {
public static void main(String args[]) {
    Scanner keyboard = new Scanner(System.in);
    ArrayList<Integer> answers = new ArrayList<Integer>();
    ;
    int count = 0;
    int a = 1 + (int) (Math.random() * 9);
    int guess = 0;

    System.out.printf("Guess the number from 1 - 10: ");

    while (guess != a) {
        guess = keyboard.nextInt();
        boolean isAnswered = false;
        for (Integer answer : answers) {
            if (guess == answer) {
                isAnswered = true;
                break;
            }
        }
        if (!isAnswered) {
            count++;
            answers.add(guess);
        }
        if (guess > a) {
            System.out.printf("Lower!: ");
        } else if (guess < a) {
            System.out.printf("Higher!: ");
        }


    }
    System.out.println("Congratulations! You guessed the number with "
            + count + " tries.");
      }
     }
公共类猜测编号{
公共静态void main(字符串参数[]){
扫描仪键盘=新扫描仪(System.in);
ArrayList answers=新的ArrayList();
;
整数计数=0;
int a=1+(int)(Math.random()*9);
int guess=0;
System.out.printf(“从1-10猜数字:”);
while(猜!=a){
guess=keyboard.nextInt();
布尔值isAnswered=false;
for(整数答案:答案){
如果(猜测=答案){
isAnswered=true;
打破
}
}
如果(!isAnswered){
计数++;
答案:添加(猜测);
}
如果(猜测>a){
System.out.printf(“Lower!:”);
}else if(猜测
为什么要同时标记java和javascript?它们是完全不同的语言,不能协同工作。如果用户输入
123
,它会被算作5次猜测还是3次猜测?java和javascript完全不相关——其中一种基本上是一个玩具,用于编写小代码,传统上被经验不足的程序员使用和滥用。另一个是用于web浏览器的脚本语言。如果我添加了“javascript”标记,我很抱歉。我的错。你能键入代码吗?非常感谢!