I am making a very simple blackjack game with one class and at least 2 methods. I want to take the random generated numbers that are being output (cards being dealt) and add them so I can make an if else statement to check if the total is over 21 causing the player to lose.
import java.util.*;
public class BlackJack {
private Scanner scan;
static Random generator = new Random();
public BlackJack(){
scan = new Scanner (System.in);
}
public static void main(String[] args) {
StarterCards();
Hit();
}
public static void Hit() {
System.out.println("Hit? (y or n)");
String yes = new String ("y");
Scanner input = new Scanner(System.in);
String yesorno = input.nextLine();
if(yesorno.equals(yes)) {
RandomCard();
Hit();
if()
} else {
System.out.println("Game Over");
}
}
public static void RandomCard() {
int card = generator.nextInt(10) + 1;
System.out.println("Card : "+ card);
int total2 = card;
}
public static void StarterCards() {
int card1 = generator.nextInt(10) + 1;
int card2 = generator.nextInt(10) + 1;
System.out.println("Card : "+ card1);
System.out.println("Card : "+ card2);
}
}
Re:
How to take integers being printed by a method?
The solution: don't have the method print anything. Instead have the method return the number, and let the calling code print it or do with the number whatever it wants to do.
i.e.,
public int myMethod() {
int someNumber = ....; // calculate it here
// System.out.println(someNumber); // don't print it here!
return someNumber
}