我的班级代码:http://notepad.cc/lureascu84 我的测试人员代码:http://notepad.cc/mammivo62
我被困在如何导入类程序中的代码以在测试器中使用。在我的简单数字猜测程序中,我有:
while (guess != num){
guess = scanner.nextInt();
if (guess > num){
System.out.println("The number you have entered is too high!");
numberOfGuesses++;
}
if (guess < num){
System.out.println("The number you have entered is too low!");
numberOfGuesses++;
}
}
System.out.println("You win with only " + numberOfGuesses + " wrong attempts!");
当使用访问器和mutator方法时,我会坚持写什么。
答案 0 :(得分:0)
我相信你所坚持的是实例化GuessingGame(int maxValue)
类。您需要创建一个将调用类的构造函数的新对象,然后您将能够执行其中包含的方法。
GuessingGame game = new GuessingGame(10);
上面从测试人员类创建了一个新的GuessingGame对象,类似于创建Random random = new Random()
,其中“maxValue”已设置为10.从此处可以进行方法调用。
game.guess(9);
会运行guess(int newGuess)
,并且可能会输出用户的猜测太低。
答案 1 :(得分:0)
这是我为“随机数猜”游戏制作的程序。 尽管代码可能看起来很长,但是非常简单。 特点:-
(1).1到10之间生成一个数字玩家可以通过以下方式重玩游戏: (2)。按'T'或在游戏结束时按'E'退出游戏... (3)。如果播放器输入任何错误的值,程序都不会得到... (4)。终止。玩家可以看到他们尝试的次数...希望能帮助您!
package FunProjects;
import java.util.*;
public class RandomGuessGame {
public static void main(String[] args) {
int num; //the guess number input by the user
double ran_num; //Random number generated by the game
char again; //press 't' or 'e' to try again or to exit the game
int i //variable which is being incremented for the variable 'tries'
Scanner scs = new Scanner(System.in);
do {
i = 1;
{
System.out.println("Random number between 0 - 10 has been generated");
Random rand = new Random();
ran_num = rand.nextInt(10);
do {
int tries = i++; // number of tries by the user
System.out.println("Enter Your Guess");
Scanner sc = new Scanner(System.in);
num = sc.nextInt();
if (num == ran_num)
{
System.out.println("*********************************");
System.out.println("*********************************");
System.out.println("Congratulations!!! Correct Guess :) ");
System.out.println("Your Guess --> " + num);
System.out.println("Number of tries = " + tries);
System.out.println("*********************************");
System.out.println("*********************************");
} else if (num < ran_num)
{
System.out.println("Wrong !!! :(");
System.out.println("Go Higher");
} else if (num > ran_num)
{
System.out.println("Wrong !!! :(");
System.out.println("Go Lower");
}
else if (num > 10 || num < 0)
{
System.out.println("Your guess exceed the limit ");
System.out.println("Enter your guess Between 1 - 10");
}
else
{
System.out.println("Undefined Output -_-");
}
} while (num != ran_num);
do
{
System.out.println("Press 'T' to try again");
System.out.println("Press 'E' to exit the game");
again = scs.next().charAt(0);
{
if (again != 't' && again != 'T' && again != 'e' && again != 'E')
{
System.out.println("Wrong Input ");
}
}
}
while (again != 't' && again != 'T' && again != 'e' && again != 'E');
if (again == 'e' || again == 'E')
{
System.exit(0);
}
}
}
while (again == 't' || again == 'T');
}
}