java 有人看我的源代码的数字猜谜游戏,似乎一直失败[关闭]

mnowg1ta  于 11个月前  发布在  Java
关注(0)|答案(3)|浏览(82)

**已关闭。**此问题需要debugging details。目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
5年前关闭。
Improve this question
可能有人运行我的代码,并协助我调试我我有很多麻烦,试图弄清楚,我没有太多的指导,当谈到编码。
我的代码现在的问题是,它运行某些部分两次。请注解的问题和任何建议,以解决它。
简单介绍一下我想做的事情:猜数字游戏。这个游戏的想法是,计算机将生成一个随机数字,并询问用户是否知道这个数字。如果用户得到正确的答案,他们将得到一个祝贺消息,然后游戏将结束,但如果用户输入错误的数字,他们将得到一个重试消息,然后他们将再试一次

import javax.swing.*;

import java.lang.*;

public class Main {

public static void main(String[] args) {
   /*
   number guessing game
    */

    enterScreen();
    if (enterScreen() == 0){
        number();
        userIn();
        answer();
    }

}
public static int enterScreen (){
   String[] options = {"Ofcourse", "Not today"};
    int front = JOptionPane.showOptionDialog(null,
            "I'm thinking of a number between 0 and 100, can you guess what is is?",
            "Welcome",
            JOptionPane.YES_NO_OPTION,
            JOptionPane.PLAIN_MESSAGE,
            null, options, "Yes" );
    if(front == 0){
        JOptionPane.showMessageDialog(null, "Goodluck then, you might need it. :D");
    }
    else {
        JOptionPane.showMessageDialog(null, "okay i dont mind");
    }

    return front;
}
private static int number(){

    double numD;
    numD = (Math.random() * Math.random()) * 100;
    int numI = (int) numD;
    System.out.println(numD);

    return numI;
}
private static int userIn(){
    String userStr = JOptionPane.showInputDialog("What number do you think im thinking of?");
    int user = Integer.parseInt(userStr);

    return 0;
}
private static void answer(){
    // here is the problem
    if(userIn() == number()){
        JOptionPane.showMessageDialog(null, "Well done! You must be a genius.");
    }
    else {
        JOptionPane.showMessageDialog(null, "Shame, TRY AGAIN!");
        userIn();
    }
}

字符串
}

rfbsl7qr

rfbsl7qr1#

你的问题是这部分:

enterScreen();
        if (enterScreen() == 0) {
            number();
            userIn();
            answer();
        }

字符串
你可以省略第一个enterScreen()。因为你在if语句中再次调用了它。如果你看一下方法的返回类型:public static int,它返回和int。这使得方法的结果直接在if语句中可用。第一个enterScreen基本上没用,因为你不使用结果。你可以这样做:

int num = enterscreen();
if (num == 0) {
            number();
            userIn();
            answer();
        }


这基本上是相同的:

if (enterScreen() == 0) {
            number();
            userIn();
            answer();
        }

1dkrff03

1dkrff032#

你调用enterScreen()userIn()函数两次或两次以上。请计算机科学和编码。提示:计算机的执行指令从上到下。

xytpbqjk

xytpbqjk3#

你调用enterScreen()两次。只调用一次,然后比较只返回一次的值。

相关问题