2016-11-03 24 views
0

我想要得到一個骰子滾動發生,我有一些困難添加一個循環某處,所以程序不會退出一個滾動後。我想詢問用戶是否要滾動,並通過說「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 
+0

您可能想要刪除代碼中的標題註釋。另外一個'do-while'循環解決了你的問題。 –

+5

關於這個問題有一個很好的教程:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html – bradimus

回答

0

您必須閱讀下面的表達式後輸入端:

System.out.println(" Do you want to roll the dice? "); 

要接收用戶輸入致電:input.nextLine();。此後循環,而輸入是"y"。如果用戶輸入不等於"y",則while-loop終止。

while(condition)循環,只要條件true

而特定條件爲真while語句連續執行語句塊excuted。 The while and do-while Statements

例如考慮這個代碼:

public static void main(String[] args) { 
    Random dice = new Random(); 
    Scanner input = new Scanner(System.in); 
    System.out.println("Do you want to roll the dice? (y: yes/q: to quit)"); 
    String answer = input.nextLine(); // reading the input 

    // we check if the answer is equals to "y" to execute the loop, 
    // if the answer is not equals to "y" the loop is not executed 
    while ("y".equals(answer)) { 
     System.out.println("Dice Roller"); 
     System.out.println("How many faces does the dice have?"); 
     int faces = input.nextInt(); 
     int result = dice.nextInt(faces) + 1; 
     input.nextLine(); // to read the newline character (*) 
     System.out.println("The dice rolled a " + result); 
     System.out.println("Do you want to roll the dice? (y: yes/q: to quit)"); 
     answer = input.nextLine(); 
    } 
} 

要了解更多有關而和do-while機制,請訪問該toturial

(*)要了解使用nextLine的()呼叫nextInt()後請訪問Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

+0

這個答案有很多要補充的。什麼是'while'?由於OP似乎在學習JAVA。知道這個問題的當前答案是一個整數,這可能是一個問題,使用'nextLine()' – AxelH

+0

我想在System.out.println(「你想擲骰子?」)之後添加一個部分? ;有一個while循環,通過說「y」開始程序,並通過說「n」 –

+0

結束它,請考慮一下解釋do-while循環,因爲OP似乎是一個初學者。 –

0

一個do-while是一個很好的選擇。另一種做法是使用帶開關的while循環或最好是IF/ELSE IF語句。也許像下面的東西。這只是一個建議。

boolean checkForExit = false; 
while(checkForExit != true) { //while checkForExit does not equal true (therefore false) continue.. 

    System.out.println("Do you want to roll the dice?"); 
    String answer = input.next(); //get char input from user. 

    if(answer.toLowerCase().equals("y")) { //if 'y' then do this 
    //ask user for number of faces on dice and roll 
    } else { // otherwise end the program 
     //set isTrue to true to exit while loop. ends program 
     isTrue = true; 
    } 
} 
+0

請考慮使用input.nextChar()。否則,使用equals()來複制術語。你的屬性isTrue也是一個變形,請更改它的名字。此外,如果輸入是除'y'或'n'之外的輸入,則程序將循環,因爲您正在使用其他方法,而不是其他方法 –

+0

因此,我會在哪裏插入boolean和inital while? –

+0

你會在主要方法中使用這個 –