【问题标题】:I am struggling for when the user inputs aren't in range (works fine when IN range). I am aware of my mistakes, but I am having trouble fixing them当用户输入不在范围内(在范围内时工作正常)时,我正在努力。我知道我的错误,但我无法修复它们
【发布时间】:2021-12-08 17:47:06
【问题描述】:

/* 教师指导:

  • 布尔条件 - 八位字节和 IP 地址
  • 您将编写一个程序,该程序将接受来自用户的 4 个整数,检查它们是否有效
  • IP 地址范围编号 (1 - 255).(0 - 255).(0 - 255).(1 - 254)
  • 然后,如果它们是有效的,您将把它们放在 IP 地址的正确点分十进制表示法中。
  • 如果无效,告诉用户哪一个无效。
  • 样品运行: 请输入第一个八位字节: 125 请输入第二个八位字节: 10 请输入第三个八位字节: 52 请输入第四个八位字节: 10 IP地址:125.10.52.10
  • 样品运行: 请输入第一个八位字节: 125 请输入第二个八位字节: 10 请输入第三个八位字节: 520 请输入第四个八位字节: 10 第三个八位组不正确。

*/

我的代码(当用户放置一个不在范围内的八位字节时,它说它无效,我希望它这样做,但是当我不希望它们存在时,它也会打印给定的八位字节而不考虑有效性当一个或多个八位字节无效时打印。换句话说,我不希望它们在无效时打印。):

enter code here


      //Ask for octets 1-4.
  System.out.println("Please enter the first octet:");
  int oct1 = scan.nextInt();
      
  System.out.println("Please enter the second octet:");
  int oct2 = scan.nextInt();

  System.out.println("Please enter the third octet:");
  int oct3 = scan.nextInt();

  System.out.println("Please enter the fourth octet:");
  int oct4 = scan.nextInt();


      //Add boolean to determine the octets' validity
      
  boolean invalid = false;

  if (oct1 < 1 || oct1 > 255){
     System.out.print("Octet 1 is invalid.");
     invalid = true;
  }
  if (invalid)
     System.out.print(""+oct1+"");
  
  if (oct2 < 0 || oct2 > 255){
     System.out.print("Octet 2 is invalid.");
     invalid = true;
  }
  if (invalid)
     System.out.print(""+oct2+"");    
  
  if (oct3 < 0 || oct3 > 255){
     System.out.print("Octet 3 is invalid.");
     invalid = true;
  }
  if (invalid)
     System.out.print(""+oct3+"");          
      
  if (oct4 < 1 || oct4 > 254){
     System.out.print("Octet 4 is invalid.");
     invalid = true;
  }
  if (invalid)
     System.out.print(""+oct4+"");   
 

//如果每个八位字节都有效,则判断无效为假,并打印IP地址。

  if (invalid == false)
     System.out.print("IP address: "+oct1+"."+oct2+"."+oct3+"."+oct4+"");
      
      
      
      

}

}

【问题讨论】:

  • 作为起点,我建议不要在布尔值中使用 th 动词的否定形式。在这种情况下,如果您使用boolean valid,它将更具可读性,并且您将了解哪个ifs 是错误的。然后,如果您不想打印八进制数,请将if (invalid) 更改为if (!invalid)

标签: java boolean


【解决方案1】:

希望这能满足您的要求

import java.util.*;
public class Main
{
public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);

       System.out.println("Please enter the first octet:");
       int oct1 = scan.nextInt();

       System.out.println("Please enter the second octet:");
       int oct2 = scan.nextInt();

       System.out.println("Please enter the third octet:");
       int oct3 = scan.nextInt();

       System.out.println("Please enter the fourth octet:");
       int oct4 = scan.nextInt();

       boolean invalid = false;

       if (oct1 < 1 || oct1 > 255){
       System.out.print("Octet 1 is invalid.");
        invalid = true;
       }

        //if (invalid)
        //System.out.print(""+oct1+"");

        if(oct2 < 0 || oct2 > 255){
        System.out.print("Octet 2 is invalid.");
        invalid = true;
        }
        //if(invalid)
        //System.out.print(""+oct2+"");    

        if (oct3 < 0 || oct3 > 255){
        System.out.print("Octet 3 is invalid.");
        invalid = true;
        }
        //if (invalid)
        //System.out.print(""+oct3+"");          
  
        if (oct4 < 1 || oct4 > 254){
        System.out.print("Octet 4 is invalid.");
        invalid = true;
        }

       //if (invalid)
       //System.out.print(""+oct4+"");
 
       if (invalid == false)
       System.out.print("IP address: "+oct1+"."+oct2+"."+oct3+"."+oct4+"");
       }
      }

【讨论】:

  • 非常感谢。
猜你喜欢
  • 2016-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多