我正在尝试创建一个Nim稻草游戏

时间:2015-10-30 01:23:54

标签: java

我生成随机起始数量的吸管。计算机首先选择1,2或3个吸管然后玩家去。拾起最后一根稻草的人输了。我想我几乎有正确的代码,但我收到的错误是静态上下文无法引用非静态c。我不确定这意味着什么。此外,为了开始游戏,我应该生成10到20之间的数字。我做得对吗?杯子课是给我的。

    import java.util.Random;
        import java.util.Scanner;
        import java.util.ArrayList;
        import java.lang.Math;
        class MyNim
        {
        public static void main(String[] args)
        {
           int humanScore=0;
           int computerScore=0;
           Scanner kb=new Scanner(System.in);
           int n, a;
           String x;
           x="null";
           int straws;
           Random r=new Random();  
           do
           {
              straws=r.nextInt(11)+10;
              System.out.println("Straws = " +straws);
              while(straws>=0)
              {
                 a=Cup.c.select();
                 straws=straws-a;
                 System.out.println("computer chose "+a+" straws.");
                 if(straws<=1)
                 {
                    System.out.println("Human Wins!!!");
                    humanScore++;
                 }

                 System.out.println("Straws left = " +straws);
                 System.out.println("Your turn. Pick 1,2,or 3 straws.");
                 n=kb.nextInt();

                 straws=straws-n;
                 System.out.println("Straws left = " +straws);
                 if(straws<=1)
                 {
                    System.out.println("Human Loses!!!");
                    computerScore++;
                 }
              }
              System.out.println("Scores: Human = "+humanScore+" Computer =      "+computerScore);
              System.out.println("Game Over! Want to play again? Hit enter to start new game or type in quit to exit game.");
              x=kb.nextLine();  

          }while(!x.equals("quit"));
          System.out.println("Thanks for playing!");
          System.exit(1);
          }
        }
        class Cup
        {
           ArrayList<Integer> c = new ArrayList<Integer>();

           public Cup()
           {
              c.add(1);
              c.add(2);
              c.add(3);
           }
           public int count()
           {
              return c.size();
           }
           public int select()
           {
              int index = (int)(c.size()*Math.random());
              return c.get(index);
           }
           public void remove(Integer move)
           {
              c.remove(move);
           }
}

2 个答案:

答案 0 :(得分:1)

c是属于Cup类的非静态变量。

  

无法从静态上下文中引用非静态变量。

这意味着您无法以静态方法引用此变量,main恰好是。{/ p>

有关详细信息,请参阅以下答案:

What is the reason behind "non-static method cannot be referenced from a static context"?

Non-static variable cannot be referenced from a static context

答案 1 :(得分:0)

如果你想使用Cup类的一个实例,你必须先用NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error]; NSLog(@"%@", str); 方法实例化它,即

main

然后使用final Cup cup = new Cup(); ,例如

cup

你可能不应该直接访问cup的成员变量c。要强制执行此操作,请将列表final int selected = cup.select(); 设为c varibale。

private表示每个类只有一个静态变量/方法。每个实例都存在“非静态”(a.k.a成员)变量/方法。 例如,列表c是成员变量 - 它绑定到特定的Cup实例。如果你有几个这样的杯子:

static

每个人都有自己独立的List c。

访问成员变量/方法只能通过指定它们绑定的实例来起作用:

Cup cupOne = new Cup();
Cup cupTwo = new Cup();

如果您尝试通过他们的课程访问它们,您将收到您看到的错误。

HTH