我想要得到一個骰子滾動發生,我有一些困難添加一個循環某處,所以程序不會退出一個滾動後。我想詢問用戶是否要滾動,並通過說「y」。我想通過詢問用戶同樣的問題,以結束該程序,但它與「N」我想添加一個循環到我的骰子滾動程序
/*
Natasha Shorrock
Assignmnt A6
11/07/16
*/
package diceroller;
import java.util.Random;
import java.util.Scanner;
public class DiceRoller {
public static void main(String []args) {
System.out.println(" Do you want to roll the dice? ");
Random dice = new Random();
Scanner input = new Scanner(System.in);
int faces;
int result;
System.out.println("Dice Roller\n");
System.out.println("How many faces does the dice have?");
faces = input.nextInt();
result = dice.nextInt(faces) + 1;
System.out.println("\nThe dice rolled a " + result);
}//Dice Roller
}//class DiceRoller
您可能想要刪除代碼中的標題註釋。另外一個'do-while'循環解決了你的問題。 –
關於這個問題有一個很好的教程:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html – bradimus