• Post Reply Bookmark Topic Watch Topic
  • New Topic

RPS with running total and ending after five games  RSS feed

 
Channing Johnson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Channing Johnson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having trouble adding these two features to my code. Any help Greatly Appreciated!!!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your biggest problem is that all your code is in a single, monolithic method.  you should refactor it into mulitple, small methods that each do one thing, then build them up into your larger program.  If you had a single method that played a game, it'd be easy to have a loop that calls it five times.  Then if that method returned a winner, you could keep your running score.
 
Channing Johnson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds like a lot! Is there a way to just implement them into what I all ready have?
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Channing Johnson wrote:That sounds like a lot! Is there a way to just implement them into what I all ready have?

This is how what you just asked is translated in the industry, by professional developers:

"I don't have time to do it right. Is there a way I can just write more crap code on top of the crap code that I already wrote?"

Just sayin'.  Either you get into the habit of cleaning up your code first to make it easier to make changes on it, or you can force an already horrible program into a state that's even more horrible.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Channing Johnson wrote:That sounds like a lot! Is there a way to just implement them into what I all ready have?

Probably. But I wouldn't put any effort into doing something the wrong way. It often ends up taking more time, causing more headaches, and is less likely to work.  And most of the time, you'll spend hours on doing it your way, only to eventually realize that my suggestion is the right way and you've wasted all that time.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a little tough lovin' from someone who has done this thing for a long time and is tired of watching people do things they shouldn't be doing.

It's not really that hard to start cleaning up your code.  One thing you can do is to push all the bad code that's working out into its little corner, like putting a bunch of stuff in the basement where nobody can see it. It's still there, and you can dig around for a couple of hours down there if you really need to, but if you're going to buy new furniture for your house, you want to clear out the living room of the old crap first and make room for the new stuff.

You can do the same kind of thing with a computer program like this.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what you need to think is this:  Is there a way I can hide all that ugliness that's in main() right now, and just let it run whenever I need to. Then in main(), I just say, "Do all that ugly stuff", and then it would get done and I just get back a result of who won that round.  Then, all I need to do is put a loop around that call to "Do all that ugly stuff" so it gets done 5 times.

You get the idea? Have you learned about methods yet?  If you haven't, you actually created one called main(). That's a method. You can write your own methods, too.
 
Channing Johnson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand and had no intention of it coming across that way but what I was asked to do is incorporate these two things into my existing code, so that is exactly what I was trying to accomplish. What I have has all ready been graded. I just need a bit of advice on how to add them to what is all ready written. I appreciate the guidance and recommendation to make what I have better and that is something I will do for practice but for this task that is not what I am suppose to do unless that is the only way I can add these new things.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Channing Johnson wrote:for this task that is not what I am suppose to do unless that is the only way I can add these new things.

Not to push our agenda but did the task description explicitly prohibit creating another method to make your program better organized?  Did it explicitly say "Don't put any code in any method except the main() method -- if you do that, you will be deducted points!" or something to that effect?
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because if it didn't, the best way to start is like how I described previously, to push all that code you have in main() right now into its own method.  So it looks something like this:

Now you've cleared out main() so that you can put some new stuff in there without having to be too distracted by the old code.
 
Channing Johnson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O.k. That makes sense.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Channing Johnson wrote:O.k. That makes sense.

Great. Now, just figure out how to run playOneRound() five times, have it return who won each time, then keep track of the results and display them as each round of play gets done.

Does that still make sense?
 
Channing Johnson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um no. That is where the problem lies.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Channing Johnson wrote:Um no. That is where the problem lies.

So, what's the problem. Don't you know how to make code execute more than once? If you don't, then you need to read up on how to use loops.  A for-loop or a while-loop is what you'll need.

If you don't know how to make playOneRound() return something, then you'll have to google for How to write a Java Method that Returns A Value
 
Channing Johnson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would I put the loop around the if else statements then?
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Channing Johnson wrote:Would I put the loop around the if else statements then?

You'll never get it done if you don't go and try to do it. Be adventurous, try it and see if it works. If not, what's the worst that could happen, right?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget the loop for now.  If you followed Junilu's advice, you have a method that plays the game once. update that method so that it returns who the winner is. Then, from the main method, print out who the winner of that one game is.

A lot of programming is adding a tiny piece of new behavior, writing a bit of code to test that new behavior, and then testing it. When you are sure it works, you often throw away that test code, then work on the next piece.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!