Problems in creating class and method

Asked

Viewed 100 times

-1

I have the following code:

public class String verifyWord(String wordChosen, Scanner reader){ //linha 1
    boolean answeredCorrectly = false;
    int tries = 1;
    String wordChosen = random.nextInt();
    String answer = wordChosen;

    while(tries>0 && answeredCorrectly == false) { //linha 6
        answer = reader.nextInt();

        if(wordChosen == answeredCorrectly) {
            System.out.println("You got it right");
            answeredCorrectly=true;
        }
        else if(answered =! wordChosen){
            System.out.println("Wrong");
        }
    }

}

In the first line according to the program there is an error, after String says I must put a key {, but I believe that this is not the solution. On line 6 in while the program says:

"illegal start of type".~

I started quite recently and I don’t understand how to solve these mistakes.

Screenshot do erro

  • This is the entire code of your class?

  • no, just a part

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

4

First you create the class and inside create the method, then it would be something like this:

public class AlgumNomeAqui {
    public String verifyWord(String wordChosen, Scanner reader){
        boolean answeredCorrectly = false;
        int tries = 1;
        String wordChosen = random.nextInt();
        String answer = wordChosen;
        while (tries>0 && !answeredCorrectly) {
            answer = reader.nextInt();
            if (wordChosen == answeredCorrectly) {
                System.out.println("You got it right");
                answeredCorrectly = true;
            }
            else if (answered != wordChosen) {
                System.out.println("Wrong");
            }
        }
    }
}

I put in the Github for future reference.

Obviously a class should not be that simple. I solved the problem reported in the question, but it is far from really solving the question. Still has ground to understand how to create a class.

There are cases that not even a class is really needed. Since Java requires classes, it can be static if it only has a utility method.

There were other mistakes. Try to learn in a structured way, one step at a time, the basics of the syntax, the imperative to then do more advanced things. If you don’t understand what each character does in the code is not actually programming.

  • However I have already adjusted the code and is not giving any more error, but when I compile now it is appearing error in Main, never before had happened. I’ll put print screen.

  • @Dianamadeira, the image now refers to another problem, which is not the original of this question of openness, nor does it refer to the other question that you opened

0

You mixed class creation with method creation.

First create the class:

public class NomeDaClasse {
}

and the method you can put within the class:

public String verifyWord(String wordChosen, Scanner reader)
{
    ...
}

His method is with very strange logic, for example:

  1. Has the return as string but nothing is returning.
  2. Is using the function Random.nextInt() that returns a inteiro, and assigning to a variable string.

Finally, give a revised code, first of all, to avoid future problems.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.