1

I just wanted to ask basically what the title says. Here's my example and I want x to be the new set random. I also am doing this off a phone switch doesn't support an equal sign so - means equal. Also the bracket is (. So when I do Constructor a - new Constructor(x)

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);
    )
)

Basically I want x to become 5. The game I am developing concerns randomization then the values should give them to the parameter to the newly created character.

Problem I am having is that its not working which means it probably can't do this.

Is there anyway I can do this.

I apologize if it is a dumb question though but anyways thanks.

Paul Samsotha
  • 188,774
  • 31
  • 430
  • 651
Abszol
  • 57
  • 2
  • 10

3 Answers3

1
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)
    }
}

List of Problems:

• To open/close a method/class, don't use ( and ); you have to use { and }

public static void main (String() args) needs to be
public static void main (String[] args)

• To initialize something, use =, not -.

• You need to give x a type, such as int.

• When you defined Opponent character1 - new Opponent(x), you need to have a semi-colon at the end.

• You passed x as a parameter in the line Opponent character1 = new Opponent(y);, even though you were trying to define x with the parameter. Give it a different value.


One note: why would you define an instance of the class in the class? Instead of using this:

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

You could just write this:

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

However, you could create character1 if you were accessing class Opponent from a different class.

Michael Yaworski
  • 12,398
  • 17
  • 59
  • 91
0

I honestly don't know what your question is, but try running this:

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
    }
}

A few problems with your question:

  1. you are trying to initialize a static variable thinking its an instance variable?
  2. x is not initialized in main()
  3. value is not defined as a field in Opponent
Michael
  • 413
  • 2
  • 8
  • Thanks for the comments. Like I said though that I am typing from a phone and cannot use most symbols. – Abszol Nov 21 '13 at 12:46
-1

maybe something like this. i leave the random number generation up to you:

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);
    }
}
Community
  • 1
  • 1
Greg
  • 642
  • 6
  • 7