**已关闭。**此问题需要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();
}
}
字符串
}
3条答案
按热度按时间rfbsl7qr1#
你的问题是这部分:
字符串
你可以省略第一个
enterScreen()
。因为你在if语句中再次调用了它。如果你看一下方法的返回类型:public static int
,它返回和int。这使得方法的结果直接在if语句中可用。第一个enterScreen
基本上没用,因为你不使用结果。你可以这样做:型
这基本上是相同的:
型
1dkrff032#
你调用
enterScreen()
和userIn()
函数两次或两次以上。请计算机科学和编码。提示:计算机的执行指令从上到下。xytpbqjk3#
你调用
enterScreen()
两次。只调用一次,然后比较只返回一次的值。