0

编写一个程序来模拟抛硬币。首先,要求用户“跟注”或预测抛掷。接下来,让用户知道你在抛硬币。然后报告用户是否正确。

例子:

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

  Tossing...

 The coin came up heads. You win!

这是关于我应该做的事情。这是我到目前为止的代码:

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.");
        }
            
    }
  }
}

我的问题:

  1. 我可以得到一个随机数没问题,但它允许 h 和 t 用作 1 和 0。
  2. 我希望 h 或 head 等于 1 作为输入。
4

2 回答 2

1

而不是Random.nextInt(),我宁愿nextBoolean()。不要Random在循环中重新声明你的。如果输入以h集合 a开头guess,则为true; 否则,请确保它有效(并设置它false)。然后翻转coin,并比较结果。就像是,

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");
    }
}
于 2018-03-09T06:22:44.390 回答
-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.");
            }
        }

    }
  }
}
于 2018-03-09T06:18:15.123 回答