How will i correct the errors in this code

0 votes
asked Jul 15 by Jagunmolu-dev (120 points)
import com.sun.deploy.security.SelectableSecurityManager;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import javax.swing.*;
import java.util.Random;
import java.util.Scanner;

public class game {
    public static void main(String[] args) {
        System.out.println("Welcome to the lucky guess game");
        System.out.println("Please enter your name");

        Scanner nameScanner = new Scanner(System.in);
        String nameString = nameScanner.next();
        System.out.println("Hello " + nameString);

        System.out.println("Are you ready to begin?");
        System.out.println("\tY: Yes ");
        System.out.println("\tN: No");

        String beginAnswer = nameScanner.next();

                while (beginAnswer != "Y" )  {
                    System.out.println("Are you ready to begin?");
                    System.out.println("\tY: Yes ");
                    System.out.println("\tN: No");

                    beginAnswer = nameScanner.next();
                }

        Random num = new Random();
                int x = num.nextInt( 20) + 1;
                System.out.println("Please Guess a number: ");
                int userinput = nameScanner.nextInt();

                int timesTried = 0;
                boolean hasWon = false;
                boolean shouldFinish = false;

                while (!shouldFinish){
                    timesTried ++;

                    if (timesTried < 5);{
                        if (userinput == x);{
                            hasWon = true;
                            shouldFinish = true;
                    }

                    else if (userinput > x) {
                        System.out.println("Choose lower");
                        }

                     else {
                        System.out.println("Choose higher");
                        }
                        userinput = nameScanner.nextInt();
                    
                else {
                        shouldFinish = true;
                    }

                }

                if (hasWon) {
                    System.out.println("congratulations you have guessed in your: " + timesTried + "guess");
                }
                else {
                    System.out.println("Game Over");
                    System.out.println("The number was: " + x );
                }
}

1 Answer

0 votes
answered Jul 16 by Ashish Kumar (230 points)

In the above code the error are very basic. Mostly syntax error. For example

 if (timesTried < 5);{
 if (userinput == x);{

semicolon after If statement and the bracket is missing to close the main function. I was not able to compile the code probably because I am missing .sun library. But other than that it should be good.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...