美好的一天程序员,
我刚刚在java netbeans中创建了一个猜数字游戏。这是我的源代码。
package guessthenumber1;
import java.util.Random;
import java.util.Scanner;
public class GuessTheNumber1 {
public static final int TriesAvailable = 5;
public static void main(String[] args) {
Random randNumber = new Random();
Scanner NumScan = new Scanner(System.in);
short LowNumber = 1;
short HighNumber = 10;
int RandomToGuess = randNumber.nextInt(HighNumber - LowNumber) + LowNumber;
short UserTries = 5;
short DisplayUserTries = 5;
short GuessNumber;
String[] NumTriesAvailable = new String[TriesAvailable];
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++Welcome to Guess The Number++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println(" ");
System.out.println("Difficulty: Beginner");
System.out.println("Guess The Number in 5 Tries");
System.out.println(" ");
for (int x = 0; x < 5; x++) {
GuessNumber = (short) NumScan.nextInt();
if (GuessNumber == RandomToGuess) {
System.out.println("Congratulations you got the correct number!");
break;
} else if (GuessNumber > RandomToGuess) {
UserTries--;
DisplayUserTries--;
System.out.println("Number Too High");
System.out.println(DisplayUserTries + "Tries remaining:");
if (UserTries == 0) {
System.out.println("GAMEOVER :P");
System.out.println("The number was: " + RandomToGuess);
break;
}
} else if (GuessNumber < RandomToGuess) {
UserTries--;
DisplayUserTries--;
System.out.println("Oops! Too Low");
System.out.println(DisplayUserTries + "Tries remaining:");
if (UserTries == 0) {
System.out.println("GAMEOVER :P");
System.out.println("The number was: " + RandomToGuess);
break;
}
} else {
System.out.println("Please input integerss only");
}
}
}
}
游戏很好,很有效。我刚为这个程序创建了一个GUI: 我的JPanel中的GUI具有按钮,标签和文本字段
我的一个问题是如何在此GUI中提供我的源代码功能。我知道它太复杂所以我只是问我如何在用户点击它时使该程序接受1号按钮,而不是使用扫描仪。 例如,当用户单击按钮时,程序将能够执行它并且在文本字段中显示太高或太低。我应该将代码放在按钮内还是Jpanel中的表单中。请帮助这是我的第一个UI游戏。 :)
感谢您的帮助和关注。 :)