我为一个班级写了代码,并把它上交了。它可以工作,但是老师把它还给了我,因为它是一个班级而不是一个主班和一个单独的班级。我不知道如何分离代码。我是否必须重写整个事情,还是有一个简单的方法?
这是我的代码:
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");
}
}
(我希望这一切格式正确;这是我在这里的第一个问题)