Java 用户输入的幸运数字

Java 用户输入的幸运数字,java,control-structure,Java,Control Structure,我在解决以下问题时遇到了困难:我假设让用户输入一个数字并检查它是否是幸运数字。幸运数字是偶数位(从第二个位置开始)的平方和是7的倍数 以下是我的代码示例,当我运行程序时,它将卡在用户输入上,请告知如何运行: public class Tester { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println

我在解决以下问题时遇到了困难:我假设让用户输入一个数字并检查它是否是幸运数字。幸运数字是偶数位(从第二个位置开始)的平方和是7的倍数

以下是我的代码示例,当我运行程序时,它将卡在用户输入上,请告知如何运行:

public class Tester {

public static void main(String[] args) {
            
    Scanner scanner = new Scanner(System.in);

    System.out.println("Input a number: ");
    int number = scanner.nextInt();
    
    int count = 0;
    while(number!=0) {
        number/=10;
        ++count;
    }
    
    int[] array = new int[count];
    int sum = 0;
    
    for (int i=0; i<count; i++) {
        array[i] = scanner.nextInt();
    }

    for (int i=0; i<count; i++) {
        if(array[i]%2==0) {
            sum+=(array[i]*array[i]);
        }
        else {
            continue;
        }
    }
    
    if (sum%7==0) {
        System.out.println("The number: " +number+ "is a Lucky number");
    }
    else {
        System.out.println("Oops! Not a Lucky number");
    }
    
    scanner.close();
}
}
公共类测试器{
公共静态void main(字符串[]args){
扫描仪=新的扫描仪(System.in);
System.out.println(“输入一个数字:”);
int number=scanner.nextInt();
整数计数=0;
while(数字!=0){
数目/=10;
++计数;
}
int[]数组=新的int[count];
整数和=0;

对于(int i=0;i我认为罪魁祸首是以下循环:

for (int i=0; i<count; i++) {
  array[i] = scanner.nextInt();
}
方法2:使用流将数字转换为字符串并返回

List<Integer> digits = Arrays.toStream(number.toString().split("")).map(
  digitChar -> Integer.parseInt(digitChar)
).collect(Collectors.toList());
List digits=Arrays.toStream(number.toString().split(“”).map(
digitChar->Integer.parseInt(digitChar)
).collect(Collectors.toList());
注意:
您需要导入类
java.util.Arrays
java.util.stream.collector

如果您想要甚至定位的数字,那么您可以直接在while循环中获取它

while(number!=0) {
    if(count%2 !=0){
      int value = number %10;   // even positioned values
      // Do whatever you need to do with this value
    }
    number/=10;
    ++count;
}
如果要将数字转换为数字数组,请首先使用log函数查找数字,然后按相反顺序将其存储到数组中

int noOfDigits =(int) Math.floor(Math.log10(number)+1); // Finding No of digits
int[] array = new int[noOfDigits];

while(--noOfDigits>=0){
    array[noOfDigits] = number/10;         // storing every digits in reverse order
    number%=10;
}
我不知道下面的代码对您的核心逻辑是否有帮助,但我也记录了这一点

如果您想要以数组形式表示的数字中偶数位的平方和,则可以使用下面的代码

int sum = 0;
for (int i=1; i<array.length; i+=2) {
    sum += array[i] * array[i];
}

if (sum%7==0) {
    // print number is lucky
}
else {
   // print number is not lucky
}
int和=0;

对于(int i=1;i如果我正确理解了您的描述,这里有一个程序可以满足您的需要:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Input a number: ");
    System.out.flush();
    int number = scanner.nextInt();

    int count = 0;
    int n = number;
    int sum = 0;
    while(n!=0) {
        int d = n % 10;
        n/=10;
        ++count;
        if (count % 2 == 0) {
            System.out.printf("sum = %d + %d^2 = %d\n", sum, d, sum + d * d);
            sum += d * d;
        }
    }

    if (sum%7==0) {
        System.out.printf("The number: %d is a Lucky number (%d = 7 * %d)", number, sum, sum / 7);
    }
    else {
        System.out.println("Oops! Not a Lucky number");
    }

    scanner.close();
}
幸运的结果是:

Input a number: 123456
sum = 0 + 5^2 = 25
sum = 25 + 3^2 = 34
sum = 34 + 1^2 = 35
The number: 123456 is a Lucky number (35 = 7 * 5)

这里发生了什么:
array[i]=scanner.nextInt();
大多数幸运数字问题要求一个数字,但您似乎要求除此之外的其他用户输入(可能是无意的),这就是为什么您的程序看起来“卡住”的原因。您向程序传递了什么输入?您甚至需要定位数字吗?
Input a number: 123456
sum = 0 + 5^2 = 25
sum = 25 + 3^2 = 34
sum = 34 + 1^2 = 35
The number: 123456 is a Lucky number (35 = 7 * 5)