• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

nested if statements  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, can someone give me some tips on using a nested if statements in a rock, paper, and scissors program? I can make it work with else if statements, but I can't seem to figure it out how to turn it into a nested if statement. Thanks!
 
Marshal
Posts: 6089
418
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Not a bad tip would be "don't use them". Or more precisely, if you find yourself you need to nest something - extract nested part to a separate method by describing what this nested part supposed to be doing. However, that is just a general advice.

Please show us the code you wrote with if-else, and please explain why the approach you took isn't good so you decided to change to something else?
 
Z Williams
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is for an assignment, we are required to use a nested if statement.
 
Saloon Keeper
Posts: 9378
181
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What an absolutely useless exercise. Instead of teaching good programming habits, the class goes straight for the bad ones. Forget this lesson immediately when you're done with it.

First I will tell you the things that are wrong with your current code. You don't have to change it to complete the assignment, but remember them for the future. Then, I will give you a hint how you can fulfill the assignment.

You have way too much code in your main method. Methods should be short and simple, and they should do one thing well. Your main method handles both the user interface and game logic. Don't do this.

Don't use strings to represent abstract concepts like the choices a player can make in a game of Tic Tac Toe. Create separate classes to model your game.

Use appropriate names for your variables. Your variables player1 and player2 appear to represent players, but they don't. They represent choices. Name them something like firstChoice and secondChoice or player1Choice and player2Choice.

Now, here's what you can do to finish the assignment: First write three if/else clauses for each of the choices that the first player can make. Then, inside each of them, nest three if/else clauses for each of the choices that the second player can make.

Finally, you can make a slight optimization to your program by removing from each outer if or else statement the if or else statement in which the first and second player's choices are the same, and make a special case for it by checking that the two choices are equal.
 
Liutauras Vilda
Marshal
Posts: 6089
418
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP
Maybe something similar to?
 
Z Williams
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Stephan van Hulst, @Liutauras Vilda thanks for the tips. I figured out how to make this a nested if. I just added this line of code before the first if statement

and changed the else if to if. For this assignment, I think this will do.
 
Liutauras Vilda
Marshal
Posts: 6089
418
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Z Williams wrote:changed the else if to if. For this assignment, I think this will do.


Stephan was way more elaborate than I did, so read his advises carefully.

Don't think whether it will do (just to meet bare minimum) for this exercise or no - try to aim best you can do for this exercise. Read once again what Stephan wrote, make 2 versions of this application. First version so it would go sort of along the lines what Stephan advised (some parts may be too early for you as of now, but no one can stop you from taking challenge), Second version what the assignment requires to submit.

First one would be way closer to what you'll be doing in your programmer's career. Second one probably would be a throw away thing in near future.
 
Z Williams
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Z Williams wrote:changed the else if to if. For this assignment, I think this will do.


Stephan was way more elaborate than I did, so read his advises carefully.

Don't think whether it will do (just to meet bare minimum) for this exercise or no - try to aim best you can do for this exercise. Read once again what Stephan wrote, make 2 versions of this application. First version so it would go sort of along the lines what Stephan advised (some parts may be too early for you as of now, but no one can stop you from taking challenge), Second version what the assignment requires to submit.

First one would be way closer to what you'll be doing in your programmer's career. Second one probably would be a throw away thing in near future.



Thanks, this would be the second version. I will try and make another version to improve the current version. I have just started programming, so I'm still new to this. But, I will take any constructive criticism.
 
Liutauras Vilda
Marshal
Posts: 6089
418
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Z Williams wrote:I have just started programming, so I'm still new to this. But, I will take any constructive criticism.


We fully understand you, that's fine And well done for a good mindset.
 
Stephan van Hulst
Saloon Keeper
Posts: 9378
181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Z Williams wrote:For this assignment, I think this will do.


I don't think so. The point of the assignment is to eliminate the use of logic operators by nesting if-statements. The following two statements are equivalent:



Your teacher wants you to eliminate all logic operators by using nested if-statements.
 
Z Williams
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Z Williams wrote:For this assignment, I think this will do.


I don't think so. The point of the assignment is to eliminate the use of logic operators by nesting if-statements. The following two statements are equivalent:
Your teacher wants you to eliminate all logic operators by using nested if-statements.



Oh, I get what you're saying. So, instead of combining them in one if statement; I have to put them into nested if statements. Like the second code you wrote, Correct?




Also, here's the actual assignment,


Remember the childhood game “Rock, Paper, Scissors”? It is a two-player game in which each
person simultaneously chooses either rock, paper, or scissors. Rock beats scissors but loses to paper,
paper beats rock but loses to scissors, and scissors beats paper but loses to rock. Write a Java
program that prompts player 1 and player 2 to each enter a string: rock, paper, or scissors. Finish the
program by adding nested if statements to appropriately report “Player 1 wins”, “Player 2 wins”, or
“It is a tie.”

 
Stephan van Hulst
Saloon Keeper
Posts: 9378
181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. So first complete the assignment in the most straightforward form: 3 conditions for the first player, with 3 x 3 conditions for the second player. Post your code when you're done, then we'll discuss a slight improvement for ties.
 
Z Williams
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Is this correct? Do you just make 3 of the same code and change the value in each one?
 
Liutauras Vilda
Marshal
Posts: 6089
418
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Z Williams wrote:Is this correct? Do you just make 3 of the same code and change the value in each one?


You have been given enough information to assemble solution. Certainly telling you more possibly would be considered as cheating.

Try to read all the replies again, implement new approach, AND test the outcome of new approach against the initial one whether they match, so you'd know the correctness (or no) of the program.
 
Z Williams
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, how does this look?


 
Stephan van Hulst
Saloon Keeper
Posts: 9378
181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try it out? Run it with all combinations of choices for player 1 and player 2 and see if it prints what you expect it to print.
 
Marshal
Posts: 60766
189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch again

Z Williams wrote:Okay, how does this look? . . .

Not at all good, I am afraid. Sorry.

Go back to pencil paper and eraser and work out how the game is played. Show us the description you get and then later we can work out what classes you would need.
 
Z Williams
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Did you try it out? Run it with all combinations of choices for player 1 and player 2 and see if it prints what you expect it to print.



I have tested, everything works.
 
Stephan van Hulst
Saloon Keeper
Posts: 9378
181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, and that's good. The problem however is that you're comparing player 1's choice nine times.

Why do you check that the player had chosen rock on line 13, when you already know they've chosen rock on line 5?
 
Z Williams
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Yes, and that's good. The problem however is that you're comparing player 1's choice nine times.

Why do you check that the player had chosen rock on line 13, when you already know they've chosen rock on line 5?


That's a different possibility? Sorry, this is the only way I know how to do this with what I know.
 
Saloon Keeper
Posts: 4988
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote: First write three if/else clauses for each of the choices that the first player can make. Then, inside each of them, nest three if/else clauses for each of the choices that the second player can make.

Finally, you can make a slight optimization to your program by removing from each outer if or else statement the if or else statement in which the first and second player's choices are the same, and make a special case for it by checking that the two choices are equal.

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!