📄 guessgame.java
字号:
//MOHD. SYUKRI BIN MOHD. MAHMUD
//BK08110222
//HC00
import javax.swing.JOptionPane;
import java.util.Random;
/** This is GuessNumber Game */
/**
Guessing game for guess a correct number beetween 1 to 100.
*/
public class GuessGame
{
public static void main(String[] args)
{
meth_dialog();
meth_loop();
System.exit(0);
}
/**This method is to greet the users before he or she start to play GuessNumber*/
/**Game instruction is given in this method*/
public static void meth_dialog()
{
JOptionPane.showMessageDialog(null, "WELCOME TO GUESSNUMBER GAME!");
JOptionPane.showMessageDialog(null, "In this game, you just need to guess the correct number. \nIf your answer is wrong,"+
" you will be given five times of chances to try again. \nAnd of course there's a hint for you." +
" make sure u can only insert a number from 0 to 100.");
JOptionPane.showMessageDialog(null, "ARE YOU READY?");
}
/**This method shows us the loop that tell the user if the user want to try again after the game end*/
public static void meth_loop()
{
String ans;
do
{
meth_compute();
ans = JOptionPane.showInputDialog("Would You Like To Guessing Again? (Y/N)");
} while (ans.equalsIgnoreCase("Y"));
}
/**This method shows that the program will give 4 more chances if the user fail to guess the correct number for the first time*/
/**The program will give hint for every single round and will congratule user if the user's answer is correct.*/
/**
@param num Random number that generated by this program.
@param guess Random number that guessed by user.
@param diff The absolute value of (num - guess).
@param chance The chances that given by this prgram to the user.
@param count The amount of try that the user input during the game.
@param input Hold the string for JOptionPane.showInputDialog.
*/
public static void meth_compute()
{
int num;
int guess;
int diff;
int chance = 5;
int count = 1;
String input;
Random randomNumbers = new Random();
num = randomNumbers.nextInt(101);
for (count = 1; count <= 5; count++)
{
input = JOptionPane.showInputDialog("Guess The Number, Pall");
guess = Integer.parseInt(input);
diff = Math.abs(num - guess);
if (guess >= 0 && guess <= 100)
{
if (diff == 0 )
{
JOptionPane.showMessageDialog(null, "Congratulations!");
count = 7;
}
if (diff >= 50)
{
if (guess > num)
{
JOptionPane.showMessageDialog(null, "It's Very High, Try Again!");
}
else
{
JOptionPane.showMessageDialog(null, "It's Very Low, Try Again!");
}
}
if (diff >= 30 && diff < 50)
{
if (guess > num)
{
JOptionPane.showMessageDialog(null, "It's High, Try Again!");
}
else
{
JOptionPane.showMessageDialog(null, "It's Low! Try Again!");
}
}
if (diff >= 15 && diff < 30)
{
if (guess > num)
{
JOptionPane.showMessageDialog(null, "It's Moderately High, Try Again!");
}
else
{
JOptionPane.showMessageDialog(null, "It's Moderately Low, Try Again!");
}
}
if (diff > 0 && diff < 15)
{
if (guess > num)
{
JOptionPane.showMessageDialog(null, "It's Somewhat High, Try Again!");
}
else
{
JOptionPane.showMessageDialog(null, "Its Somewhat Low, Try Again!");
}
}
if (count >= chance && count <= 6)
{
JOptionPane.showMessageDialog(null, "The Correct Number Is " + num);
}
if (count == 7)
{
JOptionPane.showMessageDialog(null, "You Guessed The Right Number Which Is " + num + "! Awesome!");
}
}
else
{
JOptionPane.showMessageDialog(null, "INVALID NUMBER! PLEASE GUESS BEETWEEN NUMBER 0 TO 100 ONLY!");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -