英文:
Lucky number with User Input
问题
以下是您要求的代码的翻译:
public class Tester {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入一个数字:");
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("数字:" + number + " 是幸运数字");
}
else {
System.out.println("糟糕!不是幸运数字");
}
scanner.close();
}
}
英文:
I'm facing troubles solving the following question: I suppose to get the user to input a number and check if it is a lucky number. A lucky number is the sum of squares of even-positioned digit (starting from the second position) is a multiple of 7.
Following is the example of my codes, when i run the program it will stuck at user input, please advise how do i get it to run:
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();
}
}
答案1
得分: 1
我认为罪魁祸首是下面的循环:
for (int i=0; i<count; i++) {
array[i] = scanner.nextInt();
}
我认为你的意图是将每个数字都放入数组中。然而,你正在从扫描器(在这种情况下是用户输入)中获取count
次输入。
虽然有几种方法可以将数字的数量和每个数字都放入数组中。我将为您提供两种方法。同时,我注意到在输入整数时没有进行验证(例如负数等),我暂时不会考虑这些情况。
方法一:更正您的for循环
您只需使用一个公式获取数字的第i位。
for (int i=1; i<=count; i++) {
array[i] = (int) (number / Math.pow(10, count-i)) % 10;
}
方法二:使用流将数字转换为字符串,然后再转回数字
List<Integer> digits = Arrays.stream(number.toString().split("")).map(
digitChar -> Integer.parseInt(digitChar)
).collect(Collectors.toList());
注意:
您需要导入java.util.Arrays
和java.util.stream.Collectors
这两个类。
英文:
I believe the culprit is the below loop:
for (int i=0; i<count; i++) {
array[i] = scanner.nextInt();
}
I think your intention was to get each of the digits into an array. However, you are getting an input from the scanner (which in this case is the user input) for count
number of times.
While there are several ways of getting the number of digits and each digit into an array. I'm going to give you two ways. Also, I see no validations on the input integer (such as negative numbers, etc.) and I am going to ignore them right now.
Approach 1: Your for loop corrected
You just get the ith digit of the number using a formula.
for (int i=1; i<=count; i++) {
array[i] = (int) (number / Math.pow(10, count-i)) % 10;
}
Approach 2: Converting the numbers to String and back using streams
List<Integer> digits = Arrays.toStream(number.toString().split("")).map(
digitChar -> Integer.parseInt(digitChar)
).collect(Collectors.toList());
Note:
You need to import the classes java.util.Arrays
and java.util.stream.Collectors
答案2
得分: 0
如果您想要偶数位上的数字,可以在 while 循环中直接获取。
while (number != 0) {
if (count % 2 != 0) {
int value = number % 10; // 偶数位上的数字
// 根据需要处理这个值
}
number /= 10;
++count;
}
如果您想将数字转换为数字数组,则首先使用对数函数找到数字的位数,然后将其存储在逆序数组中。
int noOfDigits = (int) Math.floor(Math.log10(number) + 1); // 找到位数
int[] array = new int[noOfDigits];
while (--noOfDigits >= 0) {
array[noOfDigits] = number % 10; // 逆序存储每个数字
number /= 10;
}
我不知道下面的代码是否对您的核心逻辑有帮助,但我也记录下来。
如果您想要计算作为数组表示的数字中偶数位上数字的平方和,则可以使用以下代码。
int sum = 0;
for (int i = 1; i < array.length; i += 2) {
sum += array[i] * array[i];
}
if (sum % 7 == 0) {
// 打印数字是幸运的
} else {
// 打印数字不是幸运的
}
英文:
If you want even positioned digits,then you can directly get it in while loop.
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;
}
If you want to convert the number into an digit array,then first find number of digits using log function and then store it array in reverse order.
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;
}
I don't know below code will be helpful for your core logic,yet I record this too.
If you want Sum of Squares of even positioned digits in number which is represented as array, then you can use below code.
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
得分: 0
如果我正确理解你的描述,这里是一个能够实现你想要的功能的程序:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("输入一个数字: ");
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("和 = %d + %d^2 = %d\n", sum, d, sum + d * d);
sum += d * d;
}
}
if (sum % 7 == 0) {
System.out.printf("该数字:%d 是一个幸运数字(%d = 7 * %d)", number, sum, sum / 7);
} else {
System.out.println("糟糕!不是幸运数字");
}
scanner.close();
}
一个幸运的结果:
输入一个数字: 123456
和 = 0 + 5^2 = 25
和 = 25 + 3^2 = 34
和 = 34 + 1^2 = 35
该数字:123456 是一个幸运数字(35 = 7 * 5)
英文:
If I understand your description correctly, here's a program that does what you want:
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();
}
A lucky result:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论