Why does my code print "\"O.O…\" ! That’s an invalid input." at the end of every option except for option number 4

huangapple go评论158阅读模式
英文:

Why does my code print "\"O.O…\" ! That’s an invalid input." at the end of every option except for option number 4

问题


public class Main {
    public static void main(String[] args) {
        final Scanner console = new Scanner(System.in);
        String userName;
        Double randomNumber;

        System.out.println("Hi, enter your name!");
        userName = console.nextLine();
        System.out.println();

        System.out.println("There are four displays here: 1) DOG , 2) LOVE, 3) a random number\n"
                + "generated by Math.random(), and 4) Bye.\n"
                + "Please enter your choice by type in 1, 2, 3, or 4. ");
        int userNumber = console.nextInt();
        System.out.println();

        if (userNumber == 1) {
            System.out.println("\"" + userName + "\"! Thank you for choosing DOG.");
            System.out.println(" _   _\n"
                    + "/(. .)\\    )\n"
                    + "  (*)____/|\n"
                    + "  /       |\n"
                    + " /   |--\\ |\n"
                    + "(_)(_)  (_)");
        } else if (userNumber == 2) {
            System.out.println("\"" + userName + "\"! Thank you for choosing LOVE.");
            System.out.println("        @@@@@@           @@@@@@\n"
                    + "      @@@@@@@@@@       @@@@@@@@@@\n"
                    + "    @@@@@@@@@@@@@@   @@@@@@@@@@@@@@\n"
                    + "  @@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@\n"
                    + " @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
                    + "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
                    + "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
                    + "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
                    + " @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
                    + "  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
                    + "   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
                    + "    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
                    + "      @@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
                    + "        @@@@@@@@@@@@@@@@@@@@@@@\n"
                    + "          @@@@@@@@@@@@@@@@@@@\n"
                    + "            @@@@@@@@@@@@@@@\n"
                    + "              @@@@@@@@@@@\n"
                    + "                @@@@@@@\n"
                    + "                  @@@\n"
                    + "                   @");
        } else if (userNumber == 3) {
            System.out.println("\"" + userName + "\"! Thank you for choosing a Random Number.");
            randomNumber = Math.random();
            System.out.println("The Number you choose is " + randomNumber);
            System.out.println();
        } else if (userNumber == 4) {
            System.out.println("\"" + userName + "\"! Thank you for ending the program.\n"
                    + "\n"
                    + " //Bye\\\\");
        } else {
            System.out.println("\"O.O…\" ! That’s an invalid input.");
        }
    }
}

The issue in your code is that the last else statement is associated with the last if condition, so it will execute whenever the user input is not equal to 4. I've corrected the code to use else if statements after the initial if for option 4. This way, the "invalid input" message will only be printed if the user input is not 1, 2, 3, or 4.

英文:
public class Main {

	public static void main(String[] args)
	{
		final Scanner console = new Scanner(System.in);
		String userName;
		Double randomNumber;
		/* Ask user for name */
		System.out.println("Hi, enter your name!  ");
		userName = console.nextLine();
		System.out.println();
		/* Ask user for number */
		System.out.println("There are four displays here: 1) DOG , 2) LOVE, 3) a random number\r\n"
				+ "generated by Math.random(), and 4) Bye.\r\n"
				+ "Please enter your choice by type in 1, 2, 3, or 4. ");
		int userNumber = console.nextInt();
		System.out.println();
		if (userNumber==1)
		{
			/* prints output */
			System.out.println("\"" + userName + "\"! Thank you for choosing DOG."); // output
			System.out.println(" _   _\r\n" // output
					+ "/(. .)\\    )\r\n"
					+ "  (*)____/|\r\n"
					+ "  /       |\r\n"
					+ " /   |--\\ |\r\n"
					+ "(_)(_)  (_)");
		}
		
		if (userNumber==2) 
		{
			/* prints output */
			System.out.println("\"" + userName + "\"! Thank you for choosing LOVE."); // output
			System.out.println("        @@@@@@           @@@@@@\r\n" // output
					+ "      @@@@@@@@@@       @@@@@@@@@@\r\n"
					+ "    @@@@@@@@@@@@@@   @@@@@@@@@@@@@@\r\n"
					+ "  @@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@\r\n"
					+ " @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n"
					+ "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n"
					+ "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n"
					+ "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n"
					+ " @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n"
					+ "  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n"
					+ "   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n"
					+ "    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n"
					+ "      @@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n"
					+ "        @@@@@@@@@@@@@@@@@@@@@@@\r\n"
					+ "          @@@@@@@@@@@@@@@@@@@\r\n"
					+ "            @@@@@@@@@@@@@@@\r\n"
					+ "              @@@@@@@@@@@\r\n"
					+ "                @@@@@@@\r\n"
					+ "                  @@@\r\n"
					+ "                   @");
		}

		
		if (userNumber==3) 
		{
			/* prints output */
			System.out.println("\"" + userName + "\"! Thank you for choosing a Random Number."); // output
			randomNumber = Math.random();
			System.out.println("The Number you choose is " + randomNumber); // output
			System.out.println("");
		}
		
		if (userNumber==4) 
		{
			/* prints output */
			System.out.println("\"" + userName + "\"! Thank you for ending the program.\r\n" // output
					+ "\r\n"
					+ " //Bye\\\\");
		}
		
		else
		{
			/* prints output */
			System.out.println("\"O.O…\" ! That’s an invalid input."); // output
		}
	}

} 

Why does my code print ""O.O…" ! That’s an invalid input." at the end of every option except for option number 4. How do I make ""O.O…" ! That’s an invalid input." only print if I type in a number other than 1, 2, 3, and 4? Thanks. ______________________________________________________________________________

答案1

得分: 1

你的 else 仅与 if (userNumber == 4) 相关联。

例如,当 userNumber == 3 时,不等于 4,因此将执行 else 部分。

如果你想要一系列的条件,应该使用 if .. else if .. else if .. else

英文:

Your else is only associated with if (userNumber == 4)

For example, when userNumber == 3, then it isn't 4, so the else will be executed.

If you want a chain of conditions, you should use if .. else if .. else if .. else

huangapple
  • 本文由 发表于 2020年10月26日 23:46:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64540456.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定