Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- import java.util.Random;
- public class LoopsRandom
- {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int input1;
- int max;
- //int input2;
- //coin toss
- do {
- headsOrTails(); //calls method A
- System.out.println("Enter \"1\" to return or \"2\" to continue: ");
- input1 = input.nextInt();
- while (input1 > 2 || input1 < 1) { //error message
- System.out.println("Invalid input");
- System.out.println("Enter \"1\" to return or \"2\" to continue: ");
- input1 = input.nextInt();
- }
- } while (input1 == 1); //loops based on user input
- //random range
- do {
- System.out.println("Enter the minimum value: "); //user input range values
- int min = input.nextInt();
- System.out.println("Enter the maximum value: ");
- max = input.nextInt();
- randomInRange(min, max); //calls method B
- System.out.println("Enter \"1\" to return or \"2\" to continue: ");
- input1 = input.nextInt();
- while (input1 > 2 || input1 < 1) { //error message
- System.out.println("Invalid input");
- System.out.println("Enter \"1\" to return or \"2\" to continue: ");
- input1 = input.nextInt();
- }
- } while (input1 == 1); //loops based on user input
- System.out.println("Guess the value between 0 and " + max + ", or enter any negative number to quit.");
- guessingGame(max); //calls method C
- }
- public static void headsOrTails() { //method A
- Scanner input = new Scanner(System.in);
- System.out.println("Flipping coin... ");
- Random r = new Random();
- int i = r.nextInt(2); //heads or tails
- String toss;
- if (i == 00) { //heads
- toss = "heads";
- }
- else { //tails
- toss = "tails";
- }
- System.out.println("The result of the coin toss is: " + toss);
- }
- public static void randomInRange(int min, int max) { //method B
- //random within the range of int min - int max
- Random r = new Random();
- int rand = r.nextInt((max - min) + 1) + min; //random within range
- System.out.println("A random number between " + min + " and " + max +" is: " + rand);
- }
- public static void guessingGame(int max) { //method C
- Scanner input = new Scanner(System.in);
- Random r = new Random();
- int rand = r.nextInt((max) + 1); //random between 0 and max
- int guessCount = 1;
- //System.out.println(max + "(max value)");
- System.out.println("(" + rand + " is the correct answer)"); //cheat so we know it's working
- int guess;
- while ((guess = input.nextInt()) != rand && guess >= 0) { //loop to prompt guess until user input is correct/negative
- if (guess > rand) { //lower guess
- System.out.println("Incorrect, guess lower.");
- }
- else if (guess < rand && guess >= 0) { //higher guess
- System.out.println("Incorrect, guess higher.");
- }
- guessCount++; //increases count
- }
- if (guess == rand) { //correct guess
- System.out.println("Correct! You made " + guessCount + " guesses.");
- }
- else if (guess < 0) {
- System.out.println("(exiting)"); //unnecessary line so i know the code works
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement