【问题标题】:Java Integrated Application about Loops and Arrays关于循环和数组的 Java 集成应用程序
【发布时间】:2014-07-27 13:54:31
【问题描述】:

问题如下:

在 Nim 游戏中,两名玩家交替从一堆弹珠中取出。在 每一步,玩家选择要拿多少个弹珠。玩家 必须至少取一个但最多一半的大理石。然后另一个 玩家轮到。拿走最后一个弹子的玩家输了。

编写一个程序,让两个玩家互相对战。这 程序首先提示玩家输入初始大小(范围:10~100 包括)那堆弹珠。然后,两名玩家轮流输入 要采取的弹珠数量。该程序应打印的数量 每转一圈后留下的弹珠。当剩下一颗弹子时游戏停止 桩。应打印中奖信息以表明谁是中奖者 游戏结束后。你可以假设玩家总是输入一个 数字在正确的范围内。完成程序后,您可以 和你的邻居一起玩这个游戏。


示例如下:

Initial no. of marbles [10 ~ 100]: 15
Player1 [1 ~ 7]: 6
Remaining no. of marbles: 9
Player2 [1 ~ 4]: 4
Remaining no. of marbles: 5
Player1 [1 ~ 2]: 2
Remaining no. of marbles: 3
Player2 [1 ~ 1]: 1
Remaining no. of marbles: 2
Player1 [1 ~ 1]: 1
Remaining no. of marbles: 1
Player2 takes the last marble.
Player1 wins!

这是我的个人尝试(未完成):

import java.util.Scanner;

public class WhoTakesTheLastMarble {

public static void main(String[] args) {

    Scanner in = new Scanner (System.in);       
    System.out.print("Intial no. of marbles [10 ~ 100]: ");
    int n1 = in.nextInt();  
    int[] x = new int[91];
    for (int i = 10; i<=100; i++) {
        x[i] = in.nextInt();
    }

    System.out.print("Plyaer1 [1 ~ " +(n1/2) +"]:");
    int n2 = in.nextInt();

    System.out.println("Remaining no. of marbles: " +(n1-n2));
    int n3 = in.nextInt();

    System.out.print("Player2 [1 ~ " +(n3/2) +"]: ");
    int n4 = in.nextInt();

    System.out.println("Remaining no. of marbles: " +(n3-n4));

}       

}

我面临的问题是如何将 for-loop/while-loop 应用到整个事情中,以便它们可以一次又一次地重复,直到 (剩余弹珠数量:1)?

谁能花五到十分钟帮我解决这个问题并教我你解决这个问题的方法吗?非常感谢:)

【问题讨论】:

    标签: java arrays for-loop


    【解决方案1】:

    您可以使用while-loop 来查看是否剩下弹珠。您可能还想存储活动的玩家,这样您就可以在两个玩家回合中使用相同的代码。

    import java.util.Scanner;
    
    /**
     * Created by TheConstrcutor for http://stackoverflow.com/q/24981670/1266906
     */
    public class WhoTakesTheLastMarble {
        public static void main(String[] args) {
    
            Scanner in = new Scanner(System.in);
            System.out.print("Initial no. of marbles [10 ~ 100]: ");
            int numberOfMarbles = in.nextInt();
    
            int activePlayer = 1;
            while (numberOfMarbles > 1) {
                System.out.print("Player" + activePlayer + " [1 ~ " + (numberOfMarbles / 2) + "]: ");
                int taken = in.nextInt();
                numberOfMarbles -= taken;
                System.out.println("Remaining no. of marbles: " + numberOfMarbles);
    
                if (activePlayer == 1) {
                    activePlayer = 2;
                } else {
                    activePlayer = 1;
                }
            }
    
            System.out.println("Player" + activePlayer + " takes the last marble.");
            if (activePlayer == 1) {
                activePlayer = 2;
            } else {
                activePlayer = 1;
            }
            System.out.println("Player" + activePlayer + " wins!");
        }
    }
    

    【讨论】:

    • 我已经尝试过,如果第 8 行(从下面开始计数)中出现名为“未解决的编译问题:无法访问的代码”的变量“activePlayer”的错误。这是什么意思以及如何消除它?
    • @user3865063 这意味着永远无法满足条件或始终在到达某些代码之前退出方法,因此相关代码将永远不会被执行。由于我没有收到此消息,我会假设您有错字并且不要将 1 和 2 依次分配给 activePlayer
    • @user3865063 在评论中你有while(true) 没有break 这意味着while 之后的代码将永远不会运行。更改为while(n1 &gt; 1),它应该可以工作。
    • 我已将您的答案编辑到我的版本中。唯一的区别是变量:numOfMarbles -> n1;采取-> n2; activePlayer -> pn
    • @user3865063 正如我 2 分钟前所说:while(true) 将永远循环。将其更改为while(n1 &gt; 1)
    猜你喜欢
    • 2013-01-21
    • 2019-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多