Results 1 to 7 of 7
Thread: what's wrong with this code?
- 11-29-2012, 08:21 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
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 :-(“);
}
}
}
- 11-29-2012, 09:20 PM #2
Member
- Join Date
- Aug 2012
- Posts
- 9
- Rep Power
- 0
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 :-(");
}
}
}
- 11-29-2012, 10:11 PM #3
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
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.
- 11-29-2012, 10:18 PM #4
Member
- Join Date
- Aug 2012
- Posts
- 9
- Rep Power
- 0
Re: what's wrong with this code?
Thats what I said. Code looks right.
- 11-29-2012, 10:47 PM #5
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
Re: what's wrong with this code?
You first...does it compile? Are there exceptions? Does it behave as expected? You don't want to leave us guessing do you?Please tell me what's wrong
- 11-30-2012, 02:48 AM #6
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 11-30-2012, 09:49 AM #7
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
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
-
What's wrong with my code/ :-/
By Denis2k11 in forum New To JavaReplies: 5Last Post: 10-13-2012, 04:51 PM -
What is wrong with this code?
By ayershov777 in forum New To JavaReplies: 5Last Post: 09-22-2012, 05:12 AM -
What is wrong with the Code below
By Bahramudin in forum New To JavaReplies: 4Last Post: 08-16-2012, 07:10 PM -
Need help! something wrong in my code
By novak100 in forum New To JavaReplies: 2Last Post: 11-18-2009, 11:59 PM -
What's going wrong with this code?
By Suurisa in forum New To JavaReplies: 5Last Post: 10-19-2009, 11:59 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks