简体   繁体   中英

How can i add the random number to my total for java(blackjack)?

This is my code:

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);


        }   

    }       
}

When I enter n , how can I make it so the program exit, instead of printing out the total again?

Check this out:

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);

    }
}
  • Now the program exists after you enter n by calling system.exit(0) .
  • You need to call nextInt just once, so you won't create 2 different random numbers. So I put the first call into a variable next so you could use it as many times as you please without having to call nextInt again.
  • If you want the program to exit immediately after the user enters n , you will want to put the if statement right after the exit = stdin.next().charAt(0);

If you want to exit the loop, you can break from it. Basically you have to do this(I have written comments to highlight the alterations)-

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM