嵌套 if 语句没有字符串

我正在做一个回文数序列。我已经设置好了数学,但是我在嵌套我的 if 语句时遇到了麻烦,只显示一个答案。


我试图从“else if”中删除“if”,但 java 无法识别。输出不提供“非 5 位数字”,当我输入 5 位回文数时,它会发送 True 和 False 输出,而不仅仅是 true。但是,它非常适合输入错误的 5 位数字。我能否就如何正确嵌套提供一些帮助是我所要求的,或者是一个很好的解释示例来帮助我实现结果,因为我去过很多网站,但大多数都太简单并且缺乏示例比较。


import java.util.Scanner;

public class Palindrome

{

   public static void main(String args[])

   {

    Scanner input = new Scanner(System.in);

   System.out.print ("Enter 5-digit integer value: ");

   int userNumber = input.nextInt();

    System.out.println("Input value: " + "\t" + userNumber);


      //User input incorrect amount of digits

      if (userNumber >= 100000 && userNumber <= 9999) 

      {

      System.out.println("Not 5 digits.");

      }   

         else if(userNumber < 100000 && userNumber > 9999)

             {

            int Number5 = (userNumber/10000) % 10;

            int Number4 = (userNumber/1000) % 10;

            int Number3 = (userNumber /100) % 10;

            int Number2 = (userNumber/10) % 10;

            int Number1 = userNumber % 10;


            //Conditions are met for Palidrome Number

              if(Number1 == Number5 && Number2 == Number4)

                {

                  System.out.println("Judgement: " + "\t" + "\t" + "True");

               }

               //Conditions are not met for Palidrome Number

               else if (Number1 != Number5 || Number2 !=Number4);

                     {

                  System.out.println("Judgement: " + "\t" + "\t" + "False");

                   }     

     }

   }

}

结果应显示真、假或非 5 位数字


潇潇雨雨
浏览 99回答 2
2回答

繁星coding

没有数字是>= 100000 and <= 9999,这就是为什么你永远看不到不是 5 位数字的原因。您必须将该条件更改为:if (userNumber <= 9999 || userNumber > 99999)并改变:else if(userNumber < 100000 && userNumber > 9999)到一个简单的else因为如果代码到达该else部分,那么userNumber < 100000 && userNumber > 9999总是true.这同样适用于else if (Number1 != Number5 || Number2 !=Number4);(注意:;最后是错误的并且是严重头痛的根源)必须更改为:else这个说法:int Number3 = (userNumber / 100) % 10;是多余的,因为 的值Number3在这种情况下没有用。所以你的代码应该像这样简化:public static void main(String[] args) {&nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; System.out.print ("Enter 5-digit integer value: ");&nbsp; &nbsp; int userNumber = input.nextInt();&nbsp; &nbsp; System.out.println("Input value: " + "\t" + userNumber);&nbsp; &nbsp; if (userNumber <= 9999 || userNumber > 99999) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Not 5 digits.");&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; int Number5 = (userNumber / 10000) % 10;&nbsp; &nbsp; &nbsp; &nbsp; int Number4 = (userNumber / 1000) % 10;&nbsp; &nbsp; &nbsp; &nbsp; int Number2 = (userNumber / 10) % 10;&nbsp; &nbsp; &nbsp; &nbsp; int Number1 = userNumber % 10;&nbsp; &nbsp; &nbsp; &nbsp; if (Number1 == Number5 && Number2 == Number4) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Judgement: " + "\t" + "\t" + "True");&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Judgement: " + "\t" + "\t" + "False");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

白猪掌柜的

这可以工作:import java.util.Scanner;import java.lang.*;public class Main{&nbsp; &nbsp;public static void main(String args[])&nbsp; &nbsp;{&nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp;System.out.print ("Enter 5-digit integer value: ");&nbsp; &nbsp;String userNumber = input.next();&nbsp; &nbsp; System.out.println("Input value: " + "\t" + userNumber);&nbsp; &nbsp; &nbsp; //User input incorrect amount of digits&nbsp; &nbsp; &nbsp; if (userNumber.length()!=5)&nbsp;&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; System.out.println("Not 5 digits.");&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Conditions are met for Palidrome Number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(new StringBuilder(userNumber).reverse().toString().equals(userNumber))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Judgement: " + "\t" + "\t" + "True");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Conditions are not met for Palidrome Number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Judgement: " + "\t" + "\t" + "False");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}}您的第二条else if语句是多余的,并且您的最后一条 if 语句中有一个额外的分号。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java