我正在完成一项学校作业。
作业是制作一个随机选择一个数字的程序,用户必须猜出这个数字。
程序应该保持用户提交输入的次数的计数(count var)。
我知道计数器被重置的问题来自方法的初始化步骤....如果我的count变量的值高于0,有没有办法绕过初始化步骤?
我已在下面提交了我的代码:
public static void main(String[] args) {
Random rand = new Random();
int randomnumber = rand.nextInt(50);
int machine = GuessMachine(Integer.parseInt(JOptionPane.showInputDialog("gok een getal")), randomnumber);
}
public static int CallPane(int Value, int rndNumber) {
int X1;
X1 = Integer.parseInt(JOptionPane.showInputDialog("gok een getal3"));
int machine = GuessMachine(X1, rndNumber);
return X1;
}
public static int GuessMachine(int InputNumber, int rndNumber) {
int value = InputNumber;
int randomNumber = rndNumber;
int count=0;
if (InputNumber == randomNumber) {
count =+1;
JOptionPane.showMessageDialog(null, "Goed! Je deed er "+count+" keer " );
int Response = JOptionPane.showConfirmDialog(null, "Nog een keer spelen?", "Bevestigen",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (Response == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null, "tot de volgende keer");
}
else if (Response == JOptionPane.YES_OPTION) {
Random rand = new Random();
int randomnumber = rand.nextInt(50);
//callpane
int machine = GuessMachine(Integer.parseInt(JOptionPane.showInputDialog("gok een getal")), randomnumber);
}
}
else if (InputNumber < randomNumber) {
JOptionPane.showMessageDialog(null, "te weinig");
int PaneInput = CallPane(value, randomNumber);
count+=1;
}
else if (InputNumber > randomNumber) {
// JOptionPane.showMessageDialog(null,"teveel", "information", >INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "teveel");
int PaneInput = CallPane(value, randomNumber);
count=+1;
}
return value;
}
----建议后更新代码---
public static int count=0;
public static void main(String[] args) {
// TODO code application logic here
Random rand = new Random();
int randomnumber = rand.nextInt(50);
int machine = GuessMachine(Integer.parseInt(JOptionPane.showInputDialog("gok een getal")), randomnumber);
JOptionPane.showMessageDialog(null, count);
}
public static int CallPane(int Value, int rndNumber) {
int X1;
X1 = Integer.parseInt(JOptionPane.showInputDialog("gok een getal3"));
int machine = GuessMachine(X1, rndNumber);
return X1;
}
public static int GuessMachine(int InputNumber, int rndNumber) {
int value = InputNumber;
int randomNumber = rndNumber;
if (InputNumber == randomNumber) {
count +=1;
JOptionPane.showMessageDialog(null, "Goed! Je deed er "+count+" keer " );
int Response = JOptionPane.showConfirmDialog(null, "Nog een keer spelen?", "Bevestigen",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (Response == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null, "tot de volgende keer");
}
else if (Response == JOptionPane.YES_OPTION) {
Random rand = new Random();
int randomnumber = rand.nextInt(50);
//callpane
int machine = GuessMachine(Integer.parseInt(JOptionPane.showInputDialog("gok een getal")), randomnumber);
}
}
else if (InputNumber < randomNumber) {
JOptionPane.showMessageDialog(null, "te weinig");
int PaneInput = CallPane(value, randomNumber);
count+=1;
}
else if (InputNumber > randomNumber) {
// JOptionPane.showMessageDialog(null,"teveel", "information", INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "teveel");
int PaneInput = CallPane(value, randomNumber);
count+=1;
}
return value;
}
}
答案 0 :(得分:1)
使count变量成为类成员:
public:
static int count;
然后在GuessMachine中,使用相同的名称删除局部变量。
答案 1 :(得分:1)
您在整个代码中所做的是错误输入 count = + 1; 此外,必须将count变量声明为类属性 public static int count = 0;
此外,您在调用CallPane()方法后递增计数,因此它永远无法递增!
这是错误,因此每次都会重置!用 count + = 1; 替换此语句,此外,还要通过从类中的任何位置递增来声明count as static
。它会工作正常!我已在下面显示: -
public class JavaApplication5 {
public static int count=0;
public static void main(String[] args) {
Random rand = new Random();
int randomnumber = rand.nextInt(50);
System.out.println("The random number is "+randomnumber);
int machine = GuessMachine(Integer.parseInt(JOptionPane.showInputDialog("Guess a number :- ")), randomnumber);
}
public static int CallPane(int Value, int rndNumber) {
int X1;
X1 = Integer.parseInt(JOptionPane.showInputDialog("Guess a number :- "));
int machine = GuessMachine(X1, rndNumber);
return X1;
}
public static int GuessMachine(int InputNumber, int rndNumber) {
int value = InputNumber;
int randomNumber = rndNumber;
if(InputNumber == randomNumber) {
count +=1;//error 1
JOptionPane.showMessageDialog(null, "Well! You did it "+count+" times." );
int Response = JOptionPane.showConfirmDialog(null, "Play again", "Confirm",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (Response == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null, "See you later!");
}
else if (Response == JOptionPane.YES_OPTION) {
Random rand = new Random();
int randomnumber = rand.nextInt(50);
//callpane
int machine = GuessMachine(Integer.parseInt(JOptionPane.showInputDialog("Guess a number :- ")), randomnumber);
}
}
else if (InputNumber < randomNumber) {
JOptionPane.showMessageDialog(null, "Insufficient!");
count+=1;//error 2
int PaneInput = CallPane(value, randomNumber);
}
else if (InputNumber > randomNumber) {
// JOptionPane.showMessageDialog(null,"teveel", "information", >INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "Excess!");
count+=1;//error 3
int PaneInput = CallPane(value, randomNumber);
}
return value;
}
}
答案 2 :(得分:1)
count +=1
或安全count = count + 1
count
需要是静态的,因为它必须保持对函数调用的价值GuessMachine
时,计数都会重置。想一想GuessMachine
将如何被一次又一次地召唤。这意味着您将一次又一次地在函数count = 0
的顶部执行GuessMachine
。您需要将count = 0
初始化为函数GuessMachine
并进入函数main
,因为即使您将其设置为静态,如果每次调用GuessMachine时都初始化它,{ {1}}将继续重置。您的代码还有另一个问题。在count
中的if
块中,有3次中有2次,GuessMachine
增加的方式为时已晚。它需要立即增加。事实上,它是完全相同的代码段,因此我们可以将其从count
块
if
我建议你逐步完成这个计划。尝试“干运行”并尝试使用调试器逐步执行每个语句,并了解此代码的控制流程。看起来你需要大量的练习来理解如何设计代码并对其进行调试。
答案 3 :(得分:0)
初始化count变量的方法如下所示:我做了一个count变量和两个方法initializeCount()和incrementalCount()。每次我得到一个count变量时,我都会通过方法incrementalCount()加1来更新它。当我需要再次初始化变量时,我使用initializeCount();
public class YourClass {
private static int count = 0;
public static void initializeCount(){
this.count = 0;
}
public static int incrementalCount(){
this.count++;
}
public static void main(String[] args) {
Random rand = new Random();
int randomnumber = rand.nextInt(50);
initializeCount(); // for initializing the count variable
int machine = GuessMachine(Integer.parseInt(JOptionPane.showInputDialog("gok een getal")), randomnumber);
}
...
//Then you can use.
public static int GuessMachine(int InputNumber, int rndNumber) {
int value = InputNumber;
int randomNumber = rndNumber;
// int count=0;
if (InputNumber == randomNumber) {
JOptionPane.showMessageDialog(null, "Goed! Je deed er "+incrementalCount()+" keer " );
....
else if (Response == JOptionPane.YES_OPTION) {
Random rand = new Random();
int randomnumber = rand.nextInt(50);
initializeCount(); // for initializing the count variable
//callpane
int machine = GuessMachine(Integer.parseInt(JOptionPane.showInputDialog("gok een getal")), randomnumber);
}
...
}
}
...
}