2013-11-21 46 views
1

我只是想问题,标题说什么。这是我的例子,我希望x是随机的新集合。我也正在做这个关闭电话开关不支持等号 - 所以 - 意味着平等。另外,支架(所以当我做 构造一个 - 新的构造函数(x)的在Java中的构造函数中设置参数的值

public class Opponent (

    public static x - 0; 

    public Opponent (int value) (
     value - 5; 
    ) 

    public static void main (String() args) (
     Opponent character1 - new Opponent(x) 
     System.out.println(x); 
    ) 
) 

基本上我想X成为5场比赛,我发展的关注则随机的值应该给他们的参数新创建的角色。我有

问题是,它不工作,这意味着它可能无法做到这一点。

反正我能做到这一点。

我道歉,如果它是愚蠢的问题但不管怎样,谢谢。

+0

首先,定义为字段,则需要括号'{}'未圆括号 –

+0

而'='代替' - ' –

+1

这是非常基本的东西。不仅是你的问题,而且是你写的任何东西的语法。请在查询这样的问题之前查看一些教程并学习一些回馈。 –

回答

-1

也许是这样的。我离开random number generation给你:

public class Opponent 
{ 
    private int score; 

    public Opponent() 
    { 
     // you probbaly want to override this with 
     // a random number generator 
     this.score = 0; 
    } 

    public Opponent(int value) 
    { 
     this.score = value; 
    } 

    public int getScore() 
    { 
     return this.score; 
    } 

    public void setScore(int score) 
    { 
     this.score = score; 
    } 

    public static void main(String[] args) 
    { 
     Opponent character1 = new Opponent(); 
     Opponent character2 = new Opponent(5); 

     int result = character1.getScore() - character2.getScore(); 
     System.out.println(result); 
    } 
} 
0
public class Opponent { 

    public static int x = 0; 

    public Opponent (int value) { 
     x = value; 
    } 

    public static void main (String[] args) { 
     int y = 5; // random number I chose to be 5 
     Opponent character1 = new Opponent(y); 
     System.out.println(character1.x + ""); // will display 5 (which was int y) 
    } 
} 

问题列表:

•打开/关闭的方法/类,不使用();你必须使用{}

public static void main (String() args)需求是
public static void main (String[] args)

•要初始化的东西,用=-

•您需要给x一个类型,例如int

•当您定义Opponent character1 - new Opponent(x)时,您需要在末尾有一个分号。

?即使您尝​​试将x的参数一起定义,您在行Opponent character1 = new Opponent(y);中传递了x作为参数。给它一个不同的价值。


一记:为什么你会在类定义类的实例?如果不使用这个的:

Opponent character1 = new Opponent(y); 
System.out.println(character1.x + ""); 

你可以只写:

System.out.println(Opponent.x + ""); 

但是,您可以创建character1如果你从不同的类访问Opponent类。

0

老实说,我不知道你的问题是什么,但试运行此:

public class Opponent { 

    public int x; 

    public Opponent (int value) { 
     x = value; 
    } 

    public static void main (String[] args) { 
     Opponent character1 = new Opponent(5); 
     System.out.println(character1.x); // this will output: 5 
    } 
} 

你的问题的几个问题:

  1. 您要初始化静态变量的思考一个实例变量?
  2. X不是在main()
  3. 值初始化未在对手
+0

感谢您的意见。就像我说的那样,我是从电话打字,不能使用大多数符号。 – Abszol