用用户输入抛硬币 JavaScript



编写一个程序来模拟抛硬币。首先,让用户"呼叫"或预测投掷。接下来,让用户知道您正在抛硬币。然后报告用户是否正确。

例:

 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 作为输入。

与其Random.nextInt(),我更喜欢nextBoolean()。不要在循环中重新声明您的Random。如果输入以h开头,则将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");
    }
}
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.");
            }
        }
    }
  }
}

最新更新