java - 如何允许用户将负数输入数组并更改起始索引?

标签 java arrays for-loop infinite-loop

我最近制作了一个应用程序,它读取 0 到 50 范围内的多个整数,并计算每个整数的输入次数。我还这样做了,以便它计算输入每个整数的次数。现在,我必须将范围从 0 到 50 更改为 -25 到 25。但是,当我输入负数时,我收到越界错误。

我一直在尝试数组声明,我只需要知道我需要做什么才能更改范围。这是我的代码:

import java.util.Scanner;
public class SixTwo
{
public static void main(String[] args) 
{
        Scanner sc = new Scanner(System.in);
        //int[] nums = new int[51];

        int[]occur = {-25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10,
        -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
        20, 21, 22, 23, 24, 25};
        int[] nums = {-25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10,
        -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
        20, 21, 22, 23, 24, 25};
        System.out.println("Enter an integer (00 to quit)");
        int integer = sc.nextInt();
        //for(int index = 0; index <= integer; index++ ){
        for(;;){
            System.out.println("Enter an integer");
            int n = sc.nextInt();
            if(n > 25){
                continue;
            }
            if(n == 00){
                break;
            }
            occur[n] += 1;
            nums[n] = n;
        }

        System.out.println("All Numbers entered:");
        for(int i = -25; i < nums.length; i++){
            System.out.print(nums[i] + " ");
        }
        System.out.println();
        System.out.println("All Occurrences: ");
        for(int i = -25; i < occur.length; i++){
            System.out.print(occur[i] + "  ");
        }
        sc.close();
}
}

最佳答案

如果您确实需要使用数组来完成任务,那么您可能需要更改逻辑,如下所示。我添加了内联注释来描述逻辑。

首先,您需要更改发生数组的初始化,如下所示,这将在应用程序启动时将所有出现次数初始化为零。

    int[] occur = { 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0 };

那么你需要将输入作为String,否则你将无法识别用户是否输入00退出程序。之后,可以使用 Integer.valueOf(input) 将验证字符串转换为 int .

那么你可能需要通过 n > 25 || n<-25 检查输入是否超出范围。如果超出范围,将打印错误消息并再次要求用户输入有效数字。

然后您需要将for循环初始参数从0更改为发生长度。不能对数组中的索引使用负值。我相信您能够理解代码中的其他逻辑。

您可以查看更新后的代码

import java.util.Scanner;

public class SixTwo {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    // int[] nums = new int[51];


    //You need to change the initial data of the occur array to zero
    int[] occur = { 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0 };

    int[] nums = { -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6,
            -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
            23, 24, 25 };

    for (;;) {
        System.out.println("Enter an integer between -25 to 25 (00 to quit)");
        //You need to take input as String other wise you will not be able to find whether user enter 00
        String input = sc.next();
        if(input.equals("00")){
            break;
        }

        int n = 0;

        try {
            n=Integer.valueOf(input);
        } catch (Exception e) {
            continue;
        }

        //You need to add check whether entered number out of range 
        if (n > 25 || n<-25) {
            System.out.println("Out of range");
            continue;
        }

        occur[25+n] = occur[25+n]+1;
    }

    System.out.println("All Occurrences: ");
    for (int i = 0; i < nums.length; i++) {
        System.out.println(nums[i] + ":"+occur[i]);
    }
    System.out.println();

    sc.close();
}
}

然后假设您输入的是 1,3,4,4,-25,0,0,00

All Occurrences: 
-25:1
-24:0
-23:0
-22:0
-21:0
-20:0
-19:0
-18:0
-17:0
-16:0
-15:0
-14:0
-13:0
-12:0
-11:0
-10:0
-9:0
-8:0
-7:0
-6:0
-5:0
-4:0
-3:0
-2:0
-1:0
0:2
1:1
2:0
3:1
4:2
5:0
6:0
7:0
8:0
9:0
10:0
11:0
12:0
13:0
14:0
15:0
16:0
17:0
18:0
19:0
20:0
21:0
22:0
23:0
24:0
25:0

关于java - 如何允许用户将负数输入数组并更改起始索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41071669/

相关文章:

c++ - 重载 + 运算符以添加数组

c++ - 可以在基于范围的 for 循环中使用模板化的开始/结束方法吗

java - Java 可以用来确定下载的持续时间吗?

java - 以编程方式解决数独

java - Apache Camel : how to split the file and send to multiple end points

python - 迭代由不同数据帧组成的列表

在循环中跨列替换多个值

java - Android 应用程序- 没有此类方法异常

ios - 调用在类中初始化的字典数组

python - 按一列然后另一列(作为子集)对 numpy 进行排序,同时保留行顺序