60 683
Assignments Done
97,8%
Successfully Done
In April 2018

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

Question #12958
how to create a simple game using java?
Expert's answer
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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions