2016-09-15 118 views
0

我想寫一個簡單的硬幣翻轉程序,並想知道我是否可以得到一些幫助。我對Java相當陌生,只是試圖詢問用戶他們想拋硬幣多少次。這裏是我的代碼:Java硬幣翻轉程序

package cointossing; 
import java.util.Random; 
import java.util.Scanner; 
import static java.lang.System.in; 
import static java.lang.System.out; 
/** 
* Coin tossing class to simulate the flip of a coin 
* with two sides; Heads and Tails. 
* 
* @author Alex Chapman ID: 
* 
*/ 
public class CoinTossing 
{  
    public static String sideUp; 
    public static int number; 

     public void run() 
    { 
     try(Scanner input = new Scanner(in)) 
     { 
      out.print("Enter how many times you would like to flip the coin"); 
      out.print("if you enter 0 the program quits"); 
      int number = input.nextInt();  
     } 
    } 


    private static void coin() 
    { 
     Random rand = new Random(); 
     int sideup = rand.nextInt(2); 
     if (sideup == 0) 
     { 
      sideUp = "heads"; 
     } 
     else 
     { 
      sideUp = "tails"; 
     } 
    } 

    public static String getsideup() 
    { 
     out.println(sideUp); 
     return sideUp; 
    } 



    public static void main(String[] args) 
    { 
     int hcount = 0; 
     int tcount = 0; 
     for (int i = 1; i <= number; i++) 
     { 
      coin(); 
      if (getsideup().equals("heads")) 
      { 
       hcount++; 
      } 
      else 
      { 
       tcount++; 
      } 
     } 
     out.println("total heads = " + hcount + " total tails = " + tcount); 
    } 
} 

但是當我運行程序它跳過詢問用戶任何東西,只是顯示爲0的,因爲沒有次數翻轉硬幣...我覺得右邊IM軌道,但即時通訊卡...任何幫助將不勝感激..

編輯:

所以在學習的興趣,我決定改變我的計劃,改變硬幣枚舉並有計劃返回該枚舉值..我也改變了用戶輸入菜單樣式,但這是通過跟隨在一個巴恩斯和貴族書,我購買了一段時間後幫助...我瘦ki來到了一個奇怪的十字路口......我想要基本上將兩個程序融合在一起,這樣所有新的工作,只要返回枚舉值,並且仍然存在,但刪除「菜單」方面並將其替換爲用戶可以輸入他們想要從前一個程序中做多少翻轉的能力。這裏是我寫的新程序:

import java.util.*; 

public class CoinTossing 
{ 
    private enum Coin { HEADS, TAILS }; 

    private static final Random randomNumbers = new Random(); 

    private static final int HEAD = 1; 
    private static final int TAIL = 2; 

    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 

     int choice; 
     int toss = 0; 
     int tosses = 0; 
     int frontflip = 0; 
     int backflip = 0; 

     Coin gameStatus; 

     System.out.println("Welcome to the Coin Toss Program.\n"); 
     System.out.println("Choose from the menu what you want to do."); 
     System.out.print("1. Toss the coin\n2. Quit program\n"); 
     choice = input.nextInt(); 

     while (choice != 0) 
     { 
      if (choice == 1) 
      { 
       int CoinTossed = Flip(); 

       switch (CoinTossed) 
       { 
           //added tosses to switch statement to make the counter work perfect. 
       case HEAD: 
        gameStatus = Coin.HEADS; 
        tosses++; // add amount of tosses 
        break; 
       default: // changed case TAIL to default. Easy and works. 
        gameStatus = Coin.TAILS; 
        tosses++; // add amount of tosses 
        break; 
       } 

       if (gameStatus == Coin.HEADS) 
       { 
        frontflip++; //Add amount of heads 
       } 
       else // gameStatus == TAILS 
        backflip++; //Add amount of tails  
      } 

      // A try to make an real exit out of a program 

      if (choice == 2) 
      { 
       EndProgram(frontflip, backflip, tosses); 
      } 

      System.out.println("\nChoose from the menu what you want to do."); 
      System.out.print("1. Toss the coin\n2. Quit program\n"); 
      choice = input.nextInt(); 
     } 
    } 

    //Toss the coin to determine 1 or 2. 
    public static int Flip() 
    { 
     int toss; 

     toss = 1 + randomNumbers.nextInt(2); 

     if (toss == 1) 
     { 
      System.out.println("You toss the coin and it lands on head!"); 
     } 
     else 
     { 
      System.out.println("You toss the coin and it lands on tail!"); 
     } 
     return toss; 
    } 

    public static void EndProgram(int frontflip, int backflip, int tosses) 
    { 
     System.out.printf("You have tossed %d times.\n", tosses); 
     System.out.printf("Of all those tosses, %d landed on heads, and %d on tails.\n", frontflip, backflip); 
     System.exit(0); 
    } 
} 

我正在考慮創建一個新變量並讓用戶設置擲骰子數。然後加入化合物while循環檢查,像這樣

while(choice != 0 && numTosses !=0) 

,然後減少計數,我得檢查計數,一旦它儘可能達到0打印結果多少頭和多少的尾巴,然後提示用戶如果他們想再次玩這個遊戲..我真的不知道爲什麼我試圖做到這一點,但是對於知識方面,所以如果你不想幫我理解一個混蛋。

+0

您可以發佈完整的代碼而不是兩個斷開連接的段? –

+1

嘗試在'int number = input.nextInt();'行後面添加'input.nextLine();'。 –

+0

如果在顯示說明的地方使用'println'而不是'print',會發生什麼情況? –

回答

0

更改您的主要方法,如:

public static void main(String[] args) 
    { 
     int hcount = 0; 
     int tcount = 0; 
     Scanner sc = new Scanner(System.in); 
     out.println("How many coin flips do you want?"); 
     int number = sc.nextInt(); 
     for (int i = 1; i <= number; i++) 
     { 
      coin(); 
      if (getsideup().equals("heads")) 
      { 
       hcount++; 
      } 
      else 
      { 
       tcount++; 
      } 
     } 
     out.println("total heads = " + hcount + " total tails = " + tcount); 
    } 
+0

改變主要這個工作! –

+0

這很好,如果你喜歡它,請閱讀http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work,謝謝! – Shadov

+1

對於Blankets做錯了什麼的一些解釋會比僅僅一大堆代碼更好。這樣,毛毯實際上可以學到一些東西。 –

1

你是不是叫你主要的run()。

您需要在撥打for (int i = 1; i <= number; i++)之前添加run()

您還需要再次檢查您的變量,它看起來像使用sideUp作爲int和字符串。在設置值"heads""tails"時,嘗試在coin()調用中添加this.sideUp,或者將您的int sideUp變量重命名以避免混淆。