Question

In: Computer Science

I'm trying to work with this code that I have. i want to kick the user...

I'm trying to work with this code that I have. i want to kick the user back if they don't enter a number in the specified range but I haven't been able to get a while loop working right.

  1. Write a program name Blackjack_Jr that allows a human user to play a single hand of "blackjack" against a dealer.
  2. Pick two values from 1-10 for the player. These are the player's "cards". These two values must be user inputs
  3. Generate two more values from 1-10 for the dealer. These two values should be randomly generated by your code
  4. Whoever has the highest total is the winner.

public class Blackjack_Jr
{

   public static void main(String[] args)
   {
       int card1;
       int card2;
       int player1;
       int dealer;
       Random rand = new Random();
       int card3 = rand.nextInt(11);
       int card4 = rand.nextInt(11);
       Scanner in = new Scanner(System.in);
      
       System.out.println("Blackjack Jr!");
       System.out.println();
       System.out.print("Pick Your First Card from 1-10: ");
       card1 = in.nextInt();
       System.out.print("Pick Your Second Card from 1-10: ");
       card2 = in.nextInt();
       System.out.println();
       player1 = card1 + card2;
       dealer = card3 + card4;
          
       System.out.print("You have entered " +card1);
       System.out.println(" and " +card2);
       System.out.println("Your total is: " +player1);
       System.out.println();
              
      
              
       System.out.print("The dealer has drawn " +card3);
       System.out.println(" and " +card4);
       System.out.println("The dealer's total is: " +dealer);
              
           if(player1 > dealer)
           {
               System.out.println("Congratulations! You Won!");
           }else
           {
               System.out.println("Sorry you have lost. :-(");
           }
      
      

   }

}

Solutions

Expert Solution

package NewOne;

import java.util.Random;
import java.util.Scanner;

public class Blackjack_Jr {

   public static void main(String[] args) {
       int card1;
       int card2;
       int player1;
       int dealer;
       Random rand = new Random();
       int card3 = rand.nextInt(11);
       int card4 = rand.nextInt(11);
       Scanner in = new Scanner(System.in);

       System.out.println("Blackjack Jr!");
       System.out.println();
       System.out.print("Pick Your First Card from 1-10: ");
       card1 = in.nextInt();

       while (!(card1 >= 1 && card1 <= 10)) {

           System.out.print("Invalid number: Pick Your First Card from 1-10: ");
           card1 = in.nextInt();
       }
       System.out.print("Pick Your Second Card from 1-10: ");
       card2 = in.nextInt();

       while (!(card2 >= 1 && card2 <= 10)) {

           System.out.print("Invalid number: Pick Your Second Card from 1-10: ");
           card2 = in.nextInt();
       }
       System.out.println();
       player1 = card1 + card2;
       dealer = card3 + card4;

       System.out.print("You have entered " + card1);
       System.out.println(" and " + card2);
       System.out.println("Your total is: " + player1);
       System.out.println();

       System.out.print("The dealer has drawn " + card3);
       System.out.println(" and " + card4);
       System.out.println("The dealer's total is: " + dealer);

       if (player1 > dealer) {
           System.out.println("Congratulations! You Won!");
       } else {
           System.out.println("Sorry you have lost. :-(");
       }

   }

}

============


Related Solutions

The source code I have is what i'm trying to fix for the assignment at the...
The source code I have is what i'm trying to fix for the assignment at the bottom. Source Code: #include <iostream> #include <cstdlib> #include <ctime> #include <iomanip> using namespace std; const int NUM_ROWS = 10; const int NUM_COLS = 10; // Setting values in a 10 by 10 array of random integers (1 - 100) // Pre: twoDArray has been declared with row and column size of NUM_COLS // Must have constant integer NUM_COLS declared // rowSize must be less...
I'm trying to decide if I want to invest in a refinery to upgrade my bitumen...
I'm trying to decide if I want to invest in a refinery to upgrade my bitumen to oil. Currently it costs me $40, to get my bitumen to market, where I am able to sell it for $50 a barrel. If I decided to upgrade, it will costs me an extra $6 a barrel, these will produce 30 liters of oil, which I can sell for $2 each.   Should I expand my business? Select one: a. Yes - 4 b....
I'm trying to get my code to loop back to let the user input multiple patients....
I'm trying to get my code to loop back to let the user input multiple patients. import java.util.Scanner; public class XYZ {    public static void main(String[] args) {           String response;        do{            //import scanner for input            Scanner input = new Scanner(System.in);               //prompt user input for last name            System.out.print("Enter last name ");            //output for last name            String...
Python: I want to make the following code to prompt the user if want to run...
Python: I want to make the following code to prompt the user if want to run the software again. I am new to python, so i do not know if it works like c++. If can explain that would be much appreciated base = float(input("Enter base of the triagle: ")) Triangle_Right = float(input("Enter right side of the triagle: ")) Triangle_Left = float(input("Enter left side of the triagle: ")) height = float(input("Enter the height of the triangle: ")) perimiter = base...
Hello. I'm trying to write down java code making stars whose numbers are determined by user...
Hello. I'm trying to write down java code making stars whose numbers are determined by user input on the console (each star's data are stored in ArrayList). the class extends JFrame and its color, size(Zoom in and Zoom out), and location are changing every time. Moreover, its angular rotation is changed at a certain determined degree. Does anyone know how to write down the java code of this question?
I'm having a very hard time getting this c++ code to work. I have attached my...
I'm having a very hard time getting this c++ code to work. I have attached my code and the instructions. CODE: #include using namespace std; void printWelcome(string movieName, string rating, int startHour, int startMinute, char ampm); //menu function //constants const double TICKET_PRICE = 9.95; const double TAX_RATE = 0.095; const bool AVAILABLE = true; const bool UNAVAILABLE = false; int subTotal,taxAdded, totalCost; // bool checkAvailability( int tickets, int& seatingLeft){ //checks ticket availability while(tickets > 0 || tickets <= 200) //valid...
I'm trying to study for my General Chemistry II Exam, but I want to make sure...
I'm trying to study for my General Chemistry II Exam, but I want to make sure that I don't miss anything important! If you could provide your input on the following concepts, that would be greatly appreciated! Thank you so much!(: Chapter 17 – Chemical Equilibrium I. Chemical Equilibria             A. Equilibrium constant             B. Homogeneous equilibria             C. Heterogeneous equilibria             D. Molar concentrations             E. Contribution of a pure liquid or a pure solid to the equilibrium constant...
I'm trying to study for my General Chemistry II Exam, but I want to make sure...
I'm trying to study for my General Chemistry II Exam, but I want to make sure that I don't miss anything important! If you could provide your input on the following concepts, that would be greatly appreciated! Thank you so much!(: Chapter 16 - Chemical Kinetics I. Reaction Rates             A. Definition of reaction rate             B. Mathematical expression for Reaction rate             C. Instantaneous rate             D. Initial rate             E. Rate constant             F. Rate law             G....
Android Studio (Java) I'm trying to create a simple calculator. I want to put out a...
Android Studio (Java) I'm trying to create a simple calculator. I want to put out a message if they try to divide by 0. I have this so far. What code should I put? divide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (number1.getText().length() != 0 && number2.getText().length() != 0) { double n1= Double.parseDouble(number1.getText().toString()); double n2= Double.parseDouble(number2.getText().toString()); double res= n1 / n2; result.setText(String.valueOf(res)); } else { Toast.makeText(view.getContext(), "Please enter the numbers properly", Toast.LENGTH_SHORT).show(); } } });
I'm having difficulty trying to make my code work. Create a subclass of Phone called SmartPhone....
I'm having difficulty trying to make my code work. Create a subclass of Phone called SmartPhone. Make this subclass have a no-arg constructor and a constructor that takes a name, email, and phone. Override the toString method to return the required output. Given Files: public class Demo1 { public static void test(Phone p) { System.out.println("Getter test:"); System.out.println(p.getName()); System.out.println(p.getNumber()); } public static void main(String[] args) { Phone test1 = new SmartPhone(); Phone test2 = new SmartPhone("Alice", "8059226966", "[email protected]"); System.out.println(test1); System.out.println(test2); System.out.println(test1);...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT