0
我无法确定如何提示允许用户输入数字或字符来启动或结束循环的问题。如何使用用户输入启动和结束循环?
以下是代码
import java.util.Scanner;
public class diceRoller{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
int score;
int sum=0;
int roll1 = genRanInRange(1, 6);
int roll2 = genRanInRange(1, 6);
int roll3 = genRanInRange(1, 6);
System.out.println("Your rolls were: " + " " + roll1 + " " + roll2 + " " + roll3);
if (roll1 == roll2 || roll2 == roll3 || roll1 == roll3){ // start of first loop
System.out.println("You had 2 matching rolls! You gain 50 points.");
score = 50;
sum += score;
if (roll1 == roll2 && roll2 == roll3 && roll1 == roll3){ // nested loop
int rollSum = roll1 + roll2 + roll3;
if (rollSum == 18){ // nested loop 2
System.out.println("You had 3 matching 6's! You gain 500 points.");
score = 500;
sum += score;
} // end nested loop 2
} // end nested loop
} // end of first loop
else {
System.out.println("Sorry, you had 0 matching die.");
score = 1;
sum -= score;
}
System.out.println("Your score was: " + sum);
if (sum > 0){
System.out.println("Would you like to continue?");
}
else{
System.out.println("You are out of points. Would you like to restart?");
}
} // end Main
public static int genRanInRange(int start, int end)
{
return (int)(Math.random()*(end-start+1.0)) + start;
} // end genRanInRange
} // end Dice Roller
'if'条件不是循环。这是一个分支。事实上,你的代码根本不包含任何循环。 – Codebender
你想让用户从键盘输入什么? – yanana