2014-10-11 49 views
-1

我正在製作Craps java程序,並且在添加「您想再次播放」語句時遇到了一些麻煩。如果你能幫助我,那將不勝感激!此外,我必須計算用戶贏/輸的遊戲數量不正常。如果您發現問題,請告訴我!需要幫助在我的程序中添加「您想再次播放」語句

import java.util.Scanner; 
public class Lab5 { 
    static int dice2; 
    public static void main(String[] args) { 
     //variables 
     int dice1; 
     int dice2; 
     int numWins = 0; 
     int numLosses = 0; 

     //Call the welcome method 
     welcome(); 

     //fetch random numbers 

     /* 
     * ************************************************************** 
     *welcome method 
     *welcome user 
     *no parameters 
     *no return 
     **************************************************************** 
     */ 
    } 
    public static void welcome() { 
     System.out.println("Welcome to a Lucky (for me) Dice Game! \nFEELING LUCKY?!? Hope you brought lots of CASH!");{ 
     } 

     int die1 = (int) (Math.random()*6 + 1); 
     int die2 = (int) (Math.random()*6 + 1); 
     int dice = die1 + die2; 
     System.out.println("Roll: total = " + dice); 
     int numWins = 0; 
     int numLosses = 0; 
     if (dice == 7 || dice == 11){ 
      System.out.println("Woah!!! With a: "+dice+ " You WIN!!!!!!!!"); 
      numWins++; 
     } 
     else if (dice == 2 || dice == 3 || dice == 12){ 
      System.out.println("Sorry, with a "+dice+" You lose:("); 
      numLosses++; 
     } 

     while (dice != 0){ 
      int die3 = (int) (Math.random()*6 + 1); 
      int die4 = (int) (Math.random()*6 + 1); 
      int dice2 = die3 + die4; 
      System.out.println("Roll: total = "+dice2); 

      if (dice2 == 2|| dice2 == 3 || dice2 == 12){ 
       System.out.println("Sorry, with a "+dice2+" You lose:("); 
       numLosses++; 
       dice = 0; 
      } 
      else if (dice2 == 7 || dice2 == 11){ 
       System.out.println("Woah!!! With a: "+dice2+ " You WIN!!!!!!!!"); 
       numWins++; 
       dice = 0; 
      } 
      { 
       System.out.println("So far you have won " + numWins + 
       " times and lost " + numLosses + " times, "); 

       { 


       } 
      } 
     } 
}} 

這是我的輸出,當我運行它:

Welcome to a Lucky (for me) Dice Game! 
FEELING LUCKY?!? Hope you brought lots of CASH! 
Roll: total = 2 
Sorry, with a 2 You lose:(
Roll: total = 8 
So far you have won 0 times and lost 1 times, 
Roll: total = 10 
So far you have won 0 times and lost 1 times, 
Roll: total = 8 
So far you have won 0 times and lost 1 times, 
Roll: total = 3 
Sorry, with a 3 You lose:(
So far you have won 0 times and lost 2 times, 

計數器應止贏後聲明或丟失。我該如何解決?

+0

首先,你需要了解如何格式化,尤其是縮進代碼以及它是當編譯器並不重要,關鍵是我們的代碼的理解,現在你以後瞭解它的Java語言的一部分。編程語言的大部分結構都是爲了讓人們能夠理解它。所以請嘗試修復您的代碼。縮進所有塊4個空格,而不是隨機。同一塊上的所有代碼都應該縮進一樣。如果您認真對待您的問題,請認真溝通您的代碼。 – 2014-10-11 01:29:59

+0

對不起 - 這是我第一次使用Java,所以我還在學習。 – user3587461 2014-10-11 01:32:07

+0

這一切都可以,但請立即修復您的代碼。此外,連續不超過一個空白行。 – 2014-10-11 01:34:10

回答

0

要重複某些操作,可以使用循環,例如while循環或do-while循環,如果您不確定會循環多少次。要獲得用戶輸入,請使用已經導入的掃描儀對象。在do-while循環結構是這樣的......

do { 
    // code to do inside of the loop 
} while (somethingIsTrue); 

您將需要使用一些定點變量的循環變化,然後測試,而布爾檢查的內部。它可能是一個字符串,在這種情況下,你可以在你的while布爾檢查中使用String equals(...)equalsIgnoreCase(...)方法。

所以考慮使用System.out.print(...)在代碼塊末尾提示輸入,使用掃描器獲取輸入,然後在布爾測試中測試該輸入。

-1
import java.util.NoSuchElementException; 
import java.util.Scanner; 

public class Lab5 
{ 
    static int dice2; 

    public static void main(String[] args) 
    { 
     //variables 
     int dice1; 
     int dice2; 
     int numWins = 0; 
     int numLosses = 0; 

     //Call the welcome method 
     //welcome(); 

     //fetch random numbers 

     /* 
     * ************************************************************** 
     *welcome method 
     *welcome user 
     *no parameters 
     *no return 
     **************************************************************** 
     */ 


     while(true) 
     { 
      welcome(); 
     } 

    } 

    public static void welcome() 
    { 
     System.out.println("Welcome to a Lucky (for me) Dice Game! \nFEELING LUCKY?!? Hope you brought lots of CASH!"); 

     int die1 = (int) (Math.random()*6 + 1); 
     int die2 = (int) (Math.random()*6 + 1); 
     int dice = die1 + die2; 

     System.out.println("Roll: total = " + dice); 

     int numWins = 0; 
     int numLosses = 0; 

     if (dice == 7 || dice == 11) 
     { 
      System.out.println("Woah!!! With a: "+dice+ " You WIN!!!!!!!!"); 
      numWins++; 
     } 
     else if (dice == 2 || dice == 3 || dice == 12) 
     { 
      System.out.println("Sorry, with a "+dice+" You lose:("); 
      numLosses++; 
     } 

     while (dice != 0) 
     { 
      int die3 = (int) (Math.random()*6 + 1); 
      int die4 = (int) (Math.random()*6 + 1); 

      int dice2 = die3 + die4; 

      System.out.println("Roll: total = "+dice2); 

      if (dice2 == 2|| dice2 == 3 || dice2 == 12) 
      { 
       System.out.println("Sorry, with a "+dice2+" You lose:("); 
       numLosses++; 
       dice = 0; 
      } 
      else if (dice2 == 7 || dice2 == 11) 
      { 
       System.out.println("Woah!!! With a: "+dice2+ " You WIN!!!!!!!!"); 
       numWins++; 
       dice = 0; 
      } 
      { 
       System.out.println("So far you have won " + numWins + 
       " times and lost " + numLosses + " times, "); 

       { 


       } 
      } 
     } 

     System.out.printf("\n\nWould you like to play again? "); 

     Scanner scanner = new Scanner(System.in); 

     String uinput = scanner.nextLine(); 

     if(uinput.isEmpty() || 
      uinput.equals("n") || uinput.equals("no")) 
     { 
      scanner.close(); 

      System.out.println("Goodbye."); 

      return; 
     } 

     System.out.println(); 
}} 
+0

你既是給OP提供了一個答案,也給了他一個錯誤的答案。請通過建議他自己修復它的方法來解決這個問題。 – 2014-10-11 02:20:22

+0

感謝您刪除那些令人反感的評論。 – 2014-10-11 02:39:59