2015-07-10 80 views
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 
+1

'if'條件不是循環。這是一個分支。事實上,你的代碼根本不包含任何循環。 – Codebender

+0

你想讓用戶從鍵盤輸入什麼? – yanana

回答

0

這應該做的伎倆

System.out.print("Would you like to continue? Y/N: "); 
String UserResp = scanner.nextLine(); 

把代碼的其餘部分在一個while循環。 並且在開頭定義的布爾值對於第一個循環爲true,然後將UserResp與Y或N進行比較以設置布爾值。

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; 
     char ans='y'; 
     while(ans=='Y'||ans=='y'){ //loop while user says Y 
      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?(Y/N) "); 
        ans=keyboard.next().charAt(0); 
       } 
      else{ 
       System.out.println("You are out of points. Would you like to restart?(Y/N) "); 
       ans=keyboard.next().charAt(0); 
      } 

     } 
     keyboard.close(); //Don't forget to close sccaner. 
    } // end Main 

    public static int genRanInRange(int start, int end) 
    { 
     return (int)(Math.random()*(end-start+1.0)) + start; 
    } // end genRanInRange 

} // end Dice Roller 

我改變你的類的市值從diceRollerDiceRoller。

類名應該總是有它的第一個字母用大寫'

一兩件事:

不要忘記關閉掃描儀。

相關問題