The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 337 Weeks Ago, 1 Day Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
Hi
I am stuck on this Java coding question. I think I am on the right path of solving it but just not sure how to cod by not using a switch with cases.
Please if you could help in shortening my coding.
Some programming notes:
Feedback:
If you focus on each round (where a round is a user input plus a computer input).
User input: 1, 2 or 3
Computer input ==> focus on a total sum for each round that will guarantee that each round
ends on an even number and that there is no way for the computer
to exceed 20/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package count21;
/**
*
* @author SOPHOL
*/
import java.util.Scanner;
public class Count21 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final int TWENTY_ONE = 21;
int playerOneEntry;
int computerEntry = 0;
int total = 0;
System.out.println("Instructions, two players take turns by entering 1, 2, or 3 \n" + "which is then added to a running total. Whoever makes the score go over twenty one " + "loses\n");
while (total <= 21) {
System.out.println("Player, please enter 1, 2, or 3 >>> ");
playerOneEntry = input.nextInt();
while (playerOneEntry != 1 && playerOneEntry != 2 && playerOneEntry != 3) {
System.out.println("Player, please enter 1, 2, or 3 >>> ");
playerOneEntry = input.nextInt();
}
total += playerOneEntry;
System.out.println("Player one total is " + total);
if (total == 21) {
System.out.println("Player Wins!");
total = 25;
}
if (total < TWENTY_ONE) {
switch (total) {
case 1:
computerEntry = 1;
break;
case 2:
computerEntry = 1;
break;
case 3:
computerEntry = 3;
break;
case 4:
computerEntry = 3;
break;
case 5:
computerEntry = 3;
break;
case 6:
computerEntry = 2;
break;
case 7:
computerEntry = 1;
break;
case 8:
computerEntry = 1;
break;
case 9:
computerEntry = 3;
break;
case 10:
computerEntry = 2;
break;
case 11:
computerEntry = 3;
break;
case 12:
computerEntry = 2;
break;
case 13:
computerEntry = 1;
break;
case 14:
computerEntry = 3;
break;
case 15:
computerEntry = 2;
break;
case 16:
computerEntry = 1;
break;
case 17:
computerEntry = 1;
break;
case 18:
computerEntry = 3;
break;
case 19:
computerEntry = 2;
break;
case 20:
computerEntry = 1;
break;
default:
computerEntry = 1;
}
}
total += computerEntry;
System.out.println("Computer entered " + computerEntry);
System.out.println("Computer total is " + total);
if (total == 21) {
System.out.println();
total = 25;
System.out.println("Computer Wins!");
} else {
System.out.println("The total is " + total);
}
}
}
}
Thank you
----------- ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly