繁体   English   中英

如何将此代码分为两类? (猜游戏代码)

[英]How do i separate this code into two classes? (Guessing Game Code)

我为一个班级编写了代码,并将其上交。它可以工作,但是老师将其返回给我,因为它是一个班级,而不是主要班级和单独班级。 我不知道如何分离代码。 我需要重写整个内容,还是有一个简单的方法?

这是我的代码:

package numberguess;
import java.util.Random;
import java.util.Scanner;

public class guessmain 
{
public static void main(String[] args)
{
    Random rand = new Random();
    int answer = rand.nextInt(100);
    answer ++;
    int tries = 0;
    Scanner input = new Scanner (System.in);
    int guess;
    boolean win = false;

    while (win == false)
    {
        System.out.println("Guess a number between 1 and 100");
        guess = input.nextInt();

        if (guess > 100)
        {
            System.out.println("Your guess is out of the range");
        }
        else if (guess < 1)
        {
            System.out.println("Your guess is out of the range");
        }
        else if (guess == answer)
        {
            win = true;
            tries++;
        }
        else if (guess < answer)
        {
            System.out.println("Your guess is too low");
            tries++;
        }
        else if (guess > answer)
        {
            System.out.println("Your guess is too high");
            tries++;
        }

    }

    System.out.println("You Win!");
    System.out.println("It took you " + tries + " tries");
}
}

(我希望所有格式都正确;这是我的第一个问题)

类对于将“关注点”或问题分为不同的代码部分很有用。 您的老师希望您至少找到两个问题,然后将它们分开讲解。

我遇到的问题是

  1. 寻找正确答案的随机数
  2. 循环获取用户输入,直到猜对为止
  3. 计算用户正确猜测所需的猜测次数
  4. 显示说明和“您赢了”消息
  5. 为每个猜测确定适当的输出。

问题5目前在此功能中占用的空间最大,因此也许这是我们应该解决的第一个问题。

您的主要功能可能看起来像

int answer = rand.nextInt(100);
guessResponder = new GuessResponder(answer);

do {
    guess = input.nextInt();
    System.out.println(guessResponder.respond(guess));
} while (guess != answer);

这将计算输出的关注点转移到一个单独的类GuessResponder

祝好运!

尝试此操作,创建一个名为Calculate的类,并创建一个方法calculateNumber传递从用户那里获得的输入

提示 :将您的班级名称更改为首字母大写Guessmain

包裹号码猜测;

     import java.util.Random;
      import java.util.Scanner;

        public class guessmain {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    Calculate calculate = new Calculate();
    calculate.calculateNumber(input);
}

    }

     class Calculate {

void calculateNumber(Scanner input) {
    Random rand = new Random();
    int answer = rand.nextInt(100);
    answer++;
    int tries = 0;

    int guess;
    boolean win = false;

    while (win == false) {
        System.out.println("Guess a number between 1 and 100");
        guess = input.nextInt();

        if (guess > 100) {
            System.out.println("Your guess is out of the range");
        } else if (guess < 1) {
            System.out.println("Your guess is out of the range");
        } else if (guess == answer) {
            win = true;
            tries++;
        } else if (guess < answer) {
            System.out.println("Your guess is too low");
            tries++;
        } else if (guess > answer) {
            System.out.println("Your guess is too high");
            tries++;
        }

    }

    System.out.println("You Win!");
    System.out.println("It took you " + tries + " tries");
          }
      }

在main方法中,有2种逻辑:

  1. 随机产生答案
  2. 猜猜并打印信息。

您可以将它们放入诸如Guess之类的类中,并将这两种逻辑放入方法中。

喜欢,

import java.util.Random;
import java.util.Scanner;

public class Guess {

private int answer = 0;
int tries = 0;
Scanner input = new Scanner(System.in);
int guess;
boolean win = false;

public Guess() {
    answer = generateRandomNumber();
}

private int generateRandomNumber() {
    Random rand = new Random();
    return rand.nextInt(100) + 1; // 1 ~ 100
}

public void doGuess() {
    while (!win) {
        System.out.println("Guess a number between 1 and 100");
        guess = input.nextInt();

        if (guess > 100) {
            System.out.println("Your guess is out of the range");
        } else if (guess < 1) {
            System.out.println("Your guess is out of the range");
        } else if (guess == answer) {
            win = true;
            tries++;
        } else if (guess < answer) {
            System.out.println("Your guess is too low");
            tries++;
        } else if (guess > answer) {
            System.out.println("Your guess is too high");
            tries++;
        }
    }

    System.out.println("You Win!");
    System.out.println("It took you " + tries + " tries");
}
}

那么非常简单地创建一个Guess实例并调用相应的方法。

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

    new Guess().doGuess();
}
}

使游戏逻辑类像

import java.util.Random;
import java.util.Scanner;

public class GuessLogic {

    Scanner input = new Scanner(System.in);
    boolean win;

    public boolean guess() {
        System.out.println("Guess a number between 1 and 100");
        int guess = input.nextInt();
        int answer = new Random().nextInt(100);
        win = false;
        if (guess > 100) {
            System.out.println("Your guess is out of the range");
        } else if (guess < 1) {
            System.out.println("Your guess is out of the range");
        } else if (guess == answer) {
            win = true;
        } else if (guess < answer) {
            System.out.println("Your guess is too low");
        } else if (guess > answer) {
            System.out.println("Your guess is too high");
        }
        return win;
    }

}

还有像

public class GuessMain {
    public static void main(String[] args) {
        GuessLogic guessLogic = new GuessLogic();
        int tries = 1;
        while (!guessLogic.guess()) {
            tries++;
        }
        System.out.println("You Win!");
        System.out.println("It took you " + tries + " tries");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM