473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

I need a validation loop

6
I'm writing a program for class that is simple Java. Its a game of War that just keeps track of the score. I need the input to be validated so the user can only enter a value of 2-14 for both cards.

Here is what I have so far:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <script type="text/javascript">
  4.  
  5. document.write("Welcome to the game of war!")
  6. var playerCard = 99
  7. var holdTotal = 0
  8. var playerTotal = 0
  9. var computerTotal = 0
  10.  
  11. while (playerCard !=0) {
  12.    var playerCard = prompt("Enter the card value for the player")
  13.    playerCard = parseInt(playerCard)
  14.    var computerCard = prompt("Enter the card value for the computuer")
  15.    computerCard = parseInt(computerCard)
  16.  
  17.  
  18. if (playerCard > computerCard) {
  19.    document.write("<p>The player wins the hand!")
  20.    playerTotal = playerCard + computerCard + holdTotal + playerTotal
  21.    document.write(" Current score: Player " + playerTotal + " , Computer " + computerTotal)
  22. }
  23. else if (computerCard > playerCard) {
  24.    document.write("<p>The computer wins the hand!")
  25.    computerTotal = playerCard + computerCard + holdTotal + computerTotal
  26.    document.write(" Current score: Player " + playerTotal + " , Computer " + computerTotal)
  27. }
  28. else {
  29.  
  30.    holdTotal = playerCard + computerCard 
  31.    document.write("<p>The hand is a tie. The hold total is " + holdTotal) 
  32. }
  33. }
  34.  
  35. document.write("<p>Current score: Player " + playerTotal + " , Computer " + computerTotal)
  36.  
  37. if (playerTotal > computerTotal) {
  38.    document.write("<p>The winner of the game is: Player!")
  39. }
  40. else if (computerTotal > playerTotal) {
  41.    document.write("<p>The winner of the game is: Computer!")
  42. }
  43. else {
  44.    document.write("<p>The game is a tie.")
  45. }
  46.  
  47. </script>
  48. </body>
  49. </html>

I want to put the validation after the 1st while loop (playerCard !=0). I have tried this and every time i stick a while validation loop in there it crashes the program. It will still execute, it will just crash when I enter a value

For example: I tried using while (playerCard = 1; playerCard < 0; playerCard > 14) document.write("Error! Please enter card value of (2-14)")

But that crashes the program.
May 4 '10 #1

✓ answered by acoder


9 1864
Madden
6
Maybe I came to the wrong place? From looking around, alot of other questions seem really complex. I thought my problem would be simple for you guys.
May 4 '10 #2
Dormilich
8,658 Expert Mod 8TB
to make it short, your tried while() syntax is wrong. mind that while() only expects 1 condition.

hint: use logical operators (&&, ||, !)
May 5 '10 #3
acoder
16,027 Expert Mod 8TB
Another problem: your use of document.write. You can use document.write while the page is loading, but try to avoid it if you can. Do you know how to "write" something to the page without using document.write?
May 5 '10 #4
Madden
6
@acoder
No. document.write is the only way I've been taught. The project is due today, so I probably won't get that in. I don't know (&&, !) its a basic psuedocode class.
May 5 '10 #5
acoder
16,027 Expert Mod 8TB
@Madden
See while loop
and comparison operators (or a more mdetailed explanation).
May 5 '10 #6
Madden
6
So if I use if (playerCard = 1 || playerCard < 0 || playerCard > 14) {
document.write("Error! Please enter valid card value")
}

Would that work?
May 5 '10 #7
Madden
6
I stuck that piece of code in my program, and when I enter 0 it says that is an invalid number
May 5 '10 #8
Madden
6
Never mind, I got it on my own.
Expand|Select|Wrap|Line Numbers
  1.  <html>
  2. <body>
  3. <script type="text/javascript">
  4.  
  5. document.write("Welcome to the game of war!")
  6. var playerCard = 99
  7. var holdTotal = 0
  8. var playerTotal = 0
  9. var computerTotal = 0
  10.  
  11. while (playerCard !=0) {
  12.    var playerCard = prompt("Enter the card value for the player")
  13.    playerCard = parseInt(playerCard)
  14.  
  15. while (playerCard == 1 || playerCard < 0 || playerCard > 14) {
  16.    document.write("<p>Error! Please enter valid card value")
  17.    var playerCard = prompt("Enter the card value for the player")
  18.    playerCard = parseInt(playerCard)
  19. }
  20. var computerCard = prompt("Enter the card value for the computer")
  21.    computerCard = parseInt(computerCard)
  22.  
  23. while (computerCard == 1 || computerCard < 0 || computerCard > 14){
  24.    document.write("<p>Error! Please enter valid card value")
  25.    var computerCard = prompt("Enter the card value for the computuer")
  26.    computerCard = parseInt(computerCard)
  27. }
  28.  
  29. if (playerCard > computerCard) {
  30.    document.write("<p>The player wins the hand!")
  31.    playerTotal = playerCard + computerCard + holdTotal + playerTotal
  32.    document.write(" Current score: Player " + playerTotal + " , Computer " + computerTotal)
  33. }
  34. else if (computerCard > playerCard) {
  35.    document.write("<p>The computer wins the hand!")
  36.    computerTotal = playerCard + computerCard + holdTotal + computerTotal
  37.    document.write(" Current score: Player " + playerTotal + " , Computer " + computerTotal)
  38. }
  39. else {
  40.  
  41.    holdTotal = playerCard + computerCard 
  42.    document.write("<p>The hand is a tie. The hold total is " + holdTotal) 
  43. }
  44. }
  45.  
  46. document.write("<p>Current score: Player " + playerTotal + " , Computer " + computerTotal)
  47.  
  48. if (playerTotal > computerTotal) {
  49.    document.write("<p>The winner of the game is: Player!")
  50. }
  51. else if (computerTotal > playerTotal) {
  52.    document.write("<p>The winner of the game is: Computer!")
  53. }
  54. else {
  55.    document.write("<p>The game is a tie.")
  56. }
  57.  
  58. </script>
  59. </body>
  60. </html>
  61.  
May 5 '10 #9
acoder
16,027 Expert Mod 8TB
Well, that's good. It's one way to do it. Alternative ways would be to change the while condition to reflect the 2-14 and not 0, or add comments to that effect, and perhaps get rid of the duplicate prompts code.
May 6 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Harry_Crow | last post by:
I have a schema file PDDSch.xsd which validates the PDD.xml file. the rootnodes are as shown below PDD.xml...
3
by: ash | last post by:
Hey I am new, but I don't have time to intruduce myself yet. I am intro to C++ and this is a programme I have to write. all the direction are here, It will be very nice of someone to figure this...
3
by: Rob Meade | last post by:
Hi all, I have a login page which has username and password fields, a login button, and 2 validation controls (one for each field) - currently I have controls to display to the summary if the...
2
by: Avad | last post by:
I have a following screen. The login is a "user control" with validations controls in it. This side I have address book in which "name" is required field. But when I click on "add new user" the...
1
by: Darrel | last post by:
I am creating a server control that inherits from a command button. The problem is it needs to generate different client side script, depending on whether validators will get added to the page or...
1
by: Frankie | last post by:
Sanity Check Requested! I've been writing code way too long for the past several days and just came across this bit of logic I wrote several weeks ago. Suddenly something seems wrong with it and...
11
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether...
46
by: Bruce W. Darby | last post by:
This will be my very first VB.Net application and it's pretty simple. But I've got a snag in my syntax somewhere. Was hoping that someone could point me in the right direction. The history: My...
6
by: lucoin | last post by:
Hello guys, I met a problem about php and javascipt For a tickets booking system, when a customer search for flights from one city, so in the data base there should be many routes to different...
8
by: Bryan | last post by:
I want my business objects to be able to do this: class Person(base): def __init__(self): self.name = None @base.validator def validate_name(self): if not self.name: return
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.