如何将随机数添加到java(二十一点)的总数中?

时间:2016-01-30 18:36:42

标签: java

这是我的代码:

import java.util.*;
import java.util.Scanner;

public class Assignment2 {
    public static void main(String args[]){ 
        Scanner stdin = new Scanner(System.in);

        Random random = new Random();     
        int ran2 = (random.nextInt(10));
        int ran1 = (random.nextInt(10));
        int total = ran1 + ran2;
        char exit = 'y';

        System.out.println("First cards: " + ran1 + ", " + ran2);
        System.out.println("Total: " + total);

        while(exit != 'n' && total < 21){
            System.out.println("Do you want another card? (y/n): ");
            exit = stdin.next().charAt(0);


            System.out.println("Card: "+ (random.nextInt(10)));
            total = total + (random.nextInt(10));
            System.out.println("Total: "+ total);


        }   

    }       
}

当我输入n时,如何让程序退出,而不是再次打印出来?

2 个答案:

答案 0 :(得分:1)

检查出来:

public class Assignment2 {
    public static void main(String args[]){ 
        int next = 0;
        Scanner stdin = new Scanner(System.in);

        Random random = new Random();     
        int ran2 = (random.nextInt(10));
        int ran1 = (random.nextInt(10));
        int total = ran1 + ran2;
        char exit = 'y';

        System.out.println("First cards: " + ran1 + ", " + ran2);
        System.out.println("Total: " + total);

        while(exit != 'n' && total < 21){
            System.out.println("Do you want another card? (y/n): ");
            exit = stdin.next().charAt(0);

            next = random.nextInt(10);
            System.out.println("Card: "+ next);
            total = total + next;
            System.out.println("Total: "+ total);


        }   
        if (exit.equals('n'))
            system.exit(0);

    }
}
  • 现在,通过调用system.exit(0)
  • 输入n后,该程序就存在了。
  • 您只需拨打nextInt一次,这样您就不会创建2个不同的随机数。因此,我将第一个调用放入变量next,以便您可以随意使用它,而无需再次调用nextInt
  • 如果您希望程序在用户输入n后立即退出 ,您需要将if语句放在exit = stdin.next().charAt(0);后面

答案 1 :(得分:1)

如果你想退出循环,你可以打破它。基本上你必须这样做(我写了评论以突出改变) -

import java.util.*;
import java.util.Scanner;

public class Assignment2 {
    public static void main(String args[]){ 
        Scanner stdin = new Scanner(System.in);

        Random random = new Random();     
        int ran2 = (random.nextInt(10));
        int ran1 = (random.nextInt(10));
        int total = ran1 + ran2;
        char exit = 'y';

        System.out.println("First cards: " + ran1 + ", " + ran2);
        System.out.println("Total: " + total);

        while(exit != 'n' && total < 21){
            System.out.println("Do you want another card? (y/n): ");
            exit = stdin.next().charAt(0);

            //you need to check here if the user entered 'n'. I have used a break opertion
            //to break from the loop and print the total outside the loop. But if you want
            //to exit the program altogether, just replace break with exit(0) :)
            if(total >= 21 or exit == 'y') {
              break;
            } 

            //As Idos correctly pointed out that by calling random.nextInt(10) two
            //times you have a very big chance of creating two different random numbers.
            //So it makes sense to put the first call into a variable nextNumber.
            int nextNumber = random.nextInt(10);
            total = total + (nextNumber);

            //Now we should again check for total. If it is greater than or equal to 21
            //I am again breaking from the loop. Feel free to replace break with exit(0).
            if(total >= 21) {
              break;
            }
            System.out.println("Total: "+ total);    

        }   
        System.out.println("Your total- "+ total);
    }       
}