[英]How do I check if the input is an integer? I have issue with the output when the user enter 7 the program end
[英]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
我想我已经设法完成了大部分程序,但是在输入阶段输入数字以供以后排序时遇到了问题。 有人可以帮助我了解我需要在输入阶段修复什么,或者我需要如何声明我的变量以便稍后在程序中对数组进行排序?
这是代码:
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");
}
}
}
您遇到的问题是a, b, c, d, e
的变量应该全局声明并调用printInfo()
。
否则,您可以创建以下询问数组的大小并输入数字并检查数组是否包含连续整数
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;
}
}
, 输出
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.