如何在Java中创建一个范围从-25到25的数字数组。

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

How to create an array of numbers that range from -25 to 25 in Java

问题

public static void main(String[] args) {

    int[] array = new int[51];
    Scanner s = new Scanner(System.in);

    for (int i = 0; i < 6; i++){

        System.out.print("Enter a number between -25 and 25: ");
        int n = s.nextInt(); 
        while (n < -25 || n > 25) 
        { 
            System.out.print("Number is out of bounds, please enter a number between -25 and 25: "); 
            n = s.nextInt(); 
        } 
        array[n]++; 
    }
    for (int i = 0; i < array.length; i++)
    { 
        if (array[i] > 0) 
            System.out.println("Number of " + i + "'s" + ": " + array[i]);                            

    }
}
英文:

I'm trying to create a program that asks a user to input an arbitrary number of integers that are in the range of -25 to 25 inclusive and counts how many occurrences of each are entered. I also need to indicate the end of the input by entering a value outside of the range, then print the number of occurrences that were entered one or more times. I was able to do this with numbers ranging from 0 to 50, but once I entered a negative number, it gave me an Array index out of bounds error.

public static void main(String[] args) {
    
    int[] array = new int[51];
       Scanner s = new Scanner(System.in);

      for (int i = 0; i &lt; 6; i++){
        
       System.out.print(&quot;Enter a number between -25 and 25: &quot;);
       int n = s.nextInt(); 
       while (n &lt; -25 || n &gt; 25) 
        { 
         System.out.print(&quot;Number is out of bounds, please enter a number between -25 and 25: &quot;); 
           n = s.nextInt(); 
         } 
          array[n]++; 
         }
        for (int i = 0; i &lt; array.length; i++)
        { 
         if (array[i] &gt; 0) 
            System.out.println(&quot;Number of &quot; + i + &quot;&#39;s&quot; + &quot;: &quot; + array[i]);                            
         
      }
}

答案1

得分: 4

array 指的是使用 new int[51] 创建的数组对象。

这将是一个包含有效索引 [0][50] 的整数数组。负索引是无效的;不适用于这个数组,也不适用于其他任何数组(在Java中,所有数组从0开始。没有例外。这是无法改变的)。

那么,在你的代码中,例如 array[-20] 是什么意思呢?看起来像是:「与用户输入的 -20 相关联的值」,但是「用户输入」并不同于「Java 数组索引」。负的 Java 数组索引根本不存在(毕竟所有索引都从0开始)。对于Java来说,array[-20] 的意思是:如果你尝试这样做,就抛出 ArrayIndexOutOfBoundsException 异常。

你可能想要将输入范围(从 -25+25)映射到 Java 的数组索引范围(从 0+50,方便的是,两个范围都恰好包含了51个数字)。这应该非常简单的数学操作。在所有相关的地方都应用这个操作(每当你将用户输入用作索引时,应用「将用户输入映射到数组索引」的转换,每当你有一个索引并且需要将其显示给用户时,应用相反的转换)。

显然这是作业,所以我会将它留给读者来完成:什么是这个转换函数,你会如何在这里应用它。

英文:

array refers to an array object created with new int[51].

That would be an int array with valid indices [0] all the way up to and including [50]. A negative index isn't valid; not for this array and not for any other (in java, all arrays begin at 0. Period. You can't change this).

So, what does, say, array[-20] mean, in your code? It looks like it means: "The value associated with user input -20", but 'user input' is not the same as 'java array index'. A negative java array index just isn't a thing (they all start at 0, after all). To java, array[-20] means: throw an ArrayIndexOutOfBoundsException if you try this.

Presumably you want to map the input range (from -25 all the way to +25) onto java's array index range (from 0 all the way to +50 - conveniently, both ranges are exactly 51 numbers large). That should be easy enough. It's a very simple mathematical operation. Apply that in all places where relevant (whenever you use user input as an index, apply the 'map user input to array index' transformation, and whenever you have an index and need to show it to the user, apply the reverse transformation).

It's clearly homework, so I'll leave it as an exercise to the reader: What is that transformation function and how would you apply it here.

huangapple
  • 本文由 发表于 2020年10月23日 00:35:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64486638.html
匿名

发表评论

匿名网友

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

确定