我为一个班级写了代码,然后把它打开了。它很有效,但老师把它归还给我,因为它是一个类而不是一个主类和一个单独的类。我不知道如何分离代码。我是否必须重写整个内容,还是有一个简单的方法?
这是我的代码:
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");
}
}
(我希望这一切都格式正确;这是我的第一个问题)
答案 0 :(得分:0)
类可用于将“问题”或问题分成不同的代码段。你的老师希望你找到至少两个问题并将它们分成几个单独的课程。
突然出现的问题是
问题5目前占据了这个功能的最大空间,所以也许这是我们应该解决的第一个问题。
您的主要功能可能类似于
int answer = rand.nextInt(100);
guessResponder = new GuessResponder(answer);
do {
guess = input.nextInt();
System.out.println(guessResponder.respond(guess));
} while (guess != answer);
这将计算输出的问题转移到了一个单独的类GuessResponder
。
答案 1 :(得分:0)
试试这个,创建一个名为Calculate的类并创建一个方法calculateNumber传递你从用户那里获得的输入
提示:将您的班级名称更改为首字母大写 Guessmain
包numberguess;
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");
}
}
答案 2 :(得分:0)
在main方法中,有2个逻辑:
您可以将它们放入诸如Guess之类的类中,并将这2个逻辑放入方法中。
像,
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();
}
}
答案 3 :(得分: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");
}
}