尼姆游戏不起作用!(JAVA)

时间:2015-08-23 13:22:33

标签: java

这是我的主要方法

package game1;

public class game1 {

    public static int c;

    public static void main(String args[])
    {
        System.out.print("Welcome to my Game of Nim\n\n");
        toss TOSS = new toss();
        toothpicks TOOTHPICKS = new toothpicks();
        letsPlay START = new letsPlay();
        c = TOSS.WhoGoesFirst();
        toothpicks.number = TOOTHPICKS.HowMany();

        while(toothpicks.number > 1)
        {
            if(c==0 || c==2)
            {
                toothpicks.number = START.yourTurn();
            }   
            else if(c==1 || c==2)
            {
                toothpicks.number = START.myTurn();
            }
            c=2;
        }

    }

}

这是我为玩游戏而建的课程

package game1;
import java.util.Scanner;

public class letsPlay {

    Scanner input = new Scanner(System.in);
    int remove;
    public int yourTurn()
    {
         if(toothpicks.number == 1)
        {
            System.out.print("YOU LOSE");
            return 0;
        }

        System.out.print("Your turn. There are " +toothpicks.number+"       toothPicks left. ");
        System.out.print("How many You want to remove?");
        remove = input.nextInt();
        toothpicks.number -= remove;

        return toothpicks.number;
    }

    public int myTurn()
     {
        if(toothpicks.number == 1)
        {
            System.out.print("My turn. There are " +toothpicks.number+" toothPicks left. \n");
            System.out.print("I,the Computer, will remove it\n");
            System.out.print("YOU WIN");
            remove=1;
            toothpicks.number -= remove;
        }
        else if(toothpicks.number==2)
        {
            System.out.print("My turn. There are " +toothpicks.number+" toothPicks left. ");
            remove = 1;
            System.out.print("I,the Computer, will remove "+ remove +" toothPicks\n");
            toothpicks.number -= remove;
        }
        else if(toothpicks.number==3)
        {
            System.out.print("My turn. There are " +toothpicks.number+" toothPicks left. ");
            remove = 2;
            System.out.print("I,the Computer, will remove "+remove +" toothPicks\n");
            toothpicks.number -= remove;
        }
        else
        {
            System.out.print("My turn. There are " +toothpicks.number+" toothPicks left. ");
            remove =1 + (int)(Math.random()*4); 
            System.out.print("I,the Computer, will remove "+remove +" toothPicks.\n");
            toothpicks.number-=remove;
        }

         input.close();
         return toothpicks.number;
      }

 }

编辑:这是牙签类

   public class toothpicks {

   public static int number;
   public int HowMany()
   {
        Scanner input =new Scanner(System.in);
        do
        {
            if(game1.c==1)
            {
                System.out.print("I choose the no. of toothPicks to be " );
                number = input.nextInt(); 
                if(number<20 || number>30)
                {
                    System.out.print("ToothPicks Should be between 20-30\n");
                }
            }
            else
            {
                number = 20 + (int)(Math.random()*10);
                System.out.print("computer has choosed "+ number +" Toothpicks \n");
            }

        }while(number<20 || number>30);

        input.close();
        return number;

    }

    }

我还做了其他课程折腾的折腾。当我运行这个程序时,它无法解决 letsPlay 类中出错的问题,因为我对JAVA非常陌生,我无法弄明白,程序在它要求我之前停止输入我要删除的牙签数量。扫描仪输入有什么问题或它是什么?任何帮助都将不胜感激。

输出        欢迎来到我的Nim游戏

 This time Computer are going to go first
 I choose the no. of toothPicks to be 23
My turn. There are 23 toothPicks left. I,the Computer, will remove 4  toothPicks.
Your turn. There are 19 toothPicks left. How many You want to remove?   Exception in thread "main" java.util.NoSuchElementException

at java.util.Scanner.throwFor(Unknown Source)

at java.util.Scanner.next(Unknown Source)

at java.util.Scanner.nextInt(Unknown Source)

at java.util.Scanner.nextInt(Unknown Source)

at game1.letsPlay.yourTurn(letsPlay.java:18)

at game1.game1.main(game1.java:21)

请注意 number是一个静态变量,我在toothpicks类中声明。

1 个答案:

答案 0 :(得分:-1)

试试这个 -

首先,不要忘记在使用它的类中导入java.util.Scanner

java.util.Scanner请求整数时,您必须执行以下操作:

Scanner s = new Scanner(System.in);
int i = s.nextInt();
s.nextLine();

试试吧。在任何s.nextLine()使用后立即s.nextInt()

此外,在程序完成之前,请不要运行s.close()。否则,它将从System.in关闭InputStream,这通常是不需要的。