我的程序为什么只执行其中一个if语句的else语句?

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

Why is my program only doing the else statement on one of the if statements

问题

{
    Scanner scan = new Scanner(System.in);

    System.out.println("请输入第一个八位组:");
    int a = scan.nextInt();
    System.out.println("请输入第二个八位组:");
    int b = scan.nextInt();
    System.out.println("请输入第三个八位组:");
    int c = scan.nextInt();
    System.out.println("请输入第四个八位组:");
    int d = scan.nextInt();

    if (!(a >= 0 && a <= 255))
    {
        System.out.println("八位组 1 不正确");
    }
    if (!(b >= 0 && b <= 255))
    {
        System.out.println("八位组 2 不正确");
    }
    if (!(c >= 0 && c <= 255))
    {
        System.out.println("八位组 3 不正确");
    }
    if (!(d >= 0 && d <= 255))
    {
        System.out.println("八位组 4 不正确");
    }

    else
    {
        System.out.println("IP 地址:" + a + "." + b + "." + c + "." + d);
    }
}
英文:

I want my code to make sure that all the if statements work, and if one of them doesn't work, it should put that that specific one is incorrect and not show the IP address part. right now when I'm doing it, it only works on the 4th one. the other ones say that it is incorrect but still put the IP address.

{
    Scanner scan = new Scanner(System.in);

    System.out.println(&quot;Please enter the first octet:&quot;);
    int a = scan.nextInt();
    System.out.println(&quot;Please enter the second octet:&quot;);
    int b = scan.nextInt();
    System.out.println(&quot;Please enter the third octet:&quot;);
    int c = scan.nextInt();
    System.out.println(&quot;Please enter the fourth octet:&quot;);
    int d = scan.nextInt();

    if (!(a &gt;= 0 &amp;&amp; a &lt;= 255))
    {
        System.out.println(&quot;Octet 1 is incorrect&quot;);
    }
    if (!(b &gt;= 0 &amp;&amp; b &lt;= 255))
    {
        System.out.println(&quot;Octet 2 is incorrect&quot;);
    }
    if (!(c &gt;= 0 &amp;&amp; c &lt;= 255))
    {
        System.out.println(&quot;Octet 3 is incorrect&quot;);
    }
    if (!(d &gt;= 0 &amp;&amp; d &lt;= 255))
    {
        System.out.println(&quot;Octet 4 is incorrect&quot;);
    }

    else
    {
        System.out.println(&quot;IP Address:&quot; + a + &quot;.&quot; + b + &quot;.&quot; + c + &quot;.&quot; + d);
    }
}

答案1

得分: 2

一个else语句始终只能属于一个if条件。

我假设您想要验证所有IP地址的八位组,并在其中一个不在有效范围内时打印一条消息。只有在地址有效的情况下才应打印(您当前的else指令)。

我建议在运行检查之前创建一个布尔变量。这个布尔变量将告诉我们所有四个八位组是否正确。

boolean allOctetsValid = true;
if (!(a >= 0 && a <= 255))
{
    System.out.println("Octet 1 is incorrect");
    allOctetsValid = false;
}
if (!(b >= 0 && b <= 255))
{
    System.out.println("Octet 2 is incorrect");
    allOctetsValid = false;
}
if (!(c >= 0 && c <= 255))
{
    System.out.println("Octet 3 is incorrect");
    allOctetsValid = false;
}
if (!(d >= 0 && d <= 255))
{
    System.out.println("Octet 4 is incorrect");
    allOctetsValid = false;
}

if(allOctetsValid)
{
    System.out.println("IP地址:" + a + "." + b + "." + c + "." + d);
}

这只是一种可能的解决方案。

额外的改进:

实际条件可以简化为 0 <= d && d <= 255

考虑创建一个方法,用于返回八位组是否有效,而不是四次重复相同的条件。示例:

private boolean isValidOctet(int octet)
{
    return 0 <= octet && octet <= 255;
}
英文:

An else statement can always only belong to one if condition.

I assume you want to verify all ip address octets and print a message if one of them is not in the valid range. Only in the case that the adress is valid it should be printed (your current else directive).

I would suggest creating a boolean variable before running the checks. This boolean will tell if all four octets are correct.

boolean allOctetsValid = true;
if (!(a &gt;= 0 &amp;&amp; a &lt;= 255))
{
System.out.println(&quot;Octet 1 is incorrect&quot;);
allOctetsValid = false;
}
if (!(b &gt;= 0 &amp;&amp; b &lt;= 255))
{
System.out.println(&quot;Octet 2 is incorrect&quot;);
allOctetsValid = false;
}
if (!(c &gt;= 0 &amp;&amp; c &lt;= 255))
{
System.out.println(&quot;Octet 3 is incorrect&quot;);
allOctetsValid = false;
}
if (!(d &gt;= 0 &amp;&amp; d &lt;= 255))
{
System.out.println(&quot;Octet 4 is incorrect&quot;);
allOctetsValid = false;
}
if(allOctetsValid)
{
System.out.println(&quot;IP Address:&quot; + a + &quot;.&quot; + b + &quot;.&quot; + c + &quot;.&quot; + d);
}

This is only one possible solution.

Additional improvements:

The actual conditions could be simplified !(d &gt;= 0 &amp;&amp; d &lt;= 255) can also be written as 0 &lt;= d &amp;&amp; d &lt;= 255.

Consider creating a method that returns if an octet is valid isntead of duplicating the condition four times. Example:

private boolean isValidOctet(int octet)
{
return 0 &lt;= octet &amp;&amp; octet &lt;= 255;
}

huangapple
  • 本文由 发表于 2020年10月19日 04:43:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64418172.html
匿名

发表评论

匿名网友

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

确定