Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3. import java.util.Random;
  4. public class LoopsRandom
  5. {
  6.         public static void main(String[] args) {
  7.         Scanner input = new Scanner(System.in);    
  8.         int input1;  
  9.         int max;
  10.         //int input2;
  11.         //coin toss
  12.         do {
  13.         headsOrTails(); //calls method A
  14.         System.out.println("Enter \"1\" to return or \"2\" to continue: ");
  15.         input1 = input.nextInt();
  16.         while (input1 > 2 || input1 < 1) { //error message
  17.             System.out.println("Invalid input");
  18.             System.out.println("Enter \"1\" to return or \"2\" to continue: ");
  19.             input1 = input.nextInt();
  20.         }
  21.         } while (input1 == 1); //loops based on user input
  22.        
  23.         //random range
  24.         do {
  25.         System.out.println("Enter the minimum value: "); //user input range values
  26.         int min = input.nextInt();
  27.         System.out.println("Enter the maximum value: ");
  28.         max = input.nextInt();
  29.         randomInRange(min, max); //calls method B
  30.        
  31.         System.out.println("Enter \"1\" to return or \"2\" to continue: ");
  32.         input1 = input.nextInt();
  33.         while (input1 > 2 || input1 < 1) { //error message
  34.             System.out.println("Invalid input");
  35.             System.out.println("Enter \"1\" to return or \"2\" to continue: ");
  36.             input1 = input.nextInt();
  37.         }
  38.         } while (input1 == 1); //loops based on user input
  39.        
  40.         System.out.println("Guess the value between 0 and " + max + ", or enter any negative number to quit.");
  41.        
  42.         guessingGame(max); //calls method C
  43.        
  44.     }
  45.     public static void headsOrTails() { //method A
  46.         Scanner input = new Scanner(System.in);
  47.         System.out.println("Flipping coin... ");
  48.             Random r = new Random();
  49.             int i = r.nextInt(2); //heads or tails
  50.             String toss;
  51.             if (i == 00) { //heads
  52.                 toss = "heads";  
  53.             }
  54.             else { //tails
  55.                 toss = "tails";
  56.             }
  57.         System.out.println("The result of the coin toss is: " + toss);
  58.     }
  59.     public static void randomInRange(int min, int max) { //method B
  60.            
  61.           //random within the range of int min - int max  
  62.           Random r = new Random();
  63.           int rand = r.nextInt((max - min) + 1) + min; //random within range
  64.           System.out.println("A random number between " + min + " and " + max +" is: " + rand);
  65.     }
  66.     public static void guessingGame(int max) { //method C
  67.         Scanner input = new Scanner(System.in);
  68.         Random r = new Random();
  69.         int rand = r.nextInt((max) + 1); //random between 0 and max
  70.         int guessCount = 1;
  71.         //System.out.println(max + "(max value)");
  72.         System.out.println("(" + rand + " is the correct answer)"); //cheat so we know it's working
  73.         int guess;
  74.        
  75.        
  76.         while ((guess = input.nextInt()) != rand && guess >= 0) { //loop to prompt guess until user input is correct/negative
  77.        
  78.        
  79.         if (guess > rand) { //lower guess
  80.             System.out.println("Incorrect, guess lower.");
  81.         }
  82.        
  83.         else if (guess < rand && guess >= 0) { //higher guess
  84.             System.out.println("Incorrect, guess higher.");
  85.         }
  86.         guessCount++; //increases count
  87.         }
  88.         if (guess == rand) { //correct guess
  89.             System.out.println("Correct! You made " + guessCount + " guesses.");
  90.         }
  91.         else if (guess < 0) {
  92.         System.out.println("(exiting)"); //unnecessary line so i know the code works
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement