Ask Question
26 February, 19:42

What is wrong with the following switch statement? int ans; System. out. print ("Type y for yes or n for no"); ans = scan. nextInt (); switch (ans) { case 'y': case 'Y': System. out. println ("You said yes"); break; case 'n': case 'N': System. out. println ("You said no"); break; default: System. out. println ("invalid answer"); }

+3
Answers (1)
  1. 26 February, 22:36
    0
    In this question the ans variable data-type is int, that is wrong.

    Explanation:

    In the above code the ans variable int that is used for taking integer value. and In the switch statement, we pass the value that is character so the program will give the error. To solve this error we will declare the ans variable as char and taking input from the scanner class like this (ans = scan. next (). charAt (0); ) where ans is variable, scan is the object of scanner class and next (). charAt (0) is used to take input. that is store in ans variable.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “What is wrong with the following switch statement? int ans; System. out. print ("Type y for yes or n for no"); ans = scan. nextInt (); ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers