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: - <html>
-
<body>
-
<script type="text/javascript">
-
-
document.write("Welcome to the game of war!")
-
var playerCard = 99
-
var holdTotal = 0
-
var playerTotal = 0
-
var computerTotal = 0
-
-
while (playerCard !=0) {
-
var playerCard = prompt("Enter the card value for the player")
-
playerCard = parseInt(playerCard)
-
var computerCard = prompt("Enter the card value for the computuer")
-
computerCard = parseInt(computerCard)
-
-
-
if (playerCard > computerCard) {
-
document.write("<p>The player wins the hand!")
-
playerTotal = playerCard + computerCard + holdTotal + playerTotal
-
document.write(" Current score: Player " + playerTotal + " , Computer " + computerTotal)
-
}
-
else if (computerCard > playerCard) {
-
document.write("<p>The computer wins the hand!")
-
computerTotal = playerCard + computerCard + holdTotal + computerTotal
-
document.write(" Current score: Player " + playerTotal + " , Computer " + computerTotal)
-
}
-
else {
-
-
holdTotal = playerCard + computerCard
-
document.write("<p>The hand is a tie. The hold total is " + holdTotal)
-
}
-
}
-
-
document.write("<p>Current score: Player " + playerTotal + " , Computer " + computerTotal)
-
-
if (playerTotal > computerTotal) {
-
document.write("<p>The winner of the game is: Player!")
-
}
-
else if (computerTotal > playerTotal) {
-
document.write("<p>The winner of the game is: Computer!")
-
}
-
else {
-
document.write("<p>The game is a tie.")
-
}
-
-
</script>
-
</body>
-
</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.
9 1864
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.
to make it short, your tried while() syntax is wrong. mind that while() only expects 1 condition.
hint: use logical operators (&&, ||, !)
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?
@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.
So if I use if (playerCard = 1 || playerCard < 0 || playerCard > 14) {
document.write("Error! Please enter valid card value")
}
Would that work?
I stuck that piece of code in my program, and when I enter 0 it says that is an invalid number
Never mind, I got it on my own. - <html>
-
<body>
-
<script type="text/javascript">
-
-
document.write("Welcome to the game of war!")
-
var playerCard = 99
-
var holdTotal = 0
-
var playerTotal = 0
-
var computerTotal = 0
-
-
while (playerCard !=0) {
-
var playerCard = prompt("Enter the card value for the player")
-
playerCard = parseInt(playerCard)
-
-
while (playerCard == 1 || playerCard < 0 || playerCard > 14) {
-
document.write("<p>Error! Please enter valid card value")
-
var playerCard = prompt("Enter the card value for the player")
-
playerCard = parseInt(playerCard)
-
}
-
var computerCard = prompt("Enter the card value for the computer")
-
computerCard = parseInt(computerCard)
-
-
while (computerCard == 1 || computerCard < 0 || computerCard > 14){
-
document.write("<p>Error! Please enter valid card value")
-
var computerCard = prompt("Enter the card value for the computuer")
-
computerCard = parseInt(computerCard)
-
}
-
-
if (playerCard > computerCard) {
-
document.write("<p>The player wins the hand!")
-
playerTotal = playerCard + computerCard + holdTotal + playerTotal
-
document.write(" Current score: Player " + playerTotal + " , Computer " + computerTotal)
-
}
-
else if (computerCard > playerCard) {
-
document.write("<p>The computer wins the hand!")
-
computerTotal = playerCard + computerCard + holdTotal + computerTotal
-
document.write(" Current score: Player " + playerTotal + " , Computer " + computerTotal)
-
}
-
else {
-
-
holdTotal = playerCard + computerCard
-
document.write("<p>The hand is a tie. The hold total is " + holdTotal)
-
}
-
}
-
-
document.write("<p>Current score: Player " + playerTotal + " , Computer " + computerTotal)
-
-
if (playerTotal > computerTotal) {
-
document.write("<p>The winner of the game is: Player!")
-
}
-
else if (computerTotal > playerTotal) {
-
document.write("<p>The winner of the game is: Computer!")
-
}
-
else {
-
document.write("<p>The game is a tie.")
-
}
-
-
</script>
-
</body>
-
</html>
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |