Results 1 to 7 of 7

Thread: Counter

  1. #1
    ks1615 is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default 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);
    }
    
    
    
    }
    }

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    10

    Default 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:
    Java Code:
    if(win != number)
    here your saying "if win is not equal to number"

    I think the statement should be:
    Java Code:
    if(win == number)
    "if win is equal to number"

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    22

    Default

    And another good practice is variable naming convention. As a rule local variable names are started with lower letters.

    Not like this.

    Java Code:
    int Matches = 0;
    Mush better way is,

    Java Code:
    int matches = 0;

  4. #4
    ks1615 is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default

    Wow thanks, that fixed my issue!!

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    10

    Default

    Welcome...

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    22

  7. #7
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    8

    Default

    (Top of the page -> click Thread Tools -> Click Mark This Thread As Solved)
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

Similar Threads

  1. Replies: 1
    Last Post: 02-11-2009, 07:54 AM
  2. Need helps making counter
    By Lifeis2evil in forum New To Java
    Replies: 3
    Last Post: 11-27-2008, 12:58 AM
  3. Frequency Counter
    By justlearning in forum New To Java
    Replies: 0
    Last Post: 05-07-2008, 10:50 PM
  4. [SOLVED] BST Frequency Counter
    By theonly in forum Advanced Java
    Replies: 7
    Last Post: 04-29-2008, 11:33 PM
  5. Help with static variable counter
    By silvia in forum New To Java
    Replies: 1
    Last Post: 07-19-2007, 07:53 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
  •