【问题标题】:Lucky number with User Input带有用户输入的幸运号码
【发布时间】:2020-09-27 01:09:28
【问题描述】:

我在解决以下问题时遇到了麻烦:我想让用户输入一个数字并检查它是否是一个幸运数字。幸运数字是偶数位(从第二位开始)的平方和是 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();
}
}

【问题讨论】:

  • 这里发生了什么:array[i] = scanner.nextInt(); 大多数幸运数字问题要求一个数字,但您似乎要求除此之外的其他用户输入(可能是无意的),这就是您的程序出现的原因得到“卡住”。
  • 你向程序传递了什么输入?
  • 你需要甚至定位的数字吗?

标签: java control-structure


【解决方案1】:

我相信罪魁祸首是下面的循环:

for (int i=0; i<count; i++) {
  array[i] = scanner.nextInt();
}

我认为您的意图是将每个数字放入一个数组中。但是,您从扫描仪(在本例中为用户输入)获得了count 次的输入。

虽然有几种方法可以将位数和每个数字放入数组中。我会给你两种方法。另外,我看不到输入整数的验证(例如负数等),我现在将忽略它们。

方法 1:更正你的 for 循环

您只需使用公式获得数字的第 i 位。

for (int i=1; i<=count; i++) {
  array[i] = (int) (number / Math.pow(10, count-i)) % 10;
}

方法 2:使用流将数字转换为字符串并返回

List<Integer> digits = Arrays.toStream(number.toString().split("")).map(
  digitChar -> Integer.parseInt(digitChar)
).collect(Collectors.toList());

注意: 您需要导入类java.util.Arraysjava.util.stream.Collectors

【讨论】:

    【解决方案2】:

    如果你想要甚至定位的数字,那么你可以直接在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
    }
    

    【讨论】:

      【解决方案3】:

      如果我正确理解了您的描述,这里有一个程序可以满足您的需求:

      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-24
        • 2013-08-26
        • 2015-11-04
        • 2018-02-03
        • 2013-01-06
        • 1970-01-01
        • 2016-01-03
        相关资源
        最近更新 更多