This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Having trouble with Math.random

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi i need to write a program that collects bottle caps. this program is supposed to collect all 5 bottle caps with the letters BASIC. Each letter has a ratio of being found...B-20% A-20% S-40% I-19% C-1%.....
i want to use math.random...the range of it i want is 1-100...i made if statements..the first 20 is b, next 20 is A, next 40 is S next 19 is I and the last one is C.... the program is supposed to print out the number of bottle caps needed to get all 5 to win a prise and the number of each kind. now the following code compiles, but when i run it it does not work.
would i rather need a random class?
pls help
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm... so what does the program do when it "does not work"? Print an error message? Print anything at all? You can always add some print statements for debugging purposes to figure out what's going on. E.g.

By studying the output of statements like this, you can get more information about what your program is actually doing. Is it what you expect? How is it different? When you get to a part of the program which behavaes differently than you expect, you can add additional print statements to get more info than I've provided so far. If the results still don't make sense to you, post here describing what sort of results you're getting. Hope that helps...
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without looking too deeply at your code, the first thing I see is that your loop will only run once. The reason is that - according to your 'if' conditional, the loop only runs AS LONG AS ALL letters == 0. Once you increment a letter ( which should be done B += 1 ), you make this statement false, so the loop stops executing. Does that help?
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic