如何检查if else条件中的else语句

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

how to check the else statement in if else condition

问题

  1. package react;
  2. import java.util.Scanner;
  3. public class Intputfromuser {
  4. public static void main(String[] args) {
  5. // TODO Auto-generated method stub
  6. System.out.println("请输入一个数字,与数字5进行比较:");
  7. Scanner input = new Scanner(System.in);
  8. int a;
  9. if (input.hasNextInt()) {
  10. a = input.nextInt();
  11. if (a == 5) {
  12. System.out.println("您输入的与数字5相同。");
  13. } else if (a < 5) {
  14. System.out.println("您输入的数字小于5。");
  15. } else {
  16. System.out.println("您输入的数字大于5。");
  17. }
  18. } else {
  19. System.out.println("您输入了无效的内容,必须输入一个整数。");
  20. }
  21. }
  22. }

请注意,我进行了一些修改,以确保仅在用户输入整数时才继续,否则会触发else语句。

英文:
  1. package react;
  2. import java.util.Scanner;
  3. public class Intputfromuser {
  4. public static void main(String[] args) {
  5. // TODO Auto-generated method stub
  6. System.out.println(&quot;enter a number to compare with number 5 &quot;);
  7. Scanner input= new Scanner(System.in);
  8. int a=input.nextInt();
  9. if(a==2)
  10. {
  11. System.out.println(&quot;U Have Entered The same value&quot;);
  12. }
  13. else if(a&lt;2)
  14. {
  15. System.out.println(&quot;Ur number is Smaller than 2&quot;);
  16. }
  17. else if(a&gt;2)
  18. {
  19. System.out.println(&quot;U Have Entered the number Greater than &quot;);
  20. }
  21. else {
  22. System.out.println(&quot;U Have Enterer Invalid Input&quot;);
  23. }
  24. }
  25. }

how to get only integer from the user if the user enters any thing except integer then else statement should run

答案1

得分: 0

使用input.nextLine()代替,并将其解析为一个字符串。

为了避免ParseException,使用try { ... } catch() { ... }块将其包围。

在catch块中,你可以例如打印一条消息,告知用户输入错误。

  1. public static void main(String[] args) {
  2. System.out.println("输入一个数字以与数字5进行比较");
  3. Scanner s = new Scanner(System.in);
  4. String userInput = s.nextLine();
  5. try {
  6. int option = Integer.parseInt(userInput);
  7. if (option == 2)
  8. {
  9. System.out.println("您输入了相同的值");
  10. }
  11. else if (option < 2)
  12. {
  13. System.out.println("您的数字小于2");
  14. }
  15. else if (option > 2)
  16. {
  17. System.out.println("您输入了大于2的数字");
  18. }
  19. } catch (NumberFormatException e) {
  20. System.out.println("无效输入!");
  21. }
  22. }

希望这有所帮助!

英文:

Use input.nextLine() instead and parse it to a String.

To avoid a ParseException, surround it by using a try { ... } catch() { ... } block.

In the catch block you can e.g. print a message informing the user of the wrong input.

  1. public static void main(String[] args) {
  2. System.out.println(&quot;enter a number to compare with number 5 &quot;);
  3. Scanner s = new Scanner(System.in);
  4. String userInput = s.nextLine();
  5. try {
  6. int option = Integer.parseInt(userInput);
  7. if (option == 2)
  8. {
  9. System.out.println(&quot;U Have Entered The same value&quot;);
  10. }
  11. else if (option &lt; 2)
  12. {
  13. System.out.println(&quot;Ur number is Smaller than 2&quot;);
  14. }
  15. else if (option &gt; 2)
  16. {
  17. System.out.println(&quot;U Have Entered the number Greater than 2&quot;);
  18. }
  19. } catch (NumberFormatException e) {
  20. System.out.println(&quot;Invalid input!&quot;);
  21. }
  22. }

Hope this sort of helped!

答案2

得分: 0

你还可以创建一个方法来收集输入并在循环中使用,就像这样:

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. System.out.print("输入一个要与数字5比较的数字: ");
  5. int userInput = getInteger();
  6. if (userInput == 2)
  7. {
  8. System.out.println("您已输入相同的值");
  9. }
  10. else if (userInput < 2)
  11. {
  12. System.out.println("您的数字小于2");
  13. }
  14. else {
  15. System.out.println("您已输入大于2的数字");
  16. }
  17. }
  18. static int getInteger() {
  19. boolean correct = false;
  20. Scanner input = new Scanner(System.in);
  21. int userInput = 0;
  22. do {
  23. try {
  24. userInput = input.nextInt();
  25. correct = true;
  26. } catch (Exception e) {
  27. System.out.println("输入不正确");
  28. System.out.println("请再试一次: ");
  29. } finally {
  30. input.nextLine();
  31. }
  32. }
  33. while (!correct);
  34. input.close();
  35. return userInput;
  36. }
  37. }

使用scanner.nextInt()scanner.nextDouble()时,需要在其后调用scanner.nextLine()来清除输入。否则,您将陷入无限循环。

英文:

You can also create method to collect input and make it inside loop like this:

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. System.out.print(&quot;enter a number to compare with number 5: &quot;);
  5. int userInput = getInteger();
  6. if (userInput == 2)
  7. {
  8. System.out.println(&quot;U Have Entered The same value&quot;);
  9. }
  10. else if (userInput &lt; 2)
  11. {
  12. System.out.println(&quot;Ur number is Smaller than 2&quot;);
  13. }
  14. else {
  15. System.out.println(&quot;U Have Entered the number Greater than 2&quot;);
  16. }
  17. }
  18. static int getInteger() {
  19. boolean correct = false;
  20. Scanner input = new Scanner(System.in);
  21. int userInput = 0;
  22. do {
  23. try {
  24. userInput = input.nextInt();
  25. correct = true;
  26. } catch (Exception e) {
  27. System.out.println(&quot;Incorrect input&quot;);
  28. System.out.println(&quot;Please try again: &quot;);
  29. } finally {
  30. input.nextLine();
  31. }
  32. }
  33. while (!correct);
  34. input.close();
  35. return userInput;
  36. }
  37. }

Important note with scanner.nextInt() or scanner.nextDouble()
you need to call scanner.nextLine() after that to clear input. Otherwise you will end up with endless loop.

答案3

得分: 0

以下是翻译好的代码部分:

  1. public static void main(String[] args) {
  2. /* 打开键盘输入流。无需关闭此流。
  3. 当应用程序关闭时,JVM会自动关闭它。 */
  4. Scanner input = new Scanner(System.in);
  5. String val = ""; // 用于存储用户输入:
  6. // 用户提示,带有退出功能和输入验证:
  7. while (val.isEmpty()) {
  8. System.out.print("输入一个数字以与数字5进行比较 (输入q退出): -> ");
  9. val = input.nextLine().trim(); // 去除可能的空格。
  10. // 是否提供了 'q' 以退出?
  11. if (val.equalsIgnoreCase("q")) {
  12. /* 是的...然后退出。从main()中返回将有效地关闭这个特定的应用程序: */
  13. System.out.println("退出 - 再见");
  14. return;
  15. }
  16. // 验证输入:
  17. /* 输入是否是带符号或无符号整数的字符串表示,
  18. 并且提供的值是否在int范围内? */
  19. if (!val.matches("-?\\d+") || (Long.parseLong(val) < Integer.MIN_VALUE) ||
  20. (Long.parseLong(val) > Integer.MAX_VALUE)) {
  21. // 否...通知用户并允许重试:
  22. System.out.println("无效的数字输入! {" + val + ") 请再试一次..."
  23. + System.lineSeparator());
  24. val = ""; // 清空变量以确保重新循环:
  25. }
  26. }
  27. // 如果代码执行到此点,用户输入是有效的!
  28. // 现在将字符串数值解析为int:
  29. int a = Integer.parseInt(val);
  30. /* 此时,只有三种可用条件:
  31. 相等,小于和大于(有效性已在 `while` 循环中处理): */
  32. // 相等:
  33. if (a == 5) {
  34. System.out.println("您输入了相同的值。");
  35. }
  36. // 小于:
  37. else if (a < 5) {
  38. System.out.println("您的数字小于5。");
  39. }
  40. // 大于:
  41. else {
  42. System.out.println("您输入的数字大于5。");
  43. }
  44. // 完成
  45. }

希望这对您有所帮助。如果您有任何其他问题,请随时提问。

英文:

Another alternative. Be sure to read the comments in code:

  1. public static void main(String[] args) {
  2. /* Open a keyboard input stream. There is no need to close
  3. this stream. The JVM will do that automatically when the
  4. application closes. */
  5. Scanner input = new Scanner(System.in);
  6. String val = &quot;&quot;; // Used to store User input:
  7. // User Prompt with &#39;quit&#39; capability and entry validation:
  8. while (val.isEmpty()) {
  9. System.out.print(&quot;Enter a number to compare with number 5 (q to quit): -&gt; &quot;);
  10. val = input.nextLine().trim(); // Trim in case just a whitespace(s) was entered.
  11. // Was &#39;q&#39; for quit supplied?
  12. if (val.equalsIgnoreCase(&quot;q&quot;)) {
  13. /* Yes...then quit. Returning out of main() effectively
  14. closes this particular application: */
  15. System.out.println(&quot;Quiting - Bye Bye&quot;);
  16. return;
  17. }
  18. // Validate Entry:
  19. /* Is entry a string representation of a signed or unsigned Integer
  20. value and does the supplied value fall within the relm of an int? */
  21. if (!val.matches(&quot;-?\\d+&quot;) || (Long.parseLong(val) &lt; Integer.MIN_VALUE) ||
  22. (Long.parseLong(val) &gt; Integer.MAX_VALUE)) {
  23. // No...Inform User and allow to try again:
  24. System.out.println(&quot;Invalid Numerical Entry! {&quot; + val + &quot;) Try again...&quot;
  25. + System.lineSeparator());
  26. val = &quot;&quot;; // Empty variable to ensure re-loop:
  27. }
  28. }
  29. // If you make it to this point in code, the User input was valid!
  30. // Now parse the String numerical value to an int:
  31. int a = Integer.parseInt(val);
  32. /* At this point, there are only three usable conditions:
  33. Equal To, Less Than, and Greater Than (validity has
  34. already been handled within the `while` loop: */
  35. // Equal To:
  36. if (a == 5) {
  37. System.out.println(&quot;You have entered The same value.&quot;);
  38. }
  39. // Less Than:
  40. else if (a &lt; 5) {
  41. System.out.println(&quot;Your number is smaller than 5.&quot;);
  42. }
  43. // Greater Than:
  44. else {
  45. System.out.println(&quot;You have entered a number greater than 5.&quot;);
  46. }
  47. // DONE
  48. }

huangapple
  • 本文由 发表于 2023年2月6日 17:51:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359719.html
匿名

发表评论

匿名网友

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

确定