简体   繁体   English

我正在尝试为用户创建一个程序来输入 5 个数字并检查它们在数组中是否连续,但我遇到了问题

[英]I'm trying to create a program for the user to enter 5 numbers and check if they are consecutive in an array but I have a problem

I think I've managed to make most of the program but ran into a problem when entering the numbers at the input stage to later be sorted.我想我已经设法完成了大部分程序,但是在输入阶段输入数字以供以后排序时遇到了问题。 Could someone help me to understand what I need to fix at the input stage or how I need to declare my variables in order for them to be sorted in the array later on in the program?有人可以帮助我了解我需要在输入阶段修复什么,或者我需要如何声明我的变量以便稍后在程序中对数组进行排序?

This is the code:这是代码:

import java.util.*;

public class Main{
    int a;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Please enter your numbers to be sorted in the array: ");
        a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int d = sc.nextInt();
        int e = sc.nextInt();    
    }

    public static boolean checkConsecutive(int[] A)   {
        int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;

        // to find the largest and smallest numbers in the array
        for (int i: A) {
            if (i < min) { min = i; }
            if (i > max) { max = i; }
        }

        // in order for an array to contain consecutive integers, the difference
        // between maximum and element element in it should be exactly n-1
        if (max - min != A.length - 1) {
            return false;
        }

        // create an empty set (we can also use a visited array)
        Set<Integer> visited = new HashSet<>();

        // traverse the array and checks if each element appears only once
        for (int i: A)
        {
            // if element is seen before, return false
            if (visited.contains(i)) {
                return false;
            }

            // mark element as seen
            visited.add(i);
        }

        // we reach here when all elements in the array are distinct
        return true;
    }

    // Check if an array is formed by consecutive integers
    public static void printInfo(String[] args)    { 
        int[] A = { a, b, c, d, e};

        if (checkConsecutive(A)) {
            System.out.print("Array contains consecutive integers");
        } else {
            System.out.print("Array do not contain consecutive integers");
        }
    }
}

The problem you had that the variables of a, b, c, d, e should declared globally and calling the printInfo() .您遇到的问题是a, b, c, d, e的变量应该全局声明并调用printInfo()

Otherwise you can create the following ask for the size of the array and enter the number and check if the array contains consecutive integer or not否则,您可以创建以下询问数组的大小并输入数字并检查数组是否包含连续整数

import java.util.*;

public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Please enter the size of the array: ");
    int size = Integer.parseInt(sc.nextLine());
    int[] A = new int[size];
    for (int i = 0; i < size; i++) {
      A[i] = sc.nextInt();
    }

    if (checkConsecutive(A)) {
      System.out.print("Array contains consecutive integers");
    } else {
      System.out.print("Array does not contain consecutive integers");
    }
    sc.close();
  }

  public static boolean checkConsecutive(int[] A) {
    int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;

    // to find the largest and smallest numbers in the array
    for (int i : A) {
      if (i < min) {
        min = i;
      }
      if (i > max) {
        max = i;
      }
    }

    // in order for an array to contain consecutive integers, the difference
    // between maximum and element element in it should be exactly n-1
    if (max - min != A.length - 1) {
      return false;
    }

    // create an empty set (we can also use a visited array)
    Set<Integer> visited = new HashSet<>();

    // traverse the array and checks if each element appears only once
    for (int i : A) {
      // if element is seen before, return false
      if (visited.contains(i)) {
        return false;
      }

      // mark element as seen
      visited.add(i);
    }

    // we reach here when all elements in the array are distinct
    return true;
  }
}

, output , 输出

Please enter the size of the array: 5
1 2 3 4 5
Array contains consecutive integers
Process finished with exit code 0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查输入是否为 integer? 当用户输入 7 程序结束时,我对 output 有问题 - How do I check if the input is an integer? I have issue with the output when the user enter 7 the program end 如何创建 java 程序来查找数组中连续数字的数量? - How can I create a java program to find the amount of consecutive numbers in an array? 我正在尝试创建一个对数字求平均的程序,但无法弄清楚如何将用户输入的数字加在一起 - I am trying to create a program that averages numbers, but cannot figure out how to add the user inputted numbers together 我正在尝试创建一个应要求用户输入行数和列数的数组 - I am trying to create an Array that should request user to enter the number of rows and columns 我正在尝试检查是否存在某些文件。 为什么我的程序不能让用户输入文件? - I am trying to check and see if some files exist. Why won't my program let the user enter a file? 我正在尝试创建一个数字键盘,该键盘具有一个清除按钮和一个顶部,上面显示了单击的数字。 到目前为止,我有 - I'm trying to create a numeric keypad that has a clear button and a piece on top that shows the numbers clicked on. So far I have 我正在尝试创建导航抽屉,但出现此错误 - I'm trying to create navigation drawer but I have this error 我正在尝试编写一个递归Java程序来创建Serpinski三角形 - I'm trying to write a recursive java program to create the Serpinski triangle 我正在尝试获取数组并强制转换意图,但程序不断崩溃 - I'm trying to get an array and cast an intent but program keeps crashing 如何使用连续数字填充数组 - How do I fill an array with consecutive numbers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM