import java.util.Scanner;
public class Blackjack {
class Commands {
static final String yes = "yes";
static final String no = "no";
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//Opponent player2 = new Opponent();
double AiCard1 = 0;
double AiCard2 = 0;
double AiCard3 = 0;
double AiTotalcard3 = AiCard1 + AiCard2 + AiCard3;
double card1 = 0;
double card2 = 0;
double card3 = 0;
double card4 = 0;
double total2 = card1 + card2 + card3 + card4;
double total = card1 + card2 + card3;
System.out.println("Hello and Welcome to my custom version of blackjack!");
System.out.println("You will start off with $300");
System.out.println();
System.out.println("Do you want to read the rules before playing?");
System.out.print("Press 1 if yes, press 2 for no");
int choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("You have 4 cards to get as close to 21 as possible. Whoever is closest to 21 wins");
break;
default:
break;
}
int balance = 300;
System.out.println("Your bank balance is $" + balance);
//user places a bet
System.out.println("Place a bet");
int bet = input.nextInt();
System.out.println("You placed a bet of " + bet);
//this is the AI
AiCard1 = Math.random() * 12 + 1;
AiCard1 = Math.round(AiCard1);
AiCard2 = Math.random() + 12 + 1;
AiCard2 = Math.round(AiCard2);
double AiTotal2cards = AiCard1 + AiCard2;
if(AiTotal2cards < 15) {
AiCard3 = Math.random() * 12 + 1;
AiCard3 = Math.round(AiCard3);
AiTotalcard3 = AiCard1 + AiCard2 + AiCard3;
}
card1 = Math.random() * 12 + 1;
card1 = Math.round(card1);
System.out.println("Your first card was a " + card1);
System.out.println();
card2 = Math.random() * 10 + 1;
card2 = Math.round(card2);
System.out.println("Your second card was a " + card2);
System.out.println();
System.out.println("Your cards add up to " + (card1 + card2));
System.out.println("Do you want to add another card?");
String answer = input.next();
here:
if(answer.equals(Commands.yes)) {
card3 = Math.random() * 12 + 1;
card3 = Math.round(card3);
System.out.println("Your third card was a " + card3);
System.out.println("Your cards add up to " + (card1 + card2 + card3));
total = card1 + card2 + card3;
if(total > 21) {
System.out.println("You lose");
break here;
}
System.out.println("Do you want to add another card?");
String answer1 = input.next();
if(answer1.equals(Commands.no)){
break here;
}
if(answer1.equals(Commands.yes)) {
card4 = Math.random() * 12 + 1;
card4 = Math.round(card4);
System.out.println("Your fourth card was a " + card4);
System.out.println("Your cards add up to " + (card1 + card2 + card3 + card4));
total2 = card1 + card2 + card3 + card4;
if(total2 > 21) {
System.out.println("You lose");
break here;
}
break here;
}
}
System.out.println("Your total cards were " + total2);
System.out.println("AI total cards were " + AiTotalcard3);
input.close();
}
}
When you run the program I was hoping that the variables at the top would have new numbers saved into them when the user went through the program. Is there a better way to do this because the console output is always 0 for the total.
First lets look at AiTotalcard3:
It shouldn't always output 0 for that, more precisely it will output 0 in the case where AiCard1 + AiCard2 >= 15, which probably happens a lot.
It makes sense if you trace through your actual code. The first thing that happens is those initializers:
double AiCard1 = 0;
double AiCard2 = 0;
double AiCard3 = 0;
double AiTotalcard3 = AiCard1 + AiCard2 + AiCard3;
So AiTotalcard3 is initially set to 0. It will remain 0 until you change it, which happens here:
AiCard1 = Math.random() * 12 + 1;
AiCard1 = Math.round(AiCard1);
AiCard2 = Math.random() + 12 + 1;
AiCard2 = Math.round(AiCard2);
double AiTotal2cards = AiCard1 + AiCard2;
if(AiTotal2cards < 15) {
AiCard3 = Math.random() * 12 + 1;
AiCard3 = Math.round(AiCard3);
AiTotalcard3 = AiCard1 + AiCard2 + AiCard3;
}
But that only happens if the first two cards add up to less than 15. After that you never touch AiTotalcard3 again.
Your logic can be greatly simplified all around, and also it makes a lot more sense to use int instead of double for the cards, but ignoring all that and sticking to your current style, you could fix this by making sure all paths set AiTotalcard3, for example (again not the cleanest logic but just sticking to your pattern):
if(AiTotal2cards < 15) {
AiCard3 = Math.random() * 12 + 1;
AiCard3 = Math.round(AiCard3);
AiTotalcard3 = AiCard1 + AiCard2 + AiCard3;
} else {
AiTotalcard3 = AiCard1 + AiCard2; // be sure to always set it
}
Now, with all that in mind, you have a similar problem with total2: Not all of your execution paths set the value of total2. It works if you make it up to the 4th card but prior to that you never change it from its initial value of 0. So you're just going to have to go through this with a fine-toothed comb.
One of your fundamental issues is for some reason you're using different variables all over the place for the first 2 cards vs. all the cards. If you just used a single aiTotal and total to accumulate the card totals as you go, you'd avoid most of these issues.
I think your main issue is confusing total and total2. You seem to use both interchangeable in multiple places.
At first, if the user does not pick a third card, total2 will always equal 0 because if the if(answer.equals(Commands.yes)) { is never entered, total2 is never set past the original initialization of 0.
If the user picks a third card but not a fourth, you update the value of total correctly but not total2.
If the user picks a fourth card, then it should work correctly.
Besides the main fixes, here are some tips that you should keep in mind when writing your game:
You mention that the global variables are not recognized, but none of your variables are actually global. You should read up on what exactly that means.
I would suggest using ints or a custom datatype to store card values, as you would never need the decimal places a double provides.
Use loops in your structure to save a lot of code. It will make it cleaner and easier to understand.
Print what the AI is doing. There's no point to having an AI if you can't see its moves.
Anyway, good luck with your game! Programming can be intimidating at first but this is a great start.
Related
First of all, I have found two other threads that have similar questions. There problem is that they didn't use the right equal sign for string and not formatting the if statements correctly for their particular problem.
For my assignment, I need to create a game called Pig where the player verses a computer in getting 100 points first in rolling pairs of dice. If the player rolls 1 on one turn, they get no additional points. If the player rolls two 1's, then they lose all their points. I have not coded the computer's turn yet, just focusing on the player. Please tell me what I am doing wrong. Thank you very much in advance.
import java.util.Scanner;
public class FourFive
{
public static void main (String[] args)
{
Pigs myopp = new Pigs();
Scanner scan = new Scanner (System.in);
final int Round = 20;
int num1, num2;
int roundTotal = 0;
int playerTotal = 0;
int compTotal = 0;
int win = 100;
int turnOver = 1;
Pigs die1 = new Pigs();
Pigs die2 = new Pigs();
String play = "y";
System.out.println("Type y to play");
play = scan.nextLine();
while (play.equalsIgnoreCase("y"))
{
for (int roll = 1; roll <= 6; roll++)//Each die has 6 sides
{
num1 = die1.roll();//rolls first die
num2 = die2.roll();//rolls second die
int points = num1 + num2;// adds dies up to get total for this turn
System.out.println("You roll " + num1 + " and " + num2);
if (num1 == 1 || num2 == 1)//If either of the dies roll 1, no points
points += 0;
else if (num1 == 1 && num2 == 1)//if both are 1, lose ALL points
playerTotal = 0;
else
System.out.println("you earned " + points + " this round");
playerTotal += points; total number of points per round added
System.out.println("You have a total of " + playerTotal);
System.out.println("Type y to play");
play = scan.nextLine();
}
}
}
}
My Output:
Type y to play
y
You roll 4 and 2
you earned 6 this round
You have a total of 6
Type y to play
y
You roll 6 and 5
you earned 11 this round
You have a total of 17
Type y to play
y
You roll 1 and 1
You have a total of 19 //total should be 0 because of the two 1's
Type y to play
y
You roll 6 and 3
you earned 9 this round
You have a total of 28
Type y to play
y
You roll 1 and 1
You have a total of 30 //total should be 0 because of the two 1's
Type y to play
y
You roll 6 and 4
you earned 10 this round
You have a total of 40
Type y to play
y
You roll 5 and 2
you earned 7 this round
You have a total of 47
Type y to play
y
You roll 5 and 1
You have a total of 53 //shouldn't add any additional points because of the "1"
Type y to play
If num1 is 1, then the first if condition takes it. It will not check the 'else if' condition. Similarly, if num2 is 1, the if condition takes it. So put your && condition first.
if (num1 == 1 && num2 == 1)//if both are 1, lose ALL points
playerTotal = 0;
else if (num1 == 1 || num2 == 1)//If either of the dies roll 1, no points
points += 0;
else
System.out.println("you earned " + points + " this round");
Your if logic is flawed and a bit redundant. Try this:
if (num1 == 1 && num2 == 1) {
playerTotal = 0;
}
else if (num1 != 1 && num2 != 1) {
playerTotal += points;
System.out.println("you earned " + points + " this round");
}
System.out.println("You have a total of " + playerTotal);
I am trying to write code for a game that has a player and a computer roll dice until one, or both, reach 250( its possible for them to tie). The player and the computer can choose from 1 of 3 die choices. One - 24 sided tie, two - 10 sided die, or three - 6 sided die. There is a bonus for the 10 and 6 sided die if the die are all the same. There are 2 "lakes" where if the player lands in them the player has to go back to the lower number right before the beginning of the lake, there is also a muddy swamp where every move the player makes while in the swamp is cut in half. For every 10 spots (10, 20, 30, 40 ETC.) the player randomly draws a card. There are 11 different cards the player can randomly get:
1-4: player moves ahead a random amount from 1-6
5: player moves ahead a random amount from 4-11 (random 8 + 4)
6: player moves to where the other player is (see below)
7: player moves back to the beginning (moves to location 0)
8-9: player moves back a random amount from 1-6
10-11: player moves back a random amount from 4-11
I have a few problems. My first problem is that the die rolls do not change after every turn, they will remain the same. So if I choose 3 die I might get 3 random numbers, if I choose those die again I will get those same 3 numbers.
I also cannot seem to get the players die count to correctly update. If the player rolls 18 total points and the next turn he rolls 14 the count will go from 18 to 14.
My third problem is it seems like no matter what I do the print statement for the lakes,muddy patch and the winner announcement always print. I have tried a few different things and nothing seems to work.
I am new at code writing ( this is my 4th program written) and do not have extensive knowledge to know what is wrong. The code does not have to be expertly done, I just would like it to work properly. Any and all help is greatly appreciated.
/*This program will create a "Board" game. Each player can choose
from several different types of die. The computer and user will take
turns "rolling" a dice. There are several obstacles that can send one
of the players back. The goal is to get above 250*/
import java.util.*;
public class Project4 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
//assigning variables
int p1, p2;
p1=p2=0;
int spacesmoved = 0;
//Setting up the randomization of the 24 sided die
int minimum1 = 1;
int maximum1 = 24;
Random rn1 = new Random();
int range1 = maximum1 - minimum1 + 1;
int die1 = rn1.nextInt(range1) + minimum1;
//Setting up the randomization of the 10 sided die
int minimum2 = 1;
int maximum2 = 10;
Random rn2 = new Random();
int range2 = maximum2 - minimum2+ 1;
int die2 = rn2.nextInt(range2) + minimum2;
int die22 = rn2.nextInt(range2) + minimum2;
int die222 = rn2.nextInt(range2) + minimum2;
//Setting up the randomization of the 6 sided die
int minimum3 = 1;
int maximum3 = 10;
Random rn3 = new Random();
int range3 = maximum3 - minimum3+ 1;
int die3 = rn3.nextInt(range3) + minimum3;
int die33 = rn3.nextInt(range3) + minimum3;
int die333 = rn3.nextInt(range3) + minimum3;
//Setting a loop for the players to take turns until one, or both, reach > 250
while (p1 <= 250 && p2 <= 250) {
{System.out.println(" Current positions. Player: " + p1 + " Computer: " + p2);
System.out.println("Which die would you like to roll? die1(1) = one 24-sided die, die2(2) = two 10-sided dice, die3(3) = three 6-sided dice: ");
String diechoice = in.nextLine().toLowerCase();
//Getting the die roll if the player chooses the 24 sided die
if (diechoice.equals ("1")) {
spacesmoved = (die1);
System.out.println("Player rolled a " + die1);
System.out.println("Player moves forward " + die1 +" spaces");
p1+=spacesmoved;
}
//Getting the die roll if the player chooses the two 10 sided die
if (diechoice.equals ("2")) { spacesmoved = (die2 + die22);
System.out.println("First die is " + die2);//TESTTTT
System.out.println("Second die is a " + die22);//TEST
System.out.println(die2 + die22);//TESTTTTtttt
if (die2 == die22); {
spacesmoved = (die2 + die22 + die222);
System.out.println("Player rolled doubles, player gets to roll a 3rd 10 sided die");
System.out.println("Players 3rd dice roll is " + die222);
System.out.println("Player moves forward a total of " + spacesmoved + " spots");
p1 += spacesmoved;
}
// player1spot = (currentspot + spacesmoved);
}
//Getting the die roll if the player chooses three 6 sided die
if (diechoice.equals("3")) { spacesmoved = (die3 + die33 + die333);
System.out.println("die 1 is " + die3);
System.out.println("die 2 is " + die33);
System.out.println("die 3 is " + die333);
System.out.println("Player 1 moves forward a total of " + spacesmoved + " spots");
{ if (die3 == die33)
if (die33 == die333)
spacesmoved = ( spacesmoved * 2);
p1 += spacesmoved;
}}
/*Setting up the lakes and muddy patch. If the player lands in a lake he goes back
to the lower edge of the lake. If in the mud his moves are cut in half ONLY while in the mud */
{if (spacesmoved >= (83) || spacesmoved <= (89)); spacesmoved = (82);
System.out.println("Player landed in a lake, player goes back to space " + spacesmoved);
if (spacesmoved >= (152) || spacesmoved <= (155)); spacesmoved = (151);
System.out.println("Player landed in a lake, player goes back to space " + spacesmoved);
if (spacesmoved >= (201) || spacesmoved <= (233)); spacesmoved = (spacesmoved / 2);
System.out.println("Player landed in mud, players turns are cut in half until player gets out");
}
//Setting up the random cards if the player lands on a 10
if (p1 % 10==0);
{ int minimum4 = 0;
int maximum4 = 11;
Random rn4 = new Random();
int range4 = maximum4 - minimum4 + 1;
int card = rn4.nextInt(range4) + minimum4;
//if player gets a card that moves them ahead a random number between 1-6
if (card >=4);
int minimum = 0;
int maximum = 6;
Random rn = new Random();
int range = maximum - minimum + 1;
int cardmove = rn.nextInt(range) + minimum;
p1 = cardmove;
//if player gets a card that moves them ahead a random number between 4-11
if (card == 5);
int minimum5 = 4;
int maximum5 = 11;
Random rn5 = new Random();
int range5 = maximum5 - minimum5 + 1;
int cardmove5 = rn5.nextInt(range5) + minimum5;
p1 = cardmove5;
//if player gets a card that moves them to the spot of the other player
if (card == 6);
p2 = p1;
//if player gets a card that moves them back to 0 (moves location to 0)
if (card ==7);
p1 = 0;
//if player gets a card that moves them back between 1-6 spaces
if (card == (8) || card == 9);
int minimum6 = 1;
int maximum6 = 6;
Random rn6 = new Random();
int range6 = maximum6 - minimum6 + 1;
int cardmove6 = rn6.nextInt(range6) + minimum6;
//if player gets a card that moves them back between 4-11 spaces
if (card == (10) || card == 11);
int minimum7 = 4;
int maximum7 = 11;
Random rn7 = new Random();
int range7 = maximum7 - minimum7 + 1;
int cardmove7 = rn7.nextInt(range7) + minimum7;
}
//Setting up the computers turn
System.out.println("Computers turn");
{
int minimum = 0;
int maximum = 2;
Random rn = new Random();
int range = maximum - minimum + 1;
int computersturn = rn.nextInt(range) + minimum;
//If computer randomly chooses a 24 sided die
spacesmoved = (die1);
System.out.println("Computer rolled a " + die1);
System.out.println("Computer moved " + die1 +" spaces");
p2+=spacesmoved;
}
//If the computer randomly chooses the two 10 sided die
if (diechoice.equals ("die2")) { spacesmoved = (die2 + die22);
System.out.println("First die is " + die2);//TESTTTT
System.out.println("Second die is a " + die22);//TEST
System.out.println(die2 + die22);//TESTTTTtttt
if (die2 == die22); {
spacesmoved = (die2 + die22 + die222);
System.out.println("Computer rolled doubles, player gets to roll a 3rd 10 sided die");
System.out.println("Computer 3rd dice roll is " + die222);
System.out.println("Computer moves a total of " + spacesmoved + " spots");
p2 += spacesmoved;
}
}
//If the computer randomly chooses three 6 sided die
if (diechoice.equals("die3")) { spacesmoved = (die3 + die33 + die333);
System.out.println("die 1 is " + die3);
System.out.println("die 2 is " + die33);
System.out.println("die 3 is " + die333);
System.out.println("Computer 1 moves a total of " + spacesmoved + " spots");
{ if (die3 == die33)
if (die33 == die333)
spacesmoved = ( spacesmoved * 2);
p2 += spacesmoved;
}
//Setting the lakes and mud for the computer
if (spacesmoved >= (83) || spacesmoved <= (89)); spacesmoved = (82);
System.out.println("Computer landed in a lake, player goes back to space " + spacesmoved);
if (spacesmoved >= (152) || spacesmoved <= (155)); spacesmoved = (151);
System.out.println("Computer landed in a lake, player goes back to space " + spacesmoved);
if (spacesmoved >= (201) || spacesmoved <= (233)); spacesmoved = (spacesmoved / 2);
System.out.println("Computer landed in mud, players turns are cut in half until player gets out");
//Setting up the cards for the computer
if (p1 % 10==0);
{ int minimum4 = 0;
int maximum4 = 11;
Random rn4 = new Random();
int range4 = maximum4 - minimum4 + 1;
int card = rn4.nextInt(range4) + minimum4;
//if computer gets a card that moves them ahead a random number between 1-6
if (card >=4);
int minimum = 0;
int maximum = 6;
Random rn = new Random();
int range = maximum - minimum + 1;
int cardmove = rn.nextInt(range) + minimum;
//if computer gets a card that moves them ahead a random number between 4-11
if (card == 5);
int minimum5 = 4;
int maximum5 = 11;
Random rn5 = new Random();
int range5 = maximum5 - minimum5 + 1;
int cardmove5 = rn5.nextInt(range5) + minimum5;
//if computer gets a card that moves them to the spot of the other player
if (card == 6);
p1 = p2;
//if computer gets a card that moves them back to 0 (moves location to 0)
if (card ==7);
p1 = 0;
//if computer gets a card that moves them back between 1-6 spaces
if (card == (8) || card == 9);
int minimum6 = 1;
int maximum6 = 6;
Random rn6 = new Random();
int range6 = maximum6 - minimum6 + 1;
int cardmove6 = rn6.nextInt(range6) + minimum6;
//if computer gets a card that moves them back between 4-11 spaces
if (card == (10) || card == 11);
int minimum7 = 4;
int maximum7 = 11;
Random rn7 = new Random();
int range7 = maximum7 - minimum7 + 1;
int cardmove7 = rn7.nextInt(range7) + minimum7;
}
}
//Writing a final statment showing the winner, or if both tied.
{ if (p1 > p2);
System.out.println("Player 1 wins! Good job!");
if (p2 >p1);
System.out.println("Computer wins! Better luck next time!");
if (p2 == p1);
System.out.println("The game ends in a tie!");
}
}
}
}
}
Here are the things I noticed in relation to the three problems you mentioned:
Problem number 1:
You are setting the values of the dice at the very beginning of code execution. From that point on, you aren't changing them at all. That is the cause of the problem of always rolling the same numbers every turn. You might be thinking that every time you use die1 or any of the other die variables, that it is re-executing the code at the top of your file, but it doesn't.
The code at the top of your file is executed only once and then the value stored in that variable is used for the rest of the program execution. Until you change it. So you would want something more like this:
//Getting the die roll if the player chooses the 24 sided die
if (diechoice.equals ("1")) {
die1 = rn1.nextInt(range1) + minimum1;
System.out.println("Player rolled a " + die1);
System.out.println("Player moves forward " + die1 +" spaces");
p1+=die1;
}
You would also need to change that in the other cases where the die is rolled.
Another benefit to doing it this way is that you really only need one random number generator. You don't actually need one for each die. You can use the same one for all die rolls.
Problem number 2:
I'm not sure exactly what is going wrong with die rolls, if there really is something going wrong there, but I did notice a few places where you'll want to change what is done to p1 and p2:
When the player gets a card that moves them ahead, you'll want to use += instead of =. i.e. p1 += cardmove5 instead of p1 = cardmove5
When the player gets a card that moves them back, it looks like you forgot to add the p1 -= cardmove statements.
Also, make sure you have p1 and p2 in the right places. For example, I'm thinking that on the computer's turn, if they get the card to move them to the other player's spot, you meant to do p2 = p1, but instead you have p1 = p2. Same with the computer going back to 0. You have p1 = 0, but it seems like you would want p2 = 0. So just be careful about that. (Also be careful about copy paste. I'm guessing that's why that happened)
Problem number 3:
This problem looks like it's caused by the fact that you are using the || operator where you should be using &&. When you use ||, you are effectively saying "or". So this first statement
if (spacesmoved >= (83) || spacesmoved <= (89))
reads as "if spacesmoved is greater than or equal to 83 OR less than or equal to 89"... Think about that for a second. Is there any number that is NOT greater than 83 OR less than 89? The answer is no. EVERY number will satisfy this condition. You would want to use &&, which means "and" like this:
if (spacesmoved >= (83) && spacesmoved <= (89))
"if spacesmoved is greater than or equal to 83 AND less than or equal to 89", which would only work for numbers between 83 to 89 inclusive.
You will also want to remove the semicolons after your "if" statements in that block and the other similar blocks. If you don't, the code inside those conditions won't get executed. That's actually a really tough bug to find when it happens.
Another thing to know is that when you want multiple things to be executed in an "if" condition, you must enclose it in curly braces {}, otherwise, only the first line will be included in the condition, and any following lines will be executed unconditionally. That is another fact that is causing this third problem.
One last thing is that you should try using "else if" and "else" statements. It will help your code flow make more sense. I'm not going to do all the work for you, but this code block should probably look more like this:
if (p1 >= (83) && p1 <= (89))
{
p1 = (82);
System.out.println("Player landed in a lake, player goes back to space " + p1);
}
else if (p1 >= (152) && p1 <= (155))
{
p1 = (151);
System.out.println("Player landed in a lake, player goes back to space " + p1);
}
else if (p1 >= (201) && p1 <= (233))
{
spacesmoved = (spacesmoved / 2);
p1 -= spacesmoved;
System.out.println("Player landed in mud, players turns are cut in half until player gets out");
}
Bonus Tip
You're learning well, and it seems you are thinking of code flow pretty well. Just keep working and learning and you'll get it.
Look into your usage of parentheses. Using them doesn't hurt anything, but you are using them WAY more than you need.
Good luck! And keep learning!
In an infinite loop, how would I print a random number that will increase every set number of times( example 10 times )? Here is my code at the moment.
`import hsa.*;
public class MidTermProject{
public static void main(String[] args){
Console con = new Console();
int intCount;
int intRand;
int intAnswer;
int intRand2;
int intTotal;
int intSubtractTotal;
int intAnswer2;
int intRand3;
int intRand4;
int intQuestionsAsked;
int intTotalQuestions;
int intTRand;
int intTRand1;
int intTRand2;
int intTRand3;
int intTRand4;
double dblTotalScore;
double dblScore;
dblScore = 0;
intQuestionsAsked = 0;
//5 - Math Training Game
con.println("This is the Math Training Game. The Questions will increase difficulty.");
con.println("There will be 30 Questions in Total");
con.println("");
con.println("One Digits:");
con.println("");
//Loop ---------------------------------------------------------------------
for(;;){
for(intCount=0; intCount<5;intCount++){
//----------------------------------------------------------------------------
//1 DIGITS
intRand=(int)(Math.random()*9+1);
intRand2=(int)(Math.random()*9+1);
intTotal = intRand + intRand2;
intRand3 =(int)(Math.random()*9+1);
intRand4 =(int)(Math.random()*9+1);
intSubtractTotal = intRand3 - intRand4;
con.println("What is " + intRand + " + " + intRand2);
intAnswer = con.readInt();
if(intAnswer == intTotal){
con.println("Correct");
con.println("");
//Add score
dblScore = dblScore + 1;
intQuestionsAsked = intQuestionsAsked + 1;
}else{
con.println("Wrong");
con.println("");
intQuestionsAsked = intQuestionsAsked + 1;
}
// SUBTRACTION ---------------------------------------------------------
con.println("What is " + intRand3 + " - " + intRand4);
intAnswer2 = con.readInt();
if(intAnswer2 == intSubtractTotal){
con.println("Correct");
con.println("");
//Add Score -------------------------------------------------------------
intQuestionsAsked = intQuestionsAsked + 1;
dblScore = dblScore + 1;
}else{
con.println("Wrong");
con.println("");
intQuestionsAsked = intQuestionsAsked + 1;
}
intTotalQuestions = intQuestionsAsked;
//----------------------------------------------------------
while(intTotalQuestions == 10){
intQuestionsAsked = 0;
intTRand = intRand * 10;
intTRand2 = intRand2 * 10;
intTRand3 = intRand3 * 10;
intTRand4 = intRand4 * 10;
con.println("What is " + intRand * intTRand + "+" + intTRand2 * intRand2);
intAnswer = con.readInt();
con.println("What is " + intRand2 * intTRand2 + "-" + intRand3 * intTRand3);
intAnswer2 = con.readInt();
}
//--------------------------------------------
}
}
}
}'
Could anyone tell me what I did wrong and how I could fix this code to make it work?
What I understand is that you want to generate random numbers that get increasingly higher. Store the previous random generated value. Generate your new random number from whatever range you want, and add the previous number to ensure it increases. Also, as it appears your code is way clunkier than it has to be, I would suggest using arrays to cut down on all of the excessive instance variables you have.
Having a program run forever is bad design.
That being said, if you want your random numbers to be of greater values every time you can set a range
(min + (int)(Math.random() * ((max - min) + 1)));
and increase min and max every iteration.
I have a problem with my dealerhand method in my blackjack game.
I have a method to produce a random card from the class deck.
The cards have assigned values to them and so forth.
however the problem lies in the code where i want the dealer to draw a new card, and add the value to the existing total hand value. the code is a following.
//Basics for the values of dealers cards
int dealerHandValue = 0;
int tempDealerHandValue = 0;
int totalDealerHandValue= 0;
//Dealers first card
randomGenNum = (int)((range * Math.random()) + 1)*2;
dealerHandValue = arrayCardRank[randomGenNum];
CardSuit = arrayCardSuit[randomGenNum];
System.out.println("Dealer First Card Shows : " + (CardSuit));
tempDealerHandValue = dealerHandValue;
//Code executed when player stops drawing and stands.
while (totalDealerHandValue < 18 && totalDealerHandValue <21)
{
randomGenNum = (int)((range * Math.random()) + 1)*2;
dealerHandValue = arrayCardRank[randomGenNum];
CardSuit = arrayCardSuit[randomGenNum];
System.out.println("Dealer next Card Shows : " + (CardSuit));
tempDealerHandValue = dealerHandValue;
totalDealerHandValue = (tempDealerHandValue) + (dealerHandValue);
System.out.println("Dealer total hand value is " + (totalDealerHandValue));
}
{
System.out.println("Dealer stopped drawing");
if (totalDealerHandValue >= totalUserHandValue)
{
System.out.println("Dealer wins");
return;
}
else
System.out.println("Congratulations! You Win!");
return;
}
This method will just add the new cards value to itself, on and on until the while statement ends.
i have gone blind on the problem, and i know it is easily fixed.
can anyone help me towards what i am missing?
you're never incrementing totalDealerHandValue, just overwriting the value over and over again.
Replace these two lines:
tempDealerHandValue = dealerHandValue;
totalDealerHandValue = (tempDealerHandValue) + (dealerHandValue);
with
totalDealerHandValue += dealerHandValue;
I'm having an issue with a change machine I'm building for my Java class. I'm very new to programming so this may be a dumb logic error. My change machine asks the user to input the price of the item then amount paid then it is supposed to calculate how much change the user will receive in quarters, dimes, etc... However, it's only counting 1 quarter each time. Here's the code:
import java.util.Scanner;
import java.text.*;
public class Main {
public static void main(String[] args)
{
float penny = .1F, nickel = .5F, dime = .10F, quarter = .25F;
int pennyCtr = 0, nickelCtr = 0, dimeCtr = 0, quarterCtr = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter Purchase Price: ");
float price = scan.nextFloat();
DecimalFormat paidFormat = new DecimalFormat("0.00");
System.out.println("Enter Amount Paid: ");
float paid = scan.nextFloat();
float change = (float) (paid - price);
System.out.println("Your change from $" + paidFormat.format(paid)
+ " is: " + change);
if (change >= .25)
{
change -= quarter;
quarterCtr++;
}
else if (change < .25)
{
change -= dime;
dimeCtr++;
}
else if (change < .10)
{
change -= nickel;
nickelCtr++;
}
else if (change < .5)
{
change -= penny;
pennyCtr++;
}
System.out.println("Your change from $" + paidFormat.format(paid) +
" is: " + quarterCtr + " Quarters, " + dimeCtr + " Dimes, "
+ nickelCtr + " Nickles, " + pennyCtr + " Pennies. ");
System.out.println("Program written by Ashley ");
}
}
Some general hints:
Look at how you are declaring your variables, especially penny and nickel.
Look at how you are calculating change. Is that right?
You need a while loop. How long should it loop for? When should the loop end?
When you print out "Your change from...", consider how you could output the change neatly.
Google some questions about float subtraction - it's not as easy as it first seems! Consider using double instead for your declarations and input.
A quick look at the code tells me that you are missing a loop between this block of code:
if (change >= .25)
{
change -= quarter;
quarterCtr++;
}
else if (change < .25)
{
change -= dime;
dimeCtr++;
}
else if (change < .10)
{
change -= nickel;
nickelCtr++;
}
else if (change < .5)
{
change -= penny;
pennyCtr++;
}
Essentially, your program will terminate before it can reduce the change to 0. This is why you are only counting 1 coin every time. Consider maybe a while loop (while change > 0) perhaps.