int input = 0;扫描仪键盘=新扫描仪(System.in); input = keyboard.nextInt();

时间:2014-10-16 04:35:54

标签: java

这是我的大学编程课程的一个项目,我觉得我几乎完成了所有的编程工作。目的是玩“Nim”或移除牙签,直到玩家移除最后一个并且该玩家输了。这可能是一个简单的错误,但我无法弄清楚如何让匹配来显示整个程序的变化。我认为这可能是一个简单的错误,但任何帮助都会很棒。

谢谢!

package cs160.project2;

import java.util.Scanner;

public class CS160Project2 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    System.out.println("Welcome to my game of Nym");
    int playerFirst = 1;     //(int) (Math.random() * 2) + 1;
    int matches;
    if (playerFirst == 1) {
        System.out.println("This time the computer will go first.");
        System.out.println("You may choose the number of toothpicks, This number must be bigger than 20 and less than 30.");
        Scanner keyboard = new Scanner(System.in);
        matches = keyboard.nextInt();
        if (matches <= 20 || matches > 29) {
            System.out.println("sorry but that cannot be an input please enter another number between 20 and 30.");
            matches = keyboard.nextInt();
        }
        System.out.print(matches);
        do {
            computerTurn(matches);
            playerTurn(matches);
        } while (matches > 0);
    }
    if (playerFirst == 2) {

    }
}

public static void playerTurn(int matches) {
    System.out.print("How many toothpicks do you want to remove? ");
    int input = 0;
    Scanner keyboard = new Scanner(System.in);
    input = keyboard.nextInt();
    while (input<=0||input>=4){
        System.out.println("Please enter a number between 1 and 3: ");
        input=keyboard.nextInt();
    }
    if (input > 0 && input < 4) {
        matches=matches-input;
    }

}

public static void computerTurn(int matches) {

    if (matches == 1) {
        System.out.println("I, the Computer will take the last toothpick, and YOU WIN!");
        matches = matches - 1;
        return;
    }
    if (matches == 2) {
        matches = matches - 1;
        System.out.println("My turn. There are 2 toothpicks left. I, the Computer, will remove 1 of them.");
        System.out.print("Your turn, there is 1 toothpick left.");
    }
    if (matches == 3) {
        matches = matches - 2;
        System.out.println("My turn. There are 3 toothpicks left. I, the Computer, will remove 2 of them.");
        System.out.print("Your turn, there is 1 toothpick left.");
    }
    if (matches == 4) {
        matches = matches - 3;
        System.out.println("My turn. There are 4 toothpicks left. I, the Computer, will remove 3 of them.");
        System.out.print("Your turn, there is 1 toothpick left.");
    }
    if (matches == 5) {
        matches = matches - 1;
        System.out.println("My turn. There are 5 toothpicks left. I, the Computer, will remove 1 of them.");
        System.out.print("Your turn, there are 4 toothpick left.");
    }
    if (matches == 6) {
        matches = matches - 1;
        System.out.println("My turn. There are 6 toothpicks left. I, the Computer, will remove 1 of them.");
        System.out.print("Your turn, there are 5 toothpick left.");
    }
    if (matches == 7) {
        matches = matches - 2;
        System.out.println("My turn. There are 7 toothpicks left. I, the Computer, will remove 2 of them.");
        System.out.print("Your turn, there are 5 toothpick left.");
    }
    if (matches == 8) {
        matches = matches - 3;
        System.out.println("My turn. There are 8 toothpicks left. I, the Computer, will remove 3 of them.");
        System.out.print("Your turn, there are 5 toothpick left.");
    }
    if (matches == 9) {
        matches = matches - 1;
        System.out.println("My turn. There are 9 toothpicks left. I, the Computer, will remove 1 of them.");
        System.out.print("Your turn, there are 8 toothpick left.");
    }
    if (matches == 10) {
        matches = matches - 1;
        System.out.println("My turn. There are 10 toothpicks left. I, the Computer, will remove 1 of them.");
        System.out.print("Your turn, there are 9 toothpick left.");
    }
    if (matches == 11) {
        matches = matches - 2;
        System.out.println("My turn. There are 11 toothpicks left. I, the Computer, will remove 2 of them.");
        System.out.print("Your turn, there are 9 toothpick left.");
    }
    if (matches == 12) {
        matches = matches - 3;
        System.out.println("My turn. There are 12 toothpicks left. I, the Computer, will remove 3 of them.");
        System.out.print("Your turn, there are 9 toothpick left.");
    }
    if (matches == 13) {
        matches = matches - 1;
        System.out.println("My turn. There are 13 toothpicks left. I, the Computer, will remove 1 of them.");
        System.out.print("Your turn, there are 12 toothpick left.");
    }
    if (matches == 14) {
        matches = matches - 1;
        System.out.println("My turn. There are 14 toothpicks left. I, the Computer, will remove 1 of them.");
        System.out.print("Your turn, there are 13 toothpick left.");
    }
    if (matches == 15) {
        matches = matches - 2;
        System.out.println("My turn. There are 15 toothpicks left. I, the Computer, will remove 2 of them.");
        System.out.print("Your turn, there are 13 toothpick left.");
    }
    if (matches == 16) {
        matches = matches - 3;
        System.out.println("My turn. There are 16 toothpicks left. I, the Computer, will remove 3 of them.");
        System.out.print("Your turn, there are 13 toothpick left.");
    }
    if (matches == 17) {
        matches = matches - 1;
        System.out.println("My turn. There are 17 toothpicks left. I, the Computer, will remove 1 of them.");
        System.out.print("Your turn, there are 16 toothpick left.");
    }
    if (matches == 18) {
        matches = matches - 1;
        System.out.println("My turn. There are 18 toothpicks left. I, the Computer, will remove 1 of them.");
        System.out.print("Your turn, there are 17 toothpick left.");
    }
    if (matches == 19) {
        matches = matches - 2;
        System.out.println("My turn. There are 19 toothpicks left. I, the Computer, will remove 2 of them.");
        System.out.print("Your turn, there are 17 toothpick left.");
    }
    if (matches == 20) {
        matches = matches - 3;
        System.out.println("My turn. There are 20 toothpicks left. I, the Computer, will remove 3 of them.");
        System.out.print("Your turn, there are 17 toothpick left.");
    }
    if (matches == 21) {
        matches = matches - 1;
        System.out.println("My turn. There are 21 toothpicks left. I, the Computer, will remove 1 of them.");
        System.out.print("Your turn, there are 20 toothpick left.");
    }
    if (matches == 22) {
        matches = matches - 1;
        System.out.println("My turn. There are 22 toothpicks left. I, the Computer, will remove 1 of them.");
        System.out.print("Your turn, there are 21 toothpick left.");
    }
    if (matches == 23) {
        matches = matches - 2;
        System.out.println("My turn. There are 23 toothpicks left. I, the Computer, will remove 2 of them.");
        System.out.print("Your turn, there are 21 toothpick left.");
    }
    if (matches == 24) {
        matches = matches - 3;
        System.out.println("My turn. There are 24 toothpicks left. I, the Computer, will remove 3 of them.");
        System.out.print("Your turn, there are 21 toothpick left.");
    }
    if (matches == 25) {
        matches = matches - 1;
        System.out.println("My turn. There are 25 toothpicks left. I, the Computer, will remove 1 of them.");
        System.out.print("Your turn, there are 24 toothpick left.");
    }
    if (matches == 26) {
        matches = matches - 1;
        System.out.println("My turn. There are 26 toothpicks left. I, the Computer, will remove 1 of them.");
        System.out.print("Your turn, there are 25 toothpick left.");
    }
    if (matches == 27) {
        matches = matches - 2;
        System.out.println("My turn. There are 27 toothpicks left. I, the Computer, will remove 2 of them.");
        System.out.print("Your turn, there are 25 toothpick left.");
    }
    if (matches == 28) {
        matches = matches - 3;
        System.out.println("My turn. There are 28 toothpicks left. I, the Computer, will remove 3 of them.");
        System.out.print("Your turn, there are 25 toothpick left.");
    }
    if (matches == 29) {
        matches = matches - 1;
        System.out.println("My turn. There are 29 toothpicks left. I, the Computer, will remove 1 of them.");
        System.out.print("Your turn, there are 28 toothpick left.");
    }
}

}

2 个答案:

答案 0 :(得分:1)

有多种方法可以解决这个问题,最简单的方法是让你的播放器和计算机转换方法返回一个int,即返回主函数的剩余匹配数。例如:

public static int computerTurn(int matches) {
...
...
return matches;
}

另一种方法是将匹配变量变为静态变量,以便主类中的所有方法都可以访问它。 例如:

 static int matches;
 public static void main(String[] args) {
    System.out.println("Welcome to my game of Nym");
    int playerFirst = 1;     //(int) (Math.random() * 2) + 1;
    //int matches;
    if (playerFirst == 1) {
        System.out.println("This time the computer will go first.");
        System.out.println("You may choose the number of toothpicks, This number must be bigger than 20 and less than 30.");
        Scanner keyboard = new Scanner(System.in);
        matches = keyboard.nextInt();
        if (matches <= 20 || matches > 29) {
            System.out.println("sorry but that cannot be an input please enter another number between 20 and 30.");
            matches = keyboard.nextInt();
        }

答案 1 :(得分:1)

您必须从函数中返回matches值。您可以优化computerTurn - 功能。

public static void main(String[] arg) {
    System.out.println("Welcome to my game of Nym");
    int playerFirst = 1;     //(int) (Math.random() * 2) + 1;
    int matches;
    if (playerFirst == 1) {
        System.out.println("This time the computer will go first.");
        System.out.println("You may choose the number of toothpicks, This number must be bigger than 20 and less than 30.");
        Scanner keyboard = new Scanner(System.in);
        matches = keyboard.nextInt();
        if (matches <= 20 || matches > 29) {
            System.out.println("sorry but that cannot be an input please enter another number between 20 and 30.");
            matches = keyboard.nextInt();
        }
        System.out.print(matches);
        do {
            matches = computerTurn(matches);
            matches = playerTurn(matches);
        } while (matches > 0);
    }
    if (playerFirst == 2) {

    }


}

public static int playerTurn(int matches) {
    System.out.print("How many toothpicks do you want to remove? ");
    int input = 0;
    Scanner keyboard = new Scanner(System.in);
    input = keyboard.nextInt();
    while (input<=0||input>=4){
        System.out.println("Please enter a number between 1 and 3: ");
        input=keyboard.nextInt();
    }
    if (input > 0 && input < 4) {
        matches=matches-input;
    }
    return matches;

}

public static int computerTurn(int matches) {
    int remove = 0;
    if (matches % 4 == 0) {
        remove = 3;
    } else if (matches % 4 == 3) {
        remove = 2;
    } else {
        remove = 1;
    }
    System.out.println("My turn. There are " + matches + " toothpicks left. I, the Computer, will remove " + remove + " of them.");
    matches -= remove;
    System.out.println("Your turn, there is " + matches + " toothpick left.");
    return matches;
}