• 0

what am I doing wrong?


Question

As an output, "Player card" should be outputted with randomly selected suit and value, but it's not.

Is there something missing or I have my if...else statements messed up? I know I can use do...while, but it's not required.


public static void main(String[] args){

//A new object, player card for the card suit.
Card player = new Card();

//Setting the suit for player card to be displayed randomly
player.setSuit((int) (Math.random()*4));

//Setting the value for player card to be displayed randomly
player.setValue(1 + (int)(Math.random()*12));
//A new object, computer card for the card suit
Card comp = new Card();

//Setting the suit for computer card to be displayed randomly
comp.setSuit((int) (Math.random()*4));

//Setting the value for computer card to be displayed randomly
comp.setValue(1 + (int)(Math.random()*12));


if((comp.getValue()== player.getValue()) && (comp.getSuit() == player.getSuit()))
System.out.println("Player Card: " + player.getValueString() + " of " + player.getSuitString());
System.out.println("Computer Card: " + comp.getValueString() + " of " + comp.getSuitString());
if(player.getValue() > comp.getValue())
System.out.println("Player won!");
else if(player.getValue() < comp.getValue())
System.out.println("Computer won!");
else
System.out.println("Tie!");
[/CODE]

Link to comment
https://www.neowin.net/forum/topic/1063892-what-am-i-doing-wrong/
Share on other sites

12 answers to this question

Recommended Posts

  • 0

You're missing curly brackets around the body of your if statements. The indentation suggests something else than what is actually happening.

if (...)
// statement 1
// statement 2[/CODE]

is not equivalent to

[CODE]if (...) {
// statement 1
// statement 2
}[/CODE]

In the former case, statement 2 is actually outside the body of the if, so it always executes.

It's good practice to always use curly brackets in this case, as well as for loops, while loops, etc. I never omit them precisely because I've seen this mistake crop up too often, even in code written by professionals.

Also I'm puzzled by your first condition: that the computer card equals the player card. You're aware that you're producing different numbers for the computer card and player card, right? They will only be equal once in a while (rarely at that), not by necessity.

  • 0

This is what I have so far, this should look a little better:


//Output the results of selected cards and suits for the Player and the Computer
System.out.println("Player Card: " + player.getValueString() + " of " + player.getSuitString());
System.out.println("Computer Card: " + comp.getValueString() + " of " + comp.getSuitString());

//Indicating if the results of the player and computer are equal or not
if((comp.getValue() == player.getValue()) && (comp.getSuit() == player.getSuit())){
System.out.println("Tie!");
}

//Indicating the results of values selected
if(player.getValue() > comp.getValue()){
System.out.println("Player won!");
}
else
//Indicating the results values selected
if(player.getValue() < comp.getValue()){
System.out.println("Computer won!");
}
[/CODE]

  • 0

This part of the code I have:


//Displaying constants of the 4 suits of cards.
public final static int SPADES = 0,
HEARTS = 1,
DIAMONDS = 2,
CLUBS = 3;


//Displaying the name of the suits.
//If the name is invalid, it returns as "invalid".
public String getSuitString() {
switch (suit) {
case SPADES: return "Spades";
case HEARTS: return "Hearts";
case DIAMONDS: return "Diamonds";
case CLUBS: return "Clubs";
default: return "invalid";
}
}
[/CODE]

But it results in error.

I know it has do with the line of code SPADES, where I've assigned a value 0, which is not correct?

Also, it's not recommended to using String variables with switch statements? I should replace it with if statements?

  • 0

This part of the code I have:


//Displaying constants of the 4 suits of cards.
public final static int SPADES = 0,
HEARTS = 1,
DIAMONDS = 2,
CLUBS = 3;


//Displaying the name of the suits.
//If the name is invalid, it returns as "invalid".
public String getSuitString() {
switch (suit) {
case SPADES: return "Spades";
case HEARTS: return "Hearts";
case DIAMONDS: return "Diamonds";
case CLUBS: return "Clubs";
default: return "invalid";
}
}
[/CODE]

But it results in error.

I know it has do with the line of code SPADES, where I've assigned a value 0, which is not correct?

Also, it's not recommended to using String variables with switch statements? I should replace it with if statements?

Where are you defining suit? The error may be showing on the second line of the switch, but it could be a problem with the first line.

  • 0

Here's my entire code:


public class Card {
//Displaying constants of the 4 suits of cards.
public final static String SPADES = 0,
HEARTS = 1,
DIAMONDS = 2,
CLUBS = 3;

//Displaying constants of types of cards in the card deck.
public final static int ACE = 1,
JACK = 11,
QUEEN = 12,
KING = 13;

//Identifies the suit of the card.
//Could be any of the 4: Spades, Hearts, Diamonds, or Clubs
private String suit;

//Identifies the value of the card.
//They are numbered from 1 to 13.
private int value;

//Identifies the rank of the card
private String rank;

/**
* Description of setSuit(int theSuit)
* @param theSuit
* The suit value to set for the card
*/
//The set method for the card suit.
public void setSuit(String s) {
suit = s;
}

/**
* Description of setValue(int theValue)
* @param theValue
* The value to set for the card
*/

public String getRank(){
return rank;
}

//The set method for the card values
//The range of the value is from 1 to 13
public void setValue(int v) {

if(value >= 1 && value <= 13)
value = v;
else
value = 1;
if(value == 1)
rank = "Ace";
else
if(value == 11)
rank = "Jack";
else
if(value == 12)
rank = "Queen";
else
if(value == 13)
rank = "King";
else
rank = Integer.toString(value);

}

//The get method for the card suit.
public String getSuit() {
return suit;
}

//The get method for the card values.
public int getValue() {
return value;
}

//Displaying the name of the suits.
//If the name is invalid, it returns as "invalid".




public String getSuitString() {
switch (suit) {
case SPADES: return "Spades";
case HEARTS: return "Hearts";
case DIAMONDS: return "Diamonds";
case CLUBS: return "Clubs";
default: return "invalid";
}
}

//Displaying the value of each card.
//If the value is invalid, it returns as "invalid".
public String getValueString() {
switch (value) {
case 1: return "Ace";
case 2: return "2";
case 3: return "3";
case 4: return "4";
case 5: return "5";
case 6: return "6";
case 7: return "7";
case 8: return "8";
case 9: return "9";
case 10: return "10";
case 11: return "Jack";
case 12: return "Queen";
case 13: return "King";
default: return "invalid";
}
}
}//end class
[/CODE]

  • 0


public class Card {
//Displaying constants of the 4 suits of cards.
public final static String SPADES = 0,
HEARTS = 1,
DIAMONDS = 2,
CLUBS = 3;

//Identifies the suit of the card.
//Could be any of the 4: Spades, Hearts, Diamonds, or Clubs
private String suit;

/**
* Description of setSuit(int theSuit)
* @param theSuit
* The suit value to set for the card
*/
//The set method for the card suit.
public void setSuit(String s) {
suit = s;
}


//The get method for the card suit.
public String getSuit() {
return suit;
}


//Displaying the name of the suits.
//If the name is invalid, it returns as "invalid".
public String getSuitString() {
switch (suit) {
case SPADES: return "Spades";
case HEARTS: return "Hearts";
case DIAMONDS: return "Diamonds";
case CLUBS: return "Clubs";
default: return "invalid";
}
}
}//end class
[/CODE]

your problem is that fact that you are asigning 0 to a string. You can't do that. To fix your problem that you are seeing.

do:

[CODE]
//Displaying constants of the 4 suits of cards.
public final static String SPADES = "0",
HEARTS = "1",
DIAMONDS = "2",
CLUBS = "3";
[/CODE]

If you want to use integers, you will need to change all the "Suit" string to be integers, and do an Int32.Parse (or Convert.) whatever the keyword is. You need to remember: "0" != 0 and what you were trying to do was compare "0" and 0 which can't be compared.

  • 0

oh...that's it? :blush:

Can you guys look at this code, how is it?


public static void main(String[] args){

int playerValue;
int compValue;
int playerSuit;
int compSuit;

Card playerCard = new Card();
Card compCard = new Card();

playerValue = ((int) (Math.random() * 100));
compValue = ((int) (Math.random() * 100));

playerCard.setValue(playerValue);
compCard.setValue(compValue);
playerValue = ((int)(Math.random() * 100));
compValue = ((int)(Math.random() * 100));

if(playerValue == compValue && playerSuit == compSuit){
compSuit = compSuit + 1;
if(compSuit > 4)
compSuit = 1;
}
if(playerSuit == 1)
playerCard.setSuit("Spades");
else
if(playerSuit == 2)
playerCard.setSuit("Hearts");
else
if(playerSuit == 3)
playerCard.setSuit("Diamonds");
else
playerCard.setSuit("Clubs");

if(compSuit == 1)
compCard.setSuit("Spades");
else
if(compSuit == 2)
compCard.setSuit("Hearts");
else
if(compSuit == 3)
compCard.setSuit("Diamonds");
else
compCard.setSuit("Clubs");

System.out.println("Player card is " + playerCard.getRank() + " of " + playerCard.getSuit());
System.out.println("Computer card is " + compCard.getRank() + " of " + compCard.getSuit());

if(playerValue == compValue)
System.out.println("Tie!!");
else
if(playerValue > compValue)
System.out.println("Player wins!");
else
System.out.println("Computer wins!");
[/CODE]

  • 0

^^ You don't set anything for playerSuit nor compSuit anywhere... unless that's not all your code.

This is my other class, I've already declared playerSuit and ranks...


public class Card {
//Displaying constants of the 4 suits of cards.
public final static String SPADES = "0",
HEARTS = "1",
DIAMONDS = "2",
CLUBS = "3";

//Displaying constants of types of cards in the card deck.
public final static int ACE = 1,
JACK = 11,
QUEEN = 12,
KING = 13;

//Identifies the suit of the card.
//Could be any of the 4: Spades, Hearts, Diamonds, or Clubs
private String suit;

//Identifies the value of the card.
//They are numbered from 1 to 13.
private int value;

//Identifies the rank of the card
private String rank;

/**
* Description of setSuit(int theSuit)
* @param theSuit
* The suit value to set for the card
*/
//The set method for the card suit.
public void setSuit(String s) {
suit = s;
}

/**
* Description of setValue(int theValue)
* @param theValue
* The value to set for the card
*/

public String getRank(){
return rank;
}

//The set method for the card values
//The range of the value is from 1 to 13
public void setValue(int v) {

if(value >= 1 && value <= 13)
value = v;
else
value = 1;
if(value == 1)
rank = "Ace";
else
if(value == 11)
rank = "Jack";
else
if(value == 12)
rank = "Queen";
else
if(value == 13)
rank = "King";
else
rank = Integer.toString(value);

}

public String toString(){
String theCard;
String[] cardRank = {"Ace","Two","Three","Four","Five","Six","Seven", "Eight","Nine","Ten","Jack","Queen","King"};
String[] playerSuit = {"Clubs","Diamonds","Hearts","Spades"};

// cardSuit is not needed for War:
theCard = (cardRank[rank]);
return theCard;
}

//The get method for the card suit.
public String getSuit() {
return suit;
}

//The get method for the card values.
public int getValue() {
return value;
}

//Displaying the name of the suits.
//If the name is invalid, it returns as "invalid".
public String getSuitString() {
switch (suit) {
case SPADES: return "Spades";
case HEARTS: return "Hearts";
case DIAMONDS: return "Diamonds";
case CLUBS: return "Clubs";
default: return "invalid";
}
}

//Displaying the value of each card.
//If the value is invalid, it returns as "invalid".
public String getValueString() {
switch (value) {
case 1: return "Ace";
case 2: return "2";
case 3: return "3";
case 4: return "4";
case 5: return "5";
case 6: return "6";
case 7: return "7";
case 8: return "8";
case 9: return "9";
case 10: return "10";
case 11: return "Jack";
case 12: return "Queen";
case 13: return "King";
default: return "invalid";
}
}
}//end class
[/CODE]

This topic is now closed to further replies.
  • Posts

    • Hello, It would appear so, according to https://finance.yahoo.com/news/how-to-hide-your-home-on-google-maps-apple-maps-204146687.html. Regards, Aryeh Goretsky      
    • Hello, The Nvidia Founders Edition 3080 video card is approximately six years old, correct? Have you looked into whether replacement fans are available for it? Perhaps replacing those will improve cooling, especially when combined with cleaning the card's heatsink and replacing the thermal interface materials. Regards, Aryeh Goretsky  
    • Hello, While ~104 GB of space may seem generous (at least compared to other e-readers which have 8-32GB), I feel at this price point the device should have a Micro SDXC card slot for expansion, particularly if it allows audio books to be installed and played. I hope to see more reviews of 6" phone-sized e-readers on Neowin in the future. It will be interesting to see how they compare. Regards, Aryeh Goretsky
    • Sandboxie Plus 1.17.8 / Classic 5.72.8 by Razvan Serea Run programs in a sandbox to prevent malware from making permanent changes to your PC. Sandboxie allows you to run your browser, or any other program, so that all changes that result from the usage are kept in a sandbox environment, which can then be deleted later. Sandboxie is a sandbox-based isolation software for 32- and 64-bit Windows NT-based operating systems. It is being developed by David Xanatos since it became open source, before that it was developed by Sophos (which acquired it from Invincea, which acquired it earlier from the original author Ronen Tzur). It creates a sandbox-like isolated operating environment in which applications can be run or installed without permanently modifying the local or mapped drive. An isolated virtual environment allows controlled testing of untrusted programs and web surfing. Sandboxie is available in two flavors Plus and Classic. Both have the same core components, this means they have the same level of security and compatibility. What's different is the user interface the Plus build has a modern Qt based UI which supports all new features that have been added since the project went open source. The Classic build has the old no longer developed MFC based UI, hence it lacks support for modern features, these features can however still be used when manually configured in the Sandboxie.ini. Sandboxie Plus 1.17.8 / Classic 5.72.8 release notes: Added added DisableCustomTitleOpt=[process,][y|n] to allow [#] sandboxie title markers on custom-titlebar windows (Delphi VCL, Qt, Electron) that were previously skipped to prevent DWM repaint CPU loops #5387 Changed updated bundled ImDisk driver to 3.0.2 #5419 Fixed fix Suppress logs for expected non-user SIDs #5422 SbieSvc.exe: SBIE2218/2219 error when run program as administrator #5417 fixed explorer.exe crashes in Application Compartment when Huorong Security is installed #5423 Download: Sandboxie Plus (64-bit) | 23.5 MB (Open Source) Download: Sandboxie Classic (64-bit) | 3.0 MB Links: Sandboxie Website | GitHub | ARM64 | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Hello, Christian Maas' XVI32 is a nice (and very small) hex editor. Speaking of hex editors, many years ago a colleague and I who both worked at Tribal Voice managed to edit a copy of the company's PowWow instant messaging client to make it behave better now that all of its lookup servers and other server-side tech was gone.  The program didn't support NAT (RFC-3022 was introduced in January 2001, the same time Tribal Voice was shuttered), but it still worked okay if you manually set up port-forwarding on your router.  The server at http://powwow.jazy.net/ hosts a copy (usual warnings about downloading and running untrusted code from random internet servers apply). I occasionally use some tools like Funduc Software's Search and Replace and Application Mover when I need to make mass-edits to text-based files or move programs with a hard-coded installation directories, respectively.  When I need to figure out the exact LCD panel inside of a laptop, EnTech Taiwan's Monitor Asset Manager is my go-to tool for that purpose. JD Design's website (now hosted on github.io) has a number of interesting freeware and shareware utilities.  I used to use their TouchPro utility to set the file timestamps on software I was mastering to match its version number (e.g., version 3.00 of a program had all of its files dates set to 3:00AM, and so forth). Karenware has a number of interesting freeware utilities, too. Regards, Aryeh Goretsky  
  • Recent Achievements

    • Week One Done
      Jeroen Wilms earned a badge
      Week One Done
    • Week One Done
      rolfus earned a badge
      Week One Done
    • One Month Later
      Leroy Jethro Gibbs earned a badge
      One Month Later
    • Conversation Starter
      flexorcist earned a badge
      Conversation Starter
    • One Month Later
      AndreaB earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      509
    2. 2
      +Edouard
      198
    3. 3
      PsYcHoKiLLa
      138
    4. 4
      ATLien_0
      90
    5. 5
      Steven P.
      82
  • Tell a friend

    Love Neowin? Tell a friend!