Results 1 to 7 of 7
  1. #1
    worldchamp is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default what's wrong with this code?

    I've been trying the following code for a while but in vain. I've tried simple text editors and even various IDEs. Please tell me what's wrong with this:

    import java.util.Random;
    class PlayTwentyOne {//even adding "public" before makes no difference
    public static void main(String args[]) {
    Random myRandom = new Random();
    int card, total = 0;
    System.out.println(“Card Total”);
    while (total < 21) {
    card = myRandom.nextInt(10) + 1;
    total += card;
    System.out.print(card);
    System.out.print(“ “);
    System.out.println(total);
    }
    if (total == 21) {
    System.out.println(“You win :-)”);
    } else {
    System.out.println(“You lose :-(“);
    }
    }
    }

  2. #2
    musclecode is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: what's wrong with this code?

    where are you from? Looks right for me, just problems with commas (not sure of name, I mean this -> " ). Try it:

    import java.util.Random;

    class PlayTwentyOne {//even adding "public" before makes no difference
    public static void main(String args[]) {
    Random myRandom = new Random();
    int card, total = 0;
    System.out.println("Card Total");
    while (total < 21) {
    card = myRandom.nextInt(10) + 1;
    total += card;
    System.out.print(card);
    System.out.print(" ");
    System.out.println(total);
    }
    if (total == 21) {
    System.out.println("You win :- ");
    } else {
    System.out.println("You lose :-(");
    }
    }
    }

  3. #3
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: what's wrong with this code?

    When i tried to run your code in netbeans, the only errors it gave were to do with the quotation marks surrounding your strings. I re-entered the quotation marks again, and your program runs.

  4. #4
    musclecode is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: what's wrong with this code?

    Thats what I said. Code looks right.

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: what's wrong with this code?

    Please tell me what's wrong
    You first...does it compile? Are there exceptions? Does it behave as expected? You don't want to leave us guessing do you?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: what's wrong with this code?

    Forum Rules -- particularly the third paragraph
    Guide For New Members
    BB Code List - Java Programming Forum

    Don't be too lazy to type your code. The cause of your problem is copy/paste.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    worldchamp is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: what's wrong with this code?

    First of all thanks everyone for replying so quickly.
    @musclecode: yes, the code looks right and actually it runs now perfectly.
    @wdh321: yes, that solves the problem! I guess the error arose due to mismatch in text formats.
    @DarrylBurke: Sir, I am not lazy. But yes, after running it in various editors, I resorted to copy paste after a few runs.

    By the way, I'd like to know why this happened?
    (I mean this shouldn't have been the case with the simple notepad text editor.)
    But anyways, I learned a new thing today. So, thank you all very much.

Similar Threads

  1. What's wrong with my code/ :-/
    By Denis2k11 in forum New To Java
    Replies: 5
    Last Post: 10-13-2012, 04:51 PM
  2. What is wrong with this code?
    By ayershov777 in forum New To Java
    Replies: 5
    Last Post: 09-22-2012, 05:12 AM
  3. What is wrong with the Code below
    By Bahramudin in forum New To Java
    Replies: 4
    Last Post: 08-16-2012, 07:10 PM
  4. Need help! something wrong in my code
    By novak100 in forum New To Java
    Replies: 2
    Last Post: 11-18-2009, 11:59 PM
  5. What's going wrong with this code?
    By Suurisa in forum New To Java
    Replies: 5
    Last Post: 10-19-2009, 11:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •