• Post Reply Bookmark Topic Watch Topic
  • New Topic

Limiting duplicate user input choices in consecutive If/Else  RSS feed

 
Tommy Layton
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings Everyone!

I am a beginner trying to recreate character creation of Fallout 1 as a learning tool.
Please any help you can lend me on my below problem is very much appreciated!

Question:
How can I limit the user to spend a "Tag" on a different "Skill" on each inputFirstTag, inputSecondTag, inputThirdTag
Everything works fine until Round 2 rears its ugly head. inputSecondTag can become a duplicate of inputFirstTag, inputThirdTag can become a duplicate of inputSecondTag.

 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch and thanks for using code tags. One thing: you should break up long lines in the code because it messes up the way the screen is displayed.

In general, if you have a lot of repeated code like you do, think about what you can do to reduce it. Put common code into methods. You could definitely benefit from a method that verifies input and one that displays the increase. Give this a try and post your new code.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tommy Layton wrote:
Question:
How can I limit the user to spend a "Tag" on a different "Skill" on each inputFirstTag, inputSecondTag, inputThirdTag
Everything works fine until Round 2 rears its ugly head. inputSecondTag can become a duplicate of inputFirstTag, inputThirdTag can become a duplicate of inputSecondTag.


Do you mean he shouldn't be able to choose something in a subsequent round that he chose before in a previous round?

Two ways of doing it pop into my head. First way would be not offer it as an option in the subsequent round - don't present that option in the interface. That might not work in your case, so a second way of doing it would be to keep a list of things already chosen and if he tries to choose it again disallow it and print a message to that effect and then repeat. An ArrayList would be good for storing the previous choices. Hope I understood your problem
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than repeating a Skill like "Small Guns" all over the place, I suggest creating a Skill class to hold a name and a value field. Then you can make a List of Skill and loop through it instead of having to retype each skill name over and over.
 
Tommy Layton
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:
Tommy Layton wrote:
Question:
How can I limit the user to spend a "Tag" on a different "Skill" on each inputFirstTag, inputSecondTag, inputThirdTag
Everything works fine until Round 2 rears its ugly head. inputSecondTag can become a duplicate of inputFirstTag, inputThirdTag can become a duplicate of inputSecondTag.


Do you mean he shouldn't be able to choose something in a subsequent round that he chose before in a previous round?

Two ways of doing it pop into my head. First way would be not offer it as an option in the subsequent round - don't present that option in the interface. That might not work in your case, so a second way of doing it would be to keep a list of things already chosen and if he tries to choose it again disallow it and print a message to that effect and then repeat. An ArrayList would be good for storing the previous choices. Hope I understood your problem


I am having a new issue when trying to add an ArrayList bangHeadAgainstWall()
Here are my integers



and here is the new code with an ArrayList



You can probably see I am having an issue with things not adding up

CONSOLE WINDOW OUTPUT:
You have 3 Skills to Tag.
What skill would you like to Tag? (+25)
Small Guns, Big Guns, Energy Weapons, Unarmed, Melee Weapons, Throwing, First Aid, Doctor, Sneak, Lockpick, Steal, Traps, Science, Repair, Speech, Barter, Gambling or Outdoors?
Steal
Steal Increased by 25!

Press Enter to continue.

You have 2 Skills to Tag.
What skill would you like to Tag? (+20)
Small Guns, Big Guns, Energy Weapons, Unarmed, Melee Weapons, Throwing, First Aid, Doctor, Sneak, Lockpick, Steal, Traps, Science, Repair, Speech, Barter, Gambling or Outdoors?
Big Guns
Big Guns Increased by 20!

Press Enter to continue.

You have 1 Skill left to Tag.
What skill would you like to Tag? (+15)
Small Guns, Big Guns, Energy Weapons, Unarmed, Melee Weapons, Throwing, First Aid, Doctor, Sneak, Lockpick, Steal, Traps, Science, Repair, Speech, Barter, Gambling or Outdoors?
Unarmed
Unarmed Increased by 15!
Small Guns : 0
Big Guns : 20
Energy Weapons : 0
Unarmed : 15
Melee Weapons : 0
Throwing : 0
First Aid : 0
Doctor : 0
Sneak : 0
Lockpick : 0
Steal : 25
Traps : 0
Science : 0
Repair : 0
Speech : 0
Barter : 0
Gambling : 0
Outdoors : 0

Press Enter to continue.

Your new Skills are:
Small Guns: 30
Big Guns: 34
Energy Weapons: 28
Unarmed: 32
Melee Weapons: 32
Throwing: 34
First Aid: 30
Doctor: 30
Sneak: 28
Lockpick: 28
Steal: 28
Traps: 28
Science: 30
Repair: 28
Speech: 30
Barter: 30
Gambling: 30
Outdoors: 32
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats' not all clicking with me. All I meant was when he picks an item see if it's in my ArrayList an if it is disallow it and if it isn't add it to the ArrayList and proceed. Just keeps him from picking the same thing twice. If it's between rounds then you'll have to keep track of what round it is too.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Carey Brown is right. If you had a Skill class you could give it a toString method which returns "Gun skills: 150" or similar. Then all you have to do it print the entire list and you don't need that long block of print statements.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of your long if/else-if list you could have (assuming you have a Skill class and a skillList):

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not only are the long blocks of ifs error‑prone, but they are a maintenance nightmare.
You can simplify the finding of a Skill object if you have a Map to find a Skill from a String.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!