2014-02-16 60 views
0

我在我的Coin.java類中設置了一個for循環,以便由正在運行的程序CoinTossing.java調用...由於某些原因,for循環被忽略,我不確定爲什麼。任何提示將非常感謝!謝謝。For循環嵌套在裏面do ... while not functioning

Coin.java(凡方法!)

import static java.lang.System.exit; 
import static java.lang.System.in; 
import static java.lang.System.out; 
import java.util.Random; 
import java.util.Scanner; 

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/** 
* 
* @author Kiora Lizet 
*/ 
public class Coin 

{ 

//Enumeration with representation of coin state. 
private enum Status {HEADS, TAILS}; 

//Constants that represent flip outcome 
private static final int HEADSCOUNT = 0; 
private static final int TAILSCOUNT = 1; 

//Random number function. 
private static final Random randomNumbers = new Random(); 

//Method to flip a coin and return the output. 

Status coinState; //Contains the orientation of the coin, heads or tails. 

int numberOfHeads = 0; //Couinter for number of heads. 
int numberOfTails = 0; //Counter for number of tails. 
public int flip() 
{ 
    int outcome = randomNumbers.nextInt(2); 
    return outcome; 
} 

//Method to ask the user for their decisions on coin flips. 
public int decision() 
{ 

    int Flips = 1; 
    Scanner input = new Scanner(in); 
    int flipDecision; 
    do 
    { 
     out.print("Enter 1 to flip the coin. Enter 2 to select the amount " 
       + "of flips. (0 or below to stop): "); 
     flipDecision = input.nextInt(); //Grabs decision 

     if(flipDecision <= 0)  //Exit the program. 
      exit(0); 

     if(flipDecision >= 3)  //Reruns loop. 
     { 
      out.print("Enter a correct decision.\n"); 
     } 
    }while (flipDecision >= 3); 

    if(flipDecision == 2) 
    { 
     out.print("Please insert the number of flips desired: "); 
     Flips = input.nextInt(); 
    } 
    return Flips; 
}   

//Method to store coins flipped. 
public void coinsFlipped(int Flips) 
{ 
for(int i = 0; i == Flips; ++i) 
    { 
     int outcome = flip(); 

     switch (outcome) 
     { 
      case HEADSCOUNT: //Get a counter for one heads value. 
       out.print("Heads!"); 
       coinState = Status.HEADS; 
       break; 
      case TAILSCOUNT: //Get a counter for one tails value. 
       out.print("Tails!"); 
       coinState = Status.TAILS; 
       break; 
     } 

     if(coinState == Status.HEADS) 
     { 
      ++numberOfHeads; 
      out.printf("/nThere are currently %d repetitions of Heads.", 
        + numberOfHeads); 
     } 
     if(coinState == Status.TAILS) 
     { 
      ++numberOfTails; 
      out.printf("\nThere are currently %d repetitions of Tails.", 
        + numberOfTails); 
     } 
    } 
} 

}

CoinTossing.java(其中這些方法都彙集!)

import static java.lang.System.exit; 
import static java.lang.System.in; 
import static java.lang.System.out; 
import java.util.Scanner; 

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/** 
* 
* @author Kiora Lizet 
*/ 
public class CoinTossing 
{ 

    //Enumeration with representation of coin state. 
    private enum Status {HEADS, TAILS}; 

    //Constants that represent flip outcome 
    private static final int HEADSCOUNT = 0; 
    private static final int TAILSCOUNT = 1; 

    public static void main (String[] args) 
    { 

     Scanner input = new Scanner(in); 
     Coin Token = new Coin(); //For functions, object declaration. 

     int continuation; //Variable to decide for more flips. 


     do 
     {    
     int Flips = Token.decision(); 
     Token.coinsFlipped(Flips); 
     //Variable declared for amount of flips. 
     out.printf("Flips: %d", Flips); 

     out.print("\nWould you like to flip more coins? \nInsert 1 to do so." 
       + " Enter any other number to exit program.\n"); 
     continuation = input.nextInt(); 
     }while(continuation == 1); 
    } 
} 
+0

這是一個:爲(INT I = 0;我==翻轉; ++我)?這是行不通的。 – Tony

+0

'for(int i = 0; i == Flips; ++ i)'你確定條件正確嗎?我應該在每次迭代中總是等於'Flips'嗎? – Pshemo

+0

你確定循環在while循環中嗎? – Maroun

回答

4

for循環終止條件是越野車。這:

for(int i = 0; i == Flips; ++i) 

將終止於第一次迭代只,如果Flips的價值是什麼,但0

for(int i = 0; i <= Flips; ++i) 
+0

但當然!現在看起來有多明顯,當然循環不會迭代。非常感謝你! – Zimbler

0

forfor(int i = 0; i == Flips; ++i) 結束時翻轉不爲零:它應該被替換。

如果要求進行零翻轉或負翻轉,大概循環應該在第一次迭代之前終止,以便不發生翻轉。否則,循環應執行翻轉次數。

可以通過讓你的條件爲環如下做到這一點:
for(int i = 0; i < Flips ; ++i)