Results 1 to 7 of 7
Thread: Counter
- 02-20-2009, 01:14 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 11
- Rep Power
- 0
Counter
I'm trying to write a program that does the following:
A number is designated as int win = 1.
Then a random number is generated.
If the number matches the winning number (1) then a counter is added.
What my program continues to do is cotinually add rather than obey the if statement.
Basically if number matches 1, a counter i added and this loop continues until 20 random numbers.
Java Code:import java.io.IOException; import java.io.PrintWriter; import java.io.File; import java.util.Random; public class BottleCapPrize { public static void main (String [ ] args) throws IOException { PrintWriter outFile = new PrintWriter(new File("montecarlo.txt")); int number = 0; int Matches = 0; Random randNumList = new Random(); int win = 1; for(int cap = 1; cap <= 20; cap++) { number = randNumList.nextInt(6); if(win != number) Matches++; System.out.println(Matches); } } }
- 02-20-2009, 01:36 AM #2
to be equal or not to be equal...
Golden rule of programming... always place curly bracket ({}) after if, else and loop statements.
But the real problem is the comparation:
here your saying "if win is not equal to number"Java Code:if(win != number)
I think the statement should be:
"if win is equal to number"Java Code:if(win == number)
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-20-2009, 02:04 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
And another good practice is variable naming convention. As a rule local variable names are started with lower letters.
Not like this.
Mush better way is,Java Code:int Matches = 0;
Java Code:int matches = 0;
- 02-20-2009, 02:04 AM #4
Member
- Join Date
- Feb 2009
- Posts
- 11
- Rep Power
- 0
Wow thanks, that fixed my issue!!
- 02-20-2009, 02:14 AM #5
Welcome...
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-20-2009, 02:16 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
And please mark the thread solved. :)
- 02-20-2009, 03:02 AM #7
Similar Threads
-
how to update and display the hit counter on my very first jsp page.
By R O C K Y in forum Advanced JavaReplies: 1Last Post: 02-11-2009, 06:54 AM -
Need helps making counter
By Lifeis2evil in forum New To JavaReplies: 3Last Post: 11-26-2008, 11:58 PM -
Frequency Counter
By justlearning in forum New To JavaReplies: 0Last Post: 05-07-2008, 10:50 PM -
[SOLVED] BST Frequency Counter
By theonly in forum Advanced JavaReplies: 7Last Post: 04-29-2008, 11:33 PM -
Help with static variable counter
By silvia in forum New To JavaReplies: 1Last Post: 07-19-2007, 07:53 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks