java while循环计数错误

时间:2016-08-27 00:46:17

标签: java while-loop

我正在尝试使用ProgrammingByDoing(https://programmingbydoing.com/a/baby-nim.html)的教程在Java中创建Nim游戏。在第一次尝试中,我并不那么担心错误检查或过零,但似乎只要三组/桩中的两个达到零,那么游戏结束。但是,我希望在比赛结束前所有三个桩都达到或超过零。

我的代码是:

import java.util.Scanner;

public class BabyNim
{
    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

            int pile1 = 10;
            int pile2 = 10;
            int pile3 = 10;
            String pileChoice = "";
            int amount; 

            System.out.print("A: " + pile1 + "\t" + "B: " + pile2 + "\t" + "C: " + pile3 + "\n" );
            System.out.print("Choose a pile: ");
            pileChoice = keyboard.next();
            System.out.print("How many to remove from pile " + pileChoice + ": ");
            amount = keyboard.nextInt();
            System.out.println("");

            while ( pile1 > 0 && pile2 > 0 && pile3 > 0 )
            {
                if ( pileChoice.equals("A") ) {
                    pile1 -= amount;
                } else if ( pileChoice.equals("B") ) {
                    pile2 -= amount;
                } else {
                    pile3 -= amount;
                }

                System.out.print("A: " + pile1 + "\t" + "B: " + pile2 + "\t" + "C: " + pile3 + "\n" );
                System.out.print("Choose a pile: ");
                pileChoice = keyboard.next();
                System.out.print("How many to remove from pile " + pileChoice + ": ");
                amount = keyboard.nextInt();
                System.out.println("");
            }

            System.out.println("");
            System.out.println("All piles are empty. Good job!");

        }
    }

我的问题是我得到的结果如下:

A: 10   B: 10   C: 10

Choose a pile: B

How many to remove from pile B: 8

A: 10   B: 2    C: 10

Choose a pile: C

How many to remove from pile C: 9

A: 10   B: 2    C: 1

Choose a pile: A

How many to remove from pile A: 8

A: 2    B: 2    C: 1

Choose a pile: C

How many to remove from pile C: 1

A: 2    B: 2    C: 0

Choose a pile: A

How many to remove from pile A: 1


All piles are empty. Good job!

4 个答案:

答案 0 :(得分:2)

您使用了while ( pile1 > 0 && pile2 > 0 && pile3 > 0 )。根据{{​​1}}运算符,如果其中任何一个AND,则整个结果变为false。这就是为什么循环退出,如果它们中的任何一个是false

因此,只有当所有桩都是空的时候才能退出循环,你应该使用

<=0。根据{{​​1}}运算符,如果它们都是while( pile1>0 || pile2>0 || pile3>0),则只有整个结果变为OR(您需要)。即使其中一个是false,整个结果也会是false

希望您了解运营商。

<强>更新

我已编辑您的代码以获取您在链接中显示的输出。它工作正常。

true

而不是true使用import java.util.Scanner; public class BabyNim { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int pile1 = 10; int pile2 = 10; int pile3 = 10; String pileChoice = ""; int amount; do { System.out.print("A: " + pile1 + "\t" + "B: " + pile2 + "\t" + "C: " + pile3 + "\n" ); System.out.print("Choose a pile: "); pileChoice = keyboard.next(); System.out.print("How many to remove from pile " + pileChoice + ": "); amount = keyboard.nextInt(); System.out.println(""); if ( pileChoice.equals("A") ) { pile1 -= amount; } else if ( pileChoice.equals("B") ) { pile2 -= amount; } else { pile3 -= amount; } }while ( pile1 > 0 || pile2 > 0 || pile3 > 0 ); System.out.print("A: " + pile1 + "\t" + "B: " + pile2 + "\t" + "C: " + pile3 + "\n" ); System.out.println("All piles are empty. Good job!"); } } ,因为它是基于退出的循环。您也可以使用while(),但do{}while();很方便,因为我们不想检查进入循环的初始条件。

你不需要在循环的内部和外部写2次询问用户的输入。只需在进入循环后写一次。

希望它有所帮助。

答案 1 :(得分:1)

只要任何桩都是空的,你的循环就会结束。相反,它需要在所有堆都为空时结束。换句话说,它应该继续,而至少有一个桩是空的。

应该是:

while (pile1 > 0 || pile2 > 0 || pile3 > 0)

或许这会更有意义:

while (pile1 + pile2 + pile3 > 0)

此外,在使用do..while循环进行检查之前,您应该要求输入:

import java.util.Scanner;
public class BabyNim {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int pile1 = 10, pile2 = 10, pile3 = 10;

        do {
            System.out.printf("A: %d\tB: %d\tC: %d%n", pile1, pile2, pile3);
            System.out.print("Choose a pile: ");
            String pileChoice = in.next();
            System.out.printf("How many to remove from pile %s: ", pileChoice);
            int amount = in.nextInt();
            System.out.println();

            if (pileChoice.equals("A")) {
                pile1 -= amount;
            } else if (pileChoice.equals("B")) {
                pile2 -= amount;
            } else {
                pile3 -= amount;
            }
        } while (pile1 + pile2 + pile3 > 0);

        System.out.println("\nAll piles are empty. Good job!");
    }
}

答案 2 :(得分:1)

这是循环条件的问题,其中说:

   while ( pile1 > 0 && pile2 > 0 && pile3 > 0 )

如果任何一个堆是0,那么不要进入while循环,所以你的代码应该是这样的:

    while ( pile1 > 0 || pile2 > 0 || pile3 > 0 )

你还必须添加一些支票,否则当你从桩中减去金额时你输入的金额应该小于当前的桩尺寸,否则桩的尺寸将是负的。

例如。

if ( pileChoice.equals("A") && amount <= pile1) {
                pile1 -= amount;
         }

答案 3 :(得分:0)

  

我真的很开心做这个节目:D,好像我不是唯一一个

这是我的建议 1-通过do ... while在循环之前删除冗余代码,并查看条件
2-用if替换最后的else(你期待C,但是除了A B之外的任何字母被接受为C) 3-因为你没有检查剩余部分,你最终会得到负数。

import java.util.Scanner;

public class BabyNim
{
    public static void main(String[] args) {

            Scanner keyboard = new Scanner(System.in);
            int pile1 = 10;
            int pile2 = 10;
            int pile3 = 10;
            String pileChoice = "";
            int amount; 
            do{
                // the incorrect inputs (other letters or values greater the amount) are ignored by the loop  
                System.out.print("A: " + pile1 + "\t" + "B: " + pile2 + "\t" + "C: " + pile3 + "\n" );
                System.out.print("Choose a pile: ");
                pileChoice = keyboard.next();
                System.out.print("How many to remove from pile " + pileChoice + ": ");
                amount = Math.abs(keyboard.nextInt());// only positive numbers
                System.out.println("");                

                if ( pileChoice.equals("A") && pile1 >= amount) {
                    pile1 -= amount;
                } 
                if ( pileChoice.equals("B") && pile2 >= amount) {
                    pile2 -= amount;
                }
                if ( pileChoice.equals("C") && pile3 >= amount){
                    pile3 -= amount;
                }
            }while ( pile1 > 0 || pile2 > 0 || pile3 > 0 );

            System.out.println("All piles are empty. Good job!");

        }
    }
相关问题