【问题标题】:For loop nested inside do... while not functioningFor 循环嵌套在 do... 内,但不起作用
【发布时间】:2014-02-16 07:46:59
【问题描述】:

我在我的 Coin.java 类中设置了一个 for 循环,由正在运行的程序 CoinTossing.java 调用...由于某种原因,for 循环被忽略了,我不确定为什么。任何提示将非常感谢!谢谢。

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);
    }
}

【问题讨论】:

  • 这是那个:for(int i = 0; i == Flips; ++i) 吗?那是行不通的。
  • for(int i = 0; i == Flips; ++i) 你确定条件正确吗? i 是否应该在每次迭代中始终等于 Flips
  • 您确定该循环在while 循环内吗?

标签: java for-loop enums switch-statement do-while


【解决方案1】:

您的for 循环终止条件有问题。这个:

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

仅在第一次迭代时终止,如果Flips 的值不是0。它应该替换为:

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

【讨论】:

  • 当然!这现在看起来多么明显,当然循环不会迭代。非常感谢!
【解决方案2】:

你的for 循环 for(int i = 0; i == Flips; ++i) 只要 Flips 不为零,就会终止。

如果要求进行零翻转或负翻转,大概循环应该在第一次迭代之前终止,这样就不会发生翻转。否则,循环应该执行翻转次数。

您可以通过在 for 循环中设置如下条件来做到这一点:
for(int i = 0; i &lt; Flips ; ++i)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 2018-12-03
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多