我如何生成一个整数的各个位数的平方

huangapple go评论63阅读模式
英文:

How can I generate the squares of the digits of an integer

问题

package com.company;
import java.util.*;

public class ExerciseB {
  public static void main(String[] args) {
    while (true) {
        System.out.println("Enter a digit or 'quit' to quit: ");
        Scanner userInput = new Scanner(System.in);
        if (userInput.hasNextInt()) {
            int intInput = userInput.nextInt();
            int i;
            int intCharSquare;
            ArrayList<Integer> valueCharSquare = new ArrayList<>();

            // Generating the square of each character of the inputted integer and adding it to the valueCharSquare ArrayList
            String stringOfIntInput = String.valueOf(intInput);
            for (i = 0; i <= stringOfIntInput.length() - 1; i++) {
                intCharSquare = (int) Math.pow((int) stringOfIntInput.charAt(i), 2);
                valueCharSquare.add(intCharSquare);
            }
            
            // Storing the first value of the valueCharSquare ArrayList into a variable
            int result = valueCharSquare.get(0);

            // Converting the value of result into a string
            String.valueOf(result);

            /* Obtaining the other values of the valueCharSquare ArrayList
             * and storing them into otherResult variable
             * then converting otherResult into a string
             * then concatenating the values of result and otherResults
             */
            for (i = 1; i <= valueCharSquare.size() - 1; i++) {
                int otherResults = valueCharSquare.get(i);
                String.valueOf(otherResults);
                result = result + otherResults;
            }
            
            // Printing out the new value of result
            System.out.println(result);

        // This happens if the user input is a string
        } else if (userInput.hasNext()) {
            String stringInput = userInput.nextLine();
            if (stringInput.equals("quit")) {
                break;
            } else {
                continue;
            }
        }
    }
  }
}
英文:

I am trying to write a command line program that accepts inputs. If the input is an integer , it squares every digit of the number and concatenate them but if the input is not an integer then it asks the user to input a digit. But the output is wrong. Please Check the code.

package com.company;
import java.util.*;
public class ExerciseB {
public static void main (String[] args) {
while (true) {
System.out.println(&quot;Enter a digit or &#39;quit&#39; to quit: &quot;);
Scanner userInput = new Scanner(System.in);
if (userInput.hasNextInt()) {
int intInput = userInput.nextInt();
int i;
int intCharSquare;
ArrayList&lt;Integer&gt; valueCharSquare = new ArrayList&lt;&gt;();
//Generating the square of each character of the inputted integer and adding it to the valueCharSquare ArrayList
String stringOfIntInput= String.valueOf(intInput);
for (i = 0; i &lt;= stringOfIntInput.length() - 1; i++) {
intCharSquare = (int) Math.pow((int) stringOfIntInput.charAt(i),2);
valueCharSquare.add(intCharSquare);
}
// Storing the first value of the valueCharSquare ArrayList into a variable
int result = valueCharSquare.get(0);
//Converting the value of result into a string
String.valueOf(result);
/* Obtaining the other values of the valueCharSquare ArrayList
* and storing them into otherResult variable
* then converting otherResult into a string
* then concatenating the values of result and otherResults
*/
for (i = 1; i &lt;= valueCharSquare.size() - 1; i++) {
int otherResults = valueCharSquare.get(i);
String.valueOf(otherResults);
result = result + otherResults;
}
//printing out new value of result
System.out.println(result);
// This happens if the user input is a string
} else if (userInput.hasNext()) {
String stringInput = userInput.nextLine();
if (stringInput.equals(&quot;quit&quot;)) {
break;
} else {
continue;
}
}
}
}

}

答案1

得分: 1

你应该为整数的每个数字编写一个单独的方法来计算其平方,例如类似以下的代码:

static String squareDigits(int number) {
String numberString = Integer.toString(number);
StringBuilder buf = new StringBuilder();
if (number < 0)
buf.append('-');
for (int i = (number < 0 ? 1 : 0); i < numberString.length(); i++) {
int digit = Character.digit(numberString.charAt(i), 10);
buf.append(digit * digit);
}
return buf.toString();
}

然后,你可以独立于用户输入/输出逻辑来测试它:

System.out.println(squareDigits(13579));       // 打印:19254981
System.out.println(squareDigits(-2468));       // 打印:-4163664
System.out.println(squareDigits(0));           // 打印:0
System.out.println(squareDigits(1999999999));  // 打印:1818181818181818181
英文:

You should write a separate method for squaring the digits of an integer, e.g. something like this:

static String squareDigits(int number) {
String numberString = Integer.toString(number);
StringBuilder buf = new StringBuilder();
if (number &lt; 0)
buf.append(&#39;-&#39;);
for (int i = (number &lt; 0 ? 1 : 0); i &lt; numberString.length(); i++) {
int digit = Character.digit(numberString.charAt(i), 10);
buf.append(digit * digit);
}
return buf.toString();
}

Then you can test it independently of your user input/output logic:

System.out.println(squareDigits(13579));       // prints: 19254981
System.out.println(squareDigits(-2468));       // prints: -4163664
System.out.println(squareDigits(0));           // prints: 0
System.out.println(squareDigits(1999999999));  // prints: 1818181818181818181

huangapple
  • 本文由 发表于 2020年8月31日 19:41:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63670088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定