英文:
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("Please enter the first octet:");
int a = scan.nextInt();
System.out.println("Please enter the second octet:");
int b = scan.nextInt();
System.out.println("Please enter the third octet:");
int c = scan.nextInt();
System.out.println("Please enter the fourth octet:");
int d = scan.nextInt();
if (!(a >= 0 && a <= 255))
{
System.out.println("Octet 1 is incorrect");
}
if (!(b >= 0 && b <= 255))
{
System.out.println("Octet 2 is incorrect");
}
if (!(c >= 0 && c <= 255))
{
System.out.println("Octet 3 is incorrect");
}
if (!(d >= 0 && d <= 255))
{
System.out.println("Octet 4 is incorrect");
}
else
{
System.out.println("IP Address:" + a + "." + b + "." + c + "." + 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 >= 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 Address:" + a + "." + b + "." + c + "." + d);
}
This is only one possible solution.
Additional improvements:
The actual conditions could be simplified !(d >= 0 && d <= 255)
can also be written as 0 <= d && d <= 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 <= octet && octet <= 255;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论