0

Write a program to simulate a coin toss. First, ask the user to "call" or predict the toss. Next, let the user know you are tossing the coin. Then report whether the user was correct.

Example:

 Please call the coin toss (h or t): h

  Tossing...

 The coin came up heads. You win!

This is about what I am supposed to do. This is the code I have so far:

package inClassCh4Sec8to9;

import java.util.Random;
import java.util.Scanner;

public class ClassCh4Sec8to9 {

  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    while (true) {
        System.out.print("Enter you guess (1 for heads, 0 for tails, 2 to quit):");
        int call = input.nextInt();
        int heads = 1;
        int quit = 2;
        int tails = 0;
                
        if (call == quit) {
            break;
        } else if (call == heads) {
            
        } else if (call == tails) {
            
        } else {
            System.out.println("invalid");
            continue;
        }
        Random random = new Random();
        int coinflip = random.nextInt(2);
        if(call == coinflip){
            System.out.println("Correct!");
        }else{
            System.out.println("Sorry, incorrect.");
        }
            
    }
  }
}

My problems:

  1. I can get a random number no problem but it allows the h and t to be used as 1 and 0.
  2. I want h or heads to equal 1 as an input.
Community
  • 1
  • 1
  • 1
    Do you mean, you want the user to enter `h` or `t` instead of the numbers you're currently using? With inputs of `0` and `1`, this looks like it would work, if in a roundabout fashion? – Evan Knowles Mar 09 '18 at 06:08
  • that is the general idea yes. they input h or t and it makes the h = 1 and t =0 –  Mar 09 '18 at 06:09
  • 1
    Check this answer: https://stackoverflow.com/questions/11871520/how-can-i-read-input-from-the-console-using-the-scanner-class-in-java – Evan Knowles Mar 09 '18 at 06:10
  • i actually restarted this code just now and think i found a better way actually if you dont want to waist your time on this –  Mar 09 '18 at 06:10
  • @EvanKnowles thank you that also helps i appreciate it! –  Mar 09 '18 at 06:11

2 Answers2

1

Instead of Random.nextInt(), I would prefer nextBoolean(). Don't redeclare your Random in a loop. If the input starts with an h set a guess to true; otherwise, make sure it is valid (and set it false). Then flip the coin, and compare the result. Something like,

Scanner input = new Scanner(System.in);
Random random = new Random();

while (true) {
    System.out.print("Please call the coin toss (h or t): ");
    String call = input.nextLine().toLowerCase();
    boolean guess = call.startsWith("h"), coin = random.nextBoolean();
    if (call.startsWith("q")) {
        break;
    } else if (!guess && !call.startsWith("t")) {
        System.out.println("invalid");
        continue;
    }
    if ((guess && coin) || (!guess && !coin)) {
        System.out.printf("The coin came up %s. You win!%n", coin ? "heads" : "tails");
    } else {
        System.out.printf("The coin came up %s. You lose!%n", coin ? "heads" : "tails");
    }
}
Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226
-1
import java.util.Scanner;

public class A {

  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    while (true) {
        System.out.print("Please call the coin toss (h or t): ");
        String call = input.nextLine();
        String heads = "h";
        String tails = "t";

        if(call==null || call.length() > 1){
            break;
        }

        System.out.println("Tossing...");

        int random=(int)(Math.random()*2);

        if(random<1){ //assume that, if random variable is smaller than 1 then it is head. If bigger than 1 and smaller than 2, then tails.
            if(heads.equals(call)){
                System.out.println("The coin came up heads. You win!");
            }
            else{
                 System.out.println("Sorry, incorrect.");
            }
        }else{
            if(tails.equals(call)){
                System.out.println("The coin came up tails. You win!");
            }
            else{
                 System.out.println("Sorry, incorrect.");
            }
        }

    }
  }
}
yılmaz
  • 1,690
  • 10
  • 15
  • Thank you for this I already came up with one that is very similar to this! –  Mar 09 '18 at 06:19
  • is there away to allow multiple inputs like h and heads and other stuff using equalsIgnoreCase? –  Mar 09 '18 at 06:42
  • 1
    yes, at heads.equals(call) line you can do heads.equals(call) || "heads".equalsIgnoreCase(call) to support other inputs. But in this situation you should rethink how to exit from program since i wrote call==null || call.length() > 1 as exit condition for example purpose. – yılmaz Mar 09 '18 at 06:49
  • Your welcome, if it works for you, mark this answer as solution. – yılmaz Mar 09 '18 at 06:52
  • now it works with variation with heads and tails but not the single letters like t and h? –  Mar 09 '18 at 06:56
  • you should write condition as if(heads.equals(call) || "heads".equalsIgnoreCase(call)) – yılmaz Mar 09 '18 at 07:04
  • It's hard to know what was changed in this answer. Would you add an introductory paragraph to explain what the nature of the solution was, please? – halfer Mar 10 '18 at 19:58
  • @halfer it is already a small code. Also i had written a comment to explain how to evaluate random outcome. – yılmaz Mar 12 '18 at 05:57