Java Dice Rolling Program! -- Quick Question!

Just a quick question regarding how to properly implement a running st

Page 1 of 1

8 Replies - 4957 Views - Last Post: 22 March 2009 - 10:35 PM

#1 charlsy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-March 09

Java Dice Rolling Program! -- Quick Question!

Post icon  Posted 22 March 2009 - 09:06 PM

Okay, so as you can see below, I am creating a simple javascript program that rolls a die whenever a user clicks 'roll.'
It needs to display the value of each roll (which it is doing), and keep a running statistic of the number of times a: 1 is rolled, a 6 is rolled, and the total number of rolls. I believe I have set up the code correctly...but I don't know what to put after the 'document.myForm.num1.value'=??????? in order to get it to properly keep a running statistic. I have tried ="++1" and = "+1". I am not sure if i need to use a parseInt...

As you can see I'm quite confused about this and any help you can offer would be greatly appreciated! Thanks!

<html>
 <h1>Dice Roll</h1>
  <body>
	<form name="myForm">
		Roll the dice....<input type=button value="Press Me!" onclick='
		var num;
		num = 1;
		var dieValue;	
		var dieValue=Math.floor(Math.random()*6+1);
		document.myForm.roll.value=dieValue;

			if (dieValue == 1) {
				document.myForm.num1.value
				}
			else if (dieValue == 6) {
				document.myForm.num6.value
				}
		'><br>

	You rolled a:<input type=text name="roll"><br>
	Number of times a 1 is rolled:<input type=text name="num1" value="0"><br>
	Number of times a 6 is rolled:<input type=text name="num6" value="0"><br>
	Number of Rolls:<input type=text name="numrolls" value="0"><br>

		


  </body>
</html>




Is This A Good Question/Topic? 0
  • +

Replies To: Java Dice Rolling Program! -- Quick Question!

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5365
  • View blog
  • Posts: 27,328
  • Joined: 10-May 07

Re: Java Dice Rolling Program! -- Quick Question!

Posted 22 March 2009 - 09:09 PM

I'm guessing this is Javascript...?

If so, you need script tags, & you can't throw Javascript code in the middle of a tag like you have. You need to declare a function & then call it.

If this isn't Javascript... what exactly is it?
Was This Post Helpful? 0
  • +
  • -

#3 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Java Dice Rolling Program! -- Quick Question!

Posted 22 March 2009 - 09:19 PM

View Postno2pencil, on 22 Mar, 2009 - 10:09 PM, said:

I'm guessing this is Javascript...?

If so, you need script tags, & you can't throw Javascript code in the middle of a tag like you have. You need to declare a function & then call it.

If this isn't Javascript... what exactly is it?


It is Javascript. It says so in the first line of their post. ;)

It's Javascript...so unfortunately, I'm no help. :(
Was This Post Helpful? 0
  • +
  • -

#4 charlsy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-March 09

Re: Java Dice Rolling Program! -- Quick Question!

Posted 22 March 2009 - 09:25 PM

haha well..yes it is javascript.

And the code works, I am simply not sure how to keep running statistics.
Was This Post Helpful? 0
  • +
  • -

#5 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Java Dice Rolling Program! -- Quick Question!

Posted 22 March 2009 - 09:32 PM

I actually find it quite odd that it works...no offense to your code. I just don't see any Javascript tags anywhere...:unsure:

This post has been edited by Locke: 22 March 2009 - 09:32 PM

Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5365
  • View blog
  • Posts: 27,328
  • Joined: 10-May 07

Re: Java Dice Rolling Program! -- Quick Question!

Posted 22 March 2009 - 09:43 PM

** Moved to Javascript **
Was This Post Helpful? 0
  • +
  • -

#7 charlsy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-March 09

Re: Java Dice Rolling Program! -- Quick Question!

Posted 22 March 2009 - 09:51 PM

Okay, well I've done some searching on the site, and actually found an extremely similar (in fact it looks identical) post from another user, asking the same question.
Mabye you guys will understand this a little better? (Although the goal stays the same)

<html>
<head>

<script>

function roll()
	{
	
	document.myform.result.value =
	Math.floor((Math.random()*6)+1);
	
	document.myform.total.value =
	parseInt(document.myform.total.value) + 1;
	
	document.myform.number1.value =
		parseInt(document.myform.result.value) + 1;
	

	document.myform.number6.value =
	parseInt(document.myform.result.value) + 1;
	}


</script>

<form name = "myform">
		
		
		
		<input = text name = result>
		
		<br>Die Result <br>
		<br>
		
		<input = text name = total value = 0>
		
		<br>Number of Rolls <br>
		<br>
		
		<input = text name = number1 value = 0>
		
		<br>Die Rolls 1<br>
		<br>
		
		<input = text name = number6 value = 0>
		
		<br>Die Rolls 6<br>
		<br>
		
		
		<br>
		<input type = button value = "Roll!" onclick = roll()>
		
		

		

</form>

</head>
</html>


:blink:
Was This Post Helpful? 0
  • +
  • -

#8 charlsy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-March 09

Re: Java Dice Rolling Program! -- Quick Question!

Posted 22 March 2009 - 10:25 PM

For any of you that were interested, I figured it out!!

<html>
 <h1>Dice Roll</h1>
  <body>
	<form name="myForm">
		Roll the dice....<input type=button value="Press Me!" onclick='
		
		var dieValue;	
		var dieValue=Math.floor(Math.random()*6+1);
		document.myForm.roll.value=dieValue;
		document.myForm.numrolls.value=parseInt(document.myForm.numrolls.value)+1;


			if (dieValue == 1) {
				document.myForm.num1.value=parseInt(document.myForm.num1.value) + 1
				}
			else if (dieValue == 6) {
				document.myForm.num6.value=parseInt(document.myForm.num6.value) + 1
				}
		'><br>

	You rolled a:<input type=text name="roll"><br>
	Number of times a 1 is rolled:<input type=text name="num1" value="0"><br>
	Number of times a 6 is rolled:<input type=text name="num6" value="0"><br>
	Number of Rolls:<input type=text name="numrolls" value="0"><br>

		


  </body>
</html>



:^: B)
Was This Post Helpful? 0
  • +
  • -

#9 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5365
  • View blog
  • Posts: 27,328
  • Joined: 10-May 07

Re: Java Dice Rolling Program! -- Quick Question!

Posted 22 March 2009 - 10:35 PM

View Postcharlsy, on 22 Mar, 2009 - 11:25 PM, said:

For any of you that were interested, I figured it out!!


That's awesome news!

Way to go!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1