Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.util.*;
- public class HighLow {
- public static void main(String[] args) {
- //variables
- int chances = 12;
- int selection;
- int comp = (int) Math.floor(Math.random() * (200) + 1);
- boolean playAgain = true;
- while (playAgain) {
- //input window for player
- JOptionPane.showMessageDialog(null, "This is a game of High-Low. This computer has chosen a number from 1 to 200 and you have to guess what that number is within 13 guesses.");
- //player's input , the loops
- for (chances = 12; chances > 0; chances--){
- String user = JOptionPane.showInputDialog(null, "What do you think the number is? Enter your guess.");
- int guess = Integer.parseInt(user);
- if (guess < comp) {
- System.out.println("Your guess is too low, try again");
- System.out.println("You now have " + chances + " tries left");
- } else
- if (guess > comp) {
- System.out.println("Your guess is too high, try again");
- System.out.println("You now have " + chances + " tries left");
- } else
- if (guess == comp) {
- System.out.println("Congrats, your guess is correct! The number was " + comp + ".");
- break;
- }
- if (chances == 0) {
- System.out.println("Your guess is incorrect.");
- System.out.println("Sorry, but you are out of chances!");
- break;
- }
- }
- //ask if the user wants to play again
- selection = JOptionPane.showConfirmDialog(null, "Do you want to play again?", "Otra vez!", JOptionPane.YES_NO_OPTION);
- playAgain = (selection == JOptionPane.YES_OPTION);
- }
- }
- }
Add Comment
Please, Sign In to add comment