0

我为一个班级写了代码,并把它上交了。它可以工作,但是老师把它还给了我,因为它是一个班级而不是一个主班和一个单独的班级。我不知道如何分离代码。我是否必须重写整个事情,还是有一个简单的方法?

这是我的代码:

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");
}
}

(我希望这一切格式正确;这是我在这里的第一个问题)

4

4 回答 4

0

在 main 方法中,有两个逻辑:

  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();
}
}
于 2013-11-14T04:30:58.733 回答
0

制作一个游戏逻辑类

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");
    }
}
于 2013-11-14T04:41:04.990 回答
0

类对于将“关注点”或问题分成不同的代码部分很有用。你的老师希望你找到至少两个问题,并将它们分成不同的班级。

跳出来的问题是

  1. 找到正确答案的随机数
  2. 循环获取用户的输入,直到猜测正确
  3. 计算用户正确猜测所需的猜测次数
  4. 显示说明和“You Win”消息
  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.

祝你好运!

于 2013-11-14T04:21:39.560 回答
0

试试这个,创建一个名为 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");
          }
      }
于 2013-11-14T04:22:03.437 回答