Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Numberguessing类和客户端_Java - Fatal编程技术网

Java Numberguessing类和客户端

Java Numberguessing类和客户端,java,Java,我正在尝试编写一个数字猜测类和客户端 我在这个类/客户机上遇到的问题是,我的数字猜测要么太高要么太低,最重要的是,它会在应该循环一次的时候循环两次 Number generated from 0 to 100. Wanna take a guess(enter 0 to give up) Let's take a guess: 50 40 Your guess is too low 我可以做哪些可能的更改来改进整个循环或更改 这是我的代码,供任何想看的人使用 import java.util

我正在尝试编写一个数字猜测类和客户端

我在这个类/客户机上遇到的问题是,我的数字猜测要么太高要么太低,最重要的是,它会在应该循环一次的时候循环两次

Number generated from 0 to 100. Wanna take a guess(enter 0 to give up)
Let's take a guess: 
50
40
Your guess is too low
我可以做哪些可能的更改来改进整个循环或更改

这是我的代码,供任何想看的人使用

import java.util.Random;

public class NumberGuess
{

  private Random generator;
  private int Number;


  int intGuess= (1 + (int)(Math.random()*100));
  int numGuess=0;
  boolean isGuessCorrect=false;

  public NumberGuess(){
  }

  int numguess;
  public int guess(int guessIn){
    int numguess=guessIn;
    if(numguess>intGuess){
      return 1;
    }else if(isGuessCorrect){
      return 0;
    }else{
      return -1;
    }
  }

  public int getNumberofGuesses(){
    return numGuess;
  }

  public boolean gameIsComplete(){
    if(isGuessCorrect){
      return true;
    }else{
      return false;
    }
  }

  public void reset(){
    intGuess=(1 + (int)(Math.random()*100));
    numGuess=0;
    isGuessCorrect=false;
  }
}
客户端类

import java.util.Scanner;

public class NumberGuessclient{
    public static void main(String[] args){

      NumberGuess game1=new NumberGuess();
      Scanner scan = new Scanner(System.in);            

      int quit=1;
      while(quit != 0) {
        System.out.println("Number generated from 0 to 100. Wanna take a guess(enter 0 to give up)");
        System.out.println("Let's take a guess: ");

        int guess1= scan.nextInt(); 

    while((guess1 != 0)||(!game1.gameIsComplete())) {
            guess1 = scan.nextInt();
            if (game1.guess(guess1)==1){
              System.out.println("Your guess is too high");
            }
            else if(game1.guess(guess1)==-1) {
                      System.out.println("Your guess is too low");
                }
                else {                      System.out.println("guessed in " + game1.getNumberofGuesses() + " tries");
                }
            }
            System.out.println("Enter 1 for new game, 0 to quit: ");
            quit = scan.nextInt();
            if(quit==1){
                game1.reset();
            }
        }
    }
}
如果上面没有说明,则需要对源代码进行详细说明

NumberGuess类:

NumberGuess类将促进数字猜测游戏。构造函数应该生成一个随机数,并将该数保存在私有类字段中。该类还应定义一个方法,该方法接受“猜测”,将“猜测”与随机生成的数字进行比较,并返回以下值之一:

•   -1 the guess was less than the secret number
•    0 the guess matched the secret number
•    1 the guess was higher than the secret number 
确定其他方法(构造函数或其他方法)是否对此类有用

JavaAPI定义了一个用于生成随机数的随机类。从第250页开始,可以在API或教科书中对课程进行复习。考虑限制随机数的范围。例如,一个介于0和100之间的数字

客户端应用程序:

客户端应用程序允许最终用户玩数字猜测游戏。下面是一个运行示例。您的应用程序不需要匹配。 我想的是一个介于0和100之间的数字。你能猜到吗

Take a guess: 50
Your guess is too high
Another guess? (Y or N): y
Take a guess: 25
Your guess is too high
Another guess? (Y or N): y
Take a guess: 10
Your guess is too low
Another guess? (Y or N): y
Take a guess: 15
Your guess is too low
Another guess? (Y or N): y
Take a guess: 18
Your guess is too low
Another guess? (Y or N): y
Take a guess: 20
Congratulations! You correctly guessed the secret number in 6 tries.

你可以改变很多事情,例如:

  • 无需每次循环多次调用
    game1.guess(guess1)
  • 完成方法相当长
  • 在进入while循环之前,应该调用
    scan.nextInt()
  • 我假设最后一个输出是手动生成的,因为您从未递增
    numguess
  • guess(int)
    方法不起作用
我通常不是一个做作业的人。。。但是(我今天过得很好…!):


现在有一条评论:我记得这个游戏的“乐趣”是,你应该在最多10次尝试中“猜测”数字,你可以实现…

等等,我怎么做,我接受答案,但不知道怎么做。@blake回答你在答案分数下问的问题(答案左边的数字)你可以点击一个复选标记来接受这个答案。哦,我明白了。我已经接受了我昨天发布的一些问题。这就是你接受的方式。现在接受你前面问题中的最佳答案,你会让所有人都高兴的。好的,开始代码吧!为什么同时使用Math.random和util.random?使用util.Random和generator.nextInt(100);这场比赛。@Riley Lark是100个号码7次试跳吗?我不记得了!jejeje我相信这个游戏会更有趣,作为这个例子的一个倒数,比如,你想一个数字,程序做“猜测”=P
import java.util.Random;
import java.util.Scanner;

public class NumberGuessclient {

    private static final String[] ANS = {
        "Your guess is too low\n", 
        "guessed in %d tries\n", 
        "Your guess is too high\n"
    };

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

        while (true) {
            NumberGuess game = new NumberGuess();
            System.out.println("Number generated from 0 to 100. Wanna take a guess(enter 0 to give up)");
            System.out.println("Let's take a guess: ");

            while (!game.isGameComplete()) {
                System.out.format(ANS[game.guess(scan.nextInt())+1], game.getNumberofGuesses());
            }

            System.out.println("Enter 1 for new game, 0 to quit: ");

            if (scan.nextInt() != 1) {
                System.out.println("Bye!");
                System.exit(0);
            }
        }
    }
}

class NumberGuess {
    private static final Random RAND_GENERATOR = new Random(System.nanoTime());
    int intGuess = RAND_GENERATOR.nextInt(101);
    int numGuess = 0;
    boolean isGuessCorrect = false;

    public int guess(int guessIn) {
        numGuess++;
        if (guessIn > intGuess) {
            return 1;
        } else if (guessIn == intGuess) {
            isGuessCorrect = true;
            return 0;
        } else {
            return -1;
        }
    }

    public int getNumberofGuesses() {
        return numGuess;
    }

    public boolean isGameComplete() {
        return isGuessCorrect;
    }
}