Answer to Question #12958 in Java | JSP | JSF for jayson

Question #12958
how to create a simple game using java?
1
Expert's answer
2012-08-16T10:43:16-0400
Here is an example of simple dice game. Player could interact with
the
program using console.
import java.util.*;

public class q12958
{
protected static Scanner sc = new Scanner(System.in);

public static
void main(String[] arg) {
int score = 100;
int nr1, nr2;
while (score
> 0) {
System.out.println("Your score is: $" +
score);
System.out
.println("Would you like to throw the dice?\n1 - yes\n2
- no");
int dice = sc.nextInt();
if (dice == 1) {
nr1 = (int)
(Math.random() * 6 + 1);
nr2 = (int) (Math.random() * 6 +
1);
System.out.println("You've got [" + nr1 + ", " + nr2 + "]");
if (nr1
== nr2 || Math.abs(nr1 - nr2) == 1)
score += nr1 + nr2;
else
score -=
nr1 + nr2;
} else if (dice == 2) {
System.exit(0);
}
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS