Problems creating class and method

-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 that I must for a { key, but I believe this is not the solution. In line 6 in while the program says:

  

"illegal start of type". ~

I started very little time and I do not understand how to solve these errors.

    
asked by anonymous 26.09.2017 / 16:48

2 answers

4

First you create the class and then create the method, then it would look 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");
            }
        }
    }
}

Obviously a class should not be that simple. I solved the problem reported in the question, but it is far from really addressing the issue. You still have flooring to understand how to create a class.

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

I had other errors. Try to learn in a structured way, one step at a time, the basics of syntax, the imperative, and then do more advanced things. If you do not understand what each character does in the code you are not actually programming.

    
26.09.2017 / 16:51
0

You've mixed class creation with method creation.

First create the class:

public class NomeDaClasse {
}

and the method you can put inside the class:

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

Your method has very strange logic, for example:

  • Returns as string but is not returning anything.
  • You are using the Random.nextInt() function that returns a inteiro , and assigning a variable string .
  • Finally, a code review, first of all, to avoid future problems.

        
    26.09.2017 / 19:08