Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

First post and question  RSS feed

 
Jimmy Wieringen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello CodeRanch!,

This year september i started at my education program and i want to become a software engineer, the first programming language i need to learn is java.
Well it is going quite OK i think, after two months we learned the basics, how to start programming, if and else , booleans, arrays, int, doubles, etcc... quite the basic stuff i think..

I got the feeling i dont get programming for some reason, i see people on the internet do magical things in java ,, i really like programming i think its one of the best school subjects i have atm.
But still, i feel kinda dumb in java learning to code..

Oke that was the background of my question :P

Soooo i have been programming alot lately especially this week since i have my first exam tommorow!,,
I am programming this program for exercise and to prep me for the exam, but i have a isseu with it, i am kinda stuck atm..

So the assignment says the fallowing, I need to make a program for sport game scores.. the best of 7.
So there are 2 teams, and the user can input the names of the teams, and what they scored all games.

The first team that wins 4 games wins the best of 7 tournement.

The assignment tells me i need to use arrays for the scores and loops to check what team wins each game, annd output it for the user (output is the simple part)
Also if one team wins 4 games before al 7 games are played the program must stop cause that team wins the tournement.





The thing i want to know, is how do i use the arrays to check the scores and tell howmuch games there are played using a loop?
I really dont know where to start.

Any comments or feedback on the code is very very welcome! :)


This is my code so far:
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

My first comment is when you find yourself repeating the same basic chunk of code each time then it either ought to be a loop or a method. I'm guessing methods are beyond your knowledge at the moment so in your code where you get the team's scores 7 times you should have a loop which repeats for the length of the scores array (ie 7 times). Inside this loop you ask for each teams score and do a check to see if either team has won 4 games.
 
Jimmy Wieringen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!, thats what i meen, and that knowledge i want haha,,, how do i apply the loop? do i use a for loop ?

I dont see the logic yet at how i apply this and how i read it. Any tips ?
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I needed a user to enter 5 names I could do:


Or I could do this:



The first option may seem acceptable for a few names but it would soon get tiresome if there were 20 names. Now what if you'd used option 1 for 5 names and then your boss turned around and said it now needs to be 10 names. With option 1 you have a lot of coding to do but with option 2 all you have to do is change the size of the array. Or what if your boss says he doesn't like the prompt so change it to "Please enter a name", with option 1 you have to change it for every block of code whereas in option 2 you just change it in one place.

 
Jimmy Wieringen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i am almost not that confused anymore,

As i see it what you posted is,

I can make a string for team 1 his scores and loop that with that for loop for the amount of times needed.

So lets say 7 times.

After that i do the same for team 2, so now both teams have played 7 games?

But how do i compare them afterward so the team with the hightest score wins?


Thanks for comment !
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. You only need one loop and you ask for both teams scores in that loop, as I've previously stated. If you did it with two loops you can't fulfil the requirement of the assignment to end the game as soon as one team has won 4 games
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My suggestion would be to step away from the computer, and write down how you are going to do this in pseudo code.

Your explanation in the last post was effectively

- Enter Team1 name
- Enter Team 2 name
- Enter Team1 scores for all games - loop 7 times entering in a score for each game
- Enter Team 2 scores for all games - loop 7 times entering in a score for each game

The problem is that doing it this way, means that you won't be able to meet the requirement of "stop once one team wins 4 games"

Can you think of a better way to do it?
Is a for loop the most appropriate way? Are there any other loop styles that will work also? There isn't any "correct" answer here, but you should at least consider which style of loop to use.



 
Jimmy Wieringen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the comments, i am going to let it be till tommorow and then take a fresh look on it! Thanks for the tip to write down my actions! i am going to use that tip on my exam tommorow!

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!