[英]Take an integer array and print only the numbers that are in consecutive orders of 3 in java. If no number is found, print none
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n,num;
System.out.println("Enter Size: ");
n = input.nextInt();
int arr[] = new int [n];
System.out.println(" Enter numbers: ");
num = input.nextInt();
for (int i = 0; i <=3; i++)
{
int j;
for (j = 0; j <= 3; j++)
if (arr[i] == arr[j])
break;
if (i == j)
System.out.print( arr[i] + " ");
}
}
How do I solve this?我该如何解决这个问题?
For example:例如:
Input: 1232221144433输入:1232221144433
Expected output: 2 4预期 output:2 4
Convert the input number to an integer array and then iterate over this array to find numbers occurring thrice consecutively将输入数字转换为 integer 数组,然后遍历该数组以查找连续出现三次的数字
private static int[] convertToArray(long input) {
String temp = Long.toString(input);
int[] arr = new int[temp.length()];
for (int i = 0; i < temp.length(); i++)
arr[i] = temp.charAt(i) - '0';
return arr;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter numbers: ");
long input = scanner.nextLong();
int[] numbers = convertToArray(input);
for(int i = 0; i < numbers.length - 2; i ++) {
if(numbers[i] == numbers[i+1] && numbers[i] == numbers[i+2]) {
System.out.println(numbers[i]);
}
}
scanner.close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.