【问题标题】:How to deal with IndexOutOfBounds error in arrays in such a case?在这种情况下如何处理数组中的 IndexOutOfBounds 错误?
【发布时间】:2023-01-06 00:06:53
【问题描述】:

我需要为我的 OOP 实验室创建一个方法,详细信息如下:

ThreeWayLamp 类模拟灯的行为,该灯使用 三通灯泡。这些灯泡有四种可能的状态:关闭、低光、中等 光,和高光。每次启动开关时,灯泡都会转到下一个 状态(从高,下一个状态是关闭,从关闭到低等)。三路灯 类有一个名为 switch() 的方法,它接受一个 int 参数 指示开关被激活的次数。 (你需要扔一个 如果它是负数则例外)。 Switch() 方法应该简单地打印出来 System.out 一条消息,指示灯泡改变后的状态。

public class ThreeWayLamp {

public String[] States = {"Off","LowLifght", "MediumLifght", "HighLight"}; // an array of the 4 states
    
    
    public void Switch(int switchState){
    
      //used an if condition to determine what to print based on the parameter switchState

        if ((switchState <= States.length) && (switchState >= 0)){
            System.out.println(States[switchState]);
            
        }else if (switchState < 0 ){
            System.out.println("Wrong input, try again with diffrent number");
           
        }else if (switchState >= States.length){
            
        } //This condition is the issue, how to form a condition that will solve this problem
    }

如果参数大于数组的长度,则会发生错误,因此问题是如何形成一个条件,使数组在到达最后一个索引时再次围绕自身循环。 例如,如果输入为 5,则该方法应打印 LowLight。 是否存在可以解决此问题的可能条件或功能,或者我应该更改代码的整个结构?

【问题讨论】:

  • “如果参数大于数组的长度,则会发生错误” - 不在您发布的代码中。哪一行抛出错误?您获得的唯一数组索引位于 if 语句的主体内,该语句验证 switchState 对数组有效...
  • 使用模数。 5 % 416 % 42,等等
  • 问题是最后一个条件,如果输入大于数组长度如何处理数组,我无法形成不会导致错误的条件
  • 您的代码错误,因此当代码仍然错误时,无需“修复”其他问题。您的参数与分配的参数描述具有不同的含义,并且您的代码假定灯没有启动,这就是为什么您假设输入 5 始终为“LowLight”,这是错误的,它显然取决于先前的状态。

标签: java arrays


【解决方案1】:

您可以使用模运算符解决此问题:

System.out.println(States[switchState % States.length]);

【讨论】:

    【解决方案2】:

    您可以尝试使用 mod 来重复 String 数组的索引,而不是使用小于条件。

    试试下面的代码:

    public class ThreeWayLamp {
    
    public String[] States = {"Off","LowLifght", "MediumLifght", "HighLight"}; // an array of the 4 states
        
        
        public void Switch(int switchState){
    
            
        if ((switchState > 0){
            System.out.println(States[switchState % States.length]); // states.length = 4 in your case
        } else {
            System.out.println("Wrong input, try again with diffrent number");
        }
        
               
        }
    

    【讨论】:

      【解决方案3】:

      使用模数可以确保索引始终在数组的大小范围内

      4 % 4 = 0
      5 % 4 = 1
      6 % 4 = 2
      7 % 4 = 3
      8 % 4 = 0
      9 % 4 = 1
      ...
      
      public static void Switch(int switchState){
          if ((switchState < States.length) && (switchState >= 0)){
              System.out.println(States[switchState]);
          } else if (switchState < 0 ){
              System.out.println("Wrong input, try again with diffrent number");
          } else {
              Switch(switchState % 4);
              // or
              // System.out.println(States[switchState % 4]);
          }
      }
      

      此外,方法名称应以小写字母开头

      【讨论】:

        猜你喜欢
        • 2012-01-11
        • 2014-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多