I cannot locate my spelling/punctuation errors. My code runs fine, yet there is something wrong [closed]

StackOverflow https://stackoverflow.com/questions/14929096

  •  10-03-2022
  •  | 
  •  

Question

I wrote out a program in Java that allows the user to name certain sports based on the number of players and vice versa using a scanner. I ran it with almost every combination I could think of, and it works fine. After submitting as a class project for grading, the automated online compiler we are supposed to use returned with 2 errors. The professor tells me that there should be something wrong with my spelling/punctuation, but I cannot pinpoint it. Can someone take a look and tell me if they find something? All help is appreciated!

import java.util.Scanner;

public class SportsQuiz {

public static void main(String[] args) {

    Scanner s = new Scanner(System.in);

    final String PROMPT = "Enter 1 to guess a sport, 2 to guess how"
            + " many players: ";

    final int NUMOFBASKET = 5;
    final int NUMOFBASE = 9;
    final int NUMOFCURL = 4;
    final int NUMOFFOOT = 11;

    System.out.print(PROMPT);

    int promptnum = s.nextInt();

    if(promptnum == 1) {

        System.out.print("Choose number of players: ");

        int numplayers = s.nextInt();

        s.nextLine();

        if(numplayers != NUMOFBASKET && numplayers != NUMOFBASE 
                && numplayers != NUMOFCURL && numplayers != NUMOFFOOT){

            System.out.print("Invalid choice.");

        } else {

            System.out.print("Which sport has " + numplayers
                    + " players? ");

            String playsport = s.nextLine();

            if(playsport.equals("Basketball")
                    ||playsport.equals("basketball")
                    && numplayers == NUMOFBASKET) {

                System.out.print("Correct!");

            } else if(playsport.equals("Football")
                    ||playsport.equals("football") 
                    && numplayers == NUMOFFOOT) {

                System.out.print("Correct!");

            } else if(playsport.equals("Baseball")
                    ||playsport.equals("baseball")
                    && numplayers == NUMOFBASE) {

                System.out.print("Correct!");       

            } else if(playsport.equals("Curling")
                    ||playsport.equals("curling")
                    && numplayers == NUMOFCURL) {

                System.out.print("Correct!");

            } else {

                System.out.print("Incorrect");

            }

        }

    } else if(promptnum == 2) {

        System.out.print("Choose a sport: ");

        s.nextLine();

        String sport = s.nextLine();

        if(sport.equals("Basketball")||sport.equals("basketball")) {

            System.out.print("How many players are on a " + sport 
                    + " team? ");

            int numsportplayers = s.nextInt();

            if(numsportplayers == NUMOFBASKET){

                System.out.print("Correct!");

            } else {

                System.out.print("Incorrect!");

            }



        } else if(sport.equals("Baseball")||sport.equals("baseball")) {

            System.out.print("How many players are on a " + sport 
                    + " team? ");

            int numsportplayers = s.nextInt();

            if(numsportplayers == NUMOFBASE){

                System.out.print("Correct!");

            } else {

                System.out.print("Incorrect!");

            }

        } else if(sport.equals("Football")||sport.equals("football")) {

            System.out.print("How many players are on a " + sport 
                    + " team? ");

            int numsportplayers = s.nextInt();

            if(numsportplayers == NUMOFFOOT){

                System.out.print("Correct!");

            } else {

                System.out.print("Incorrect!");

            }

        } else if (sport.equals("Curling")||sport.equals("curling")) {

            System.out.print("How many players are on a " + sport 
                    + " team? ");

            int numsportplayers = s.nextInt();

            if(numsportplayers == NUMOFCURL){

                System.out.print("Correct!");

            } else {

                System.out.print("Incorrect!");
            }

        } else {

            System.out.print("Invalid Choice.");

        }

    } else {

        System.out.print("Invalid Choice.");

    }

  }

}

Thanks in advance!

Was it helpful?

Solution

In one instance you have "Incorrect" without an exclamation mark.

Also, in one instance you have "Invalid choice." but in another you have "Invalid Choice." (with capital C).

I'm guessing these are the two errors.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top