Jump to content

Recommended Posts

Hi everyone,

this is my second thread, I hope you can help me why do and while won't do loop as I put them in...

	public class GameGuess extends Thread {
	    int a;
	    public void Send(int a) {
	        this.a = a;
    do {
        int p1 = (int) ((Math.random() + 5) * 10);
        int p2 = (int) ((Math.random() + 5) * 10);
	        System.out.print("Player 1: " + p1);
        System.out.print("Player 2: " + p2);
	        if (p1 != a) {
            System.out.print("Player 1 get wrong guess.");
        } else if (p1 == a) {
            System.out.print("The Player 1 wins!");
        } else if (p2 != a) {
            System.out.print("Player 2 get wrong guess");
        } else if (p2 == a) {
            System.out.print("The Player 2 wins!");
    while (p1 != a || p2 != a);

Link to post
Share on other sites

Your code is not inside a method. In Java, all the code must be inside methods. What program are you using to write Java? It should be highlighting those syntax errors.

if you don't know what a method is, you'll have to start learning the very basics of object-oriented programming.

Link to post
Share on other sites

NetBean. It shows highlight error in do and while line. I checked the method has been correct,  but it won't do loop. all method and compare are inside, I wrote code after parameter. Java source said while accept != as not equal. isn't that strange?

I know what method is. I have been learning about OOP already, I am moving into intermediate level now.

Link to post
Share on other sites

In object-oriented programming objects are composed of properties and of methods. Properties contain information about the object and methods are actions that the object can do.

In your code the GameGuess object has a Send () method. Your do...while loop is not inside a method, but in Java, all logic has to be inside methods. If you have changed your code, please post your new code. 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...