所以我的代码可以正常运行;没有问题,或者至少我认为它没有任何问题;我需要添加两个最后的内容:第一个是发出蜂鸣声的方法(认为我必须使用工具包,actionListener等),第二个是打印程序生成的当前随机数的方法(类似toString)。我已经尝试过教授的方法来发出蜂鸣声,但是什么也没有,所以现在我在这里。预先感谢。
尝试了我教授代码中的一些方法,但一无所获。
//Interface
package edu.pupr.LottoGame;
public interface Ask {
public void ask();
}
//Class
package edu.pupr.LottoGame;
import java.util.Random;
import javax.swing.JOptionPane;
public class theLotto {
//VARIABLE
public int values;
//OVERLOAD CONSTRUCTOR
public theLotto(int values) {
this.values = values;
}
//MUTATOR
public void setValues(int values) {
this.values = values;
}
//ACCESSOR
public int getValues() {
return values;
}
//ANONYMOUS CLASS
public void start() {
Ask ak = new Ask() {
public void ask() {
//ANONYMOUS VARIABLES USED
String input = null;
int reply;
int randomNum;
//BEGINNING OF FIRST DO-WHILE
do {
//INPUT OF USER : 4 DIGITS
input = JOptionPane.showInputDialog("Plese enter 4 digits (1000-9999)");
values = Integer.parseInt(input);
}while(values < 1000 || values > 9999);
//ENDING OF FIRST DO WHILE
//RANDOM NUMBER GENERATOR
Random rn = new Random();
randomNum = 1000 + rn.nextInt(10000 - 1000);
//DETERMINES IF THE USER WON OR LOST
if(values == randomNum)
JOptionPane.showMessageDialog(null, "You Won! The num: " + randomNum);
else
JOptionPane.showMessageDialog(null, "You Lost! The wining num was: " + randomNum);
//BEGINNING OF SECOND DO-WHILE
do {
//WANNA PLAY AGAIN PANEL
reply = JOptionPane.showConfirmDialog(null, "Continue?", null, JOptionPane.YES_NO_OPTION);
//IF OPTION IS YES
if (reply == JOptionPane.YES_OPTION) {
//BEGINNIG OF THIRD DO-WHILE
do {
//INPUT OF USER : 4 DIGITS
input = JOptionPane.showInputDialog("Plese enter 4 digits (1000-9999)");
values = Integer.parseInt(input);
}while(values < 1000 || values > 9999);
//END OF THIRD DO-WHILE
//RANDOM NUMBER GENERATOR
Random rn2 = new Random();
randomNum = 1000 + rn2.nextInt(10000 - 1000);
//DETERMINES IF THE USER WON OR LOST
if(values == randomNum)
JOptionPane.showMessageDialog(null, "You Won! The num: " + randomNum);
else
JOptionPane.showMessageDialog(null, "You Lost! The wining num was: " + randomNum);
}
//IF OPTION IS NO
else {
JOptionPane.showMessageDialog(null, "GOODBYE");
System.exit(0);
}
}while(reply != JOptionPane.NO_OPTION);
//END OF SECOND DO-WHILE
}
};
//PART OF ANONYMOUS
ak.ask();
}
}
//Class Driver
package edu.pupr.LottoGame;
public class theLottoDriver {
public static void main(String [] args) {
theLotto lt = new theLotto(0);
lt.start();
}
}
输出应该是这样的: 输入号码:3267
09/07/2019 09:50:23:
投掷#1:4392
投掷#2:8391
你输了!!!