Jump to content
Sign in to follow this  
Sigmahokies

do and while loop

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

Share this post


Link to post
Share on other sites

Your loop isn't inside a method, I don't believe this code will even compile.

Share this post


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.

Share this post


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.

Share this post


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. 

Share this post


Link to post
Share on other sites

Are you talking about method that above of all must inside like public void (args) {}? all code are inside args already. If you want, I can show you a full of source in Java.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×