Question

I've set a for loop in my Coin.java class to be called by the running program CoinTossing.java... For some reason, the for loop is ignored, and I'm unsure why. Any tips would be greatly appreciated! Thank you.

Coin.java (Where the methods are!)

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 (Where the methods are brought together!)

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);
    }
}
Était-ce utile?

La solution

Your for loop terminating condition is buggy. This:

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

will terminate on first iteration only, if the value of Flips is anything but 0. It should be replaced with:

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

Autres conseils

Your for loop for(int i = 0; i == Flips; ++i) terminates whenever Flips is not zero.

If asked to make zero flips or negative flips, presumably the loop should terminate before the first iteration so that no flips occur. Otherwise, the loop should execute Flips times.

You can do this by having the condition on your for loop as follows:
for(int i = 0; i < Flips ; ++i)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top