Question: I need each line of code commented. Here is my code: import java.util.Random; import java.util.Scanner; public class cardGame { public static void main(String[] args) {
I need each line of code commented.
Here is my code:
import java.util.Random;
import java.util.Scanner;
public class cardGame {
public static void main(String[] args) {
// TODO Auto-generated method stub
cardG thedeck = new cardG();
Scanner cardReader = new Scanner (System.in);
int inputVal = 0;
while (inputVal!=2) {
System.out.println("1. To play War");
System.out.println("2. Exit Game");
System.out.println("Enter number choice:");
inputVal = cardReader.nextInt();
if (inputVal==1) {
System.out.println("Enter value of the card:");
int cardNum = cardReader.nextInt();
if (cardNum >= 0 && cardNum<=51) {
thedeck.theReturn(cardNum);
}
else {
System.out.println("Incorrect Card!");
}
}
}
System.out.println("System:" + thedeck.systemWon);
System.out.println("You" + thedeck.playerWinner);
}
}
class cardG{
int playerWinner = 0;
int systemWon = 0;
String cards []= {
"Diamonds",
"Spades",
"Hearts",
"Clubs",
};
Random randomNum = new Random();
String inputCard(int card) {
int theSuit = card/13;
int theRanks = card%13;
String cardOutput = "";
switch (theRanks) {
case 0:
cardOutput = "Ace of" + cards [theSuit];
break;
case 10:
cardOutput = "Jack of" + cards [theSuit];
break;
case 12:
cardOutput = "Queen of" + cards [theSuit];
break;
case 11:
cardOutput = "King of" + cards [theSuit];
break;
default:
cardOutput = theRanks + 1 + "of" + cards [theSuit];
break;
}
return cardOutput;
}
void theReturn (int theCard) {
int systemRanks = randomNum.nextInt(52);
System.out.println("Your chosen card:" + "" + theCard + "" + "" + "Your card-" + inputCard(theCard));
System.out.println(" The System chose:" + "" + "" + systemRanks + "" + "Systems Card-" + "" + inputCard(systemRanks));
if (systemRanks > theCard) {
systemWon++;
System.out.println("System has won.");
}
else if (systemRanks == theCard) {
System.out.println(" Round was a tie.");
}
else {
playerWinner++;
System.out.println("You win.");
return;
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
