java - 类、方法和随机数生成

标签 java class random methods numbers

You will write a Java program to play the game of Pico, Fermi, Bagel. Here are the rules of the game:

The computer will generate a "secret" three digit number at random. The first number will not be 0, and all the digits will be different. The user tries to guess the number. If the user guesses correctly, then the game is over.

If not, the computer gives a hint and the player tries again. The hints:

  • for each digit that matches the secret number in the proper place, the computer prints "Fermi"

  • for each digit that matches, but not in the proper place, the computer prints "Pico"

  • if none of the digits match, the computer prints "Bagels"

The program will have a main class and a Bagels class. The bagels class will call 3 other methods to

  1. generate the secret number

  2. determine whether the current guess is a winner

  3. evaluate the current guess and print hints

我的问题 -- 当我运行程序时,它要求我输入 3 位数的代码,但随后它只是不断要求我输入 3 位数的代码,而不是比较用户的猜测数字到随机生成的数字。我假设我正在对这些类或方法做一些愚蠢的事情,因为我们不久前刚刚学习它们,但它们仍然让我感到困惑。

程序的第一部分是我的主类,第二部分是 Bagels 类。

package assignment.iii;
import javax.swing.JOptionPane;
import java.util.Scanner; 

public class AssignmentIII {

public static void main(String[] args) 
   {
   int playagain = JOptionPane.showConfirmDialog(null, "Would you like to play?", "Message", JOptionPane.YES_NO_OPTION);

    while (playagain == JOptionPane.YES_OPTION) {      

     Bagels myBagels = new Bagels();
     myBagels.playGame() ;     
     myBagels.randNumber = 0;
     playagain = JOptionPane.showConfirmDialog(null, "Would you like to play again?", "Message", JOptionPane.YES_NO_OPTION);
}
}
}



 // start of Bagels class

package assignment.iii;
import java.util.Random;
import javax.swing.JOptionPane;

public class Bagels{


public int randNumber; 
private int Guess; 
private int Rand1, Rand2, Rand3;
private int Guess1, Guess2, Guess3;
private int guessCount;


public void playGame() 
{

    do {
    Guess = Integer.parseInt(JOptionPane.showInputDialog("Enter a three digit number"));
    }
    while (Guess != randNumber);                   

 }

private int generateSecretNumber()
{
         Random randN = new Random();
         return randN.nextInt(999)+1; 
}  

private void printHint(String guess)        
{
   if(randNumber == Guess)
        System.out.println("Correct");

      else
         {
         Guess1 = (Guess)/100;
         Guess2 = (Guess%100)/10;
         Guess3 = (Guess%100)%10;
         }

    if(Guess1 == Rand1)
    {
        System.out.println("Fermi");
    }
    if(Guess2 == Rand2)
    {
        System.out.println("Fermi");
    }
    if(Guess3 == Rand3 )
    {
        System.out.println("Fermi");
    }
    if(Guess2 == Rand1)
    {
        System.out.println("Pico");
    }
    if(Guess3 == Rand1)
    {
        System.out.println("Pico");
    }
    if(Guess1 == Rand2)
    {
        System.out.println("Pico");
    }
    if(Guess3 == Rand2)
    {
        System.out.println("Pico");
    }
    if(Guess1 == Rand3)
    {
        System.out.println("Pico");
    }
    if(Guess2 == Rand3)
    {
        System.out.println("Pico");
    }
    else if(Guess1 != Rand1 && Guess1 != Rand2 && Guess1 != Rand3 &&
            Guess2 != Rand1 && Guess2 != Rand2 && Guess2 != Rand3 &&
            Guess3 != Rand1 && Guess3 != Rand2 && Guess3 != Rand3)
    {
        System.out.println("Bagels");
    }
    guessCount++;
   }
}

最佳答案

未经本人测试,看起来错误确实出在您的Assignment 类的main 方法中。当您创建 bagel 对象时,首先发生的事情是调用它的 playGame 方法。

Bagels myBagels = new Bagels();
myBagels.playGame() ;

您的 playGame 方法依赖于与 randNumber 的比较,但 randNumber 永远不会被初始化! Java 成员变量有一个默认值,整数为 0(参见: Default Values and Initialization in Java )。因此除非用户输入“0”,否则循环条件将为真(猜测不等于0)并且将再次提示用户。

我提出的一个建议是编写一个采用单个 int 值的构造函数,并将 randNumber 设置为该值。

关于java - 类、方法和随机数生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26457267/

相关文章:

java - 导入 Google I/O 源代码? Mercurial 克隆

java - Android 上的相互身份验证失败 w/javax.net.ssl.SSLHandshakeException : Handshake failed

java - 使用来自另一个子类的子类方法,横向类型转换?

CSS 类(求助!)

java - 无法从静态上下文中引用非静态方法 <T>findViewById(int)

c - 如何生成 _ _ _ 形式的随机数。 _ _ _ _(C语言)

random - 跳过没有随机化的列表?

java - 使用 Java 对启用 Kerberos 的 Hadoop 集群进行身份验证

Java二分查找词树

c++ - 如何从数组中消除重复数字,并将数组大小调整为 C++ 中的新元素数