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

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

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

问题

  1. package com.company;
  2. import java.util.*;
  3. public class ExerciseB {
  4. public static void main(String[] args) {
  5. while (true) {
  6. System.out.println("Enter a digit or 'quit' to quit: ");
  7. Scanner userInput = new Scanner(System.in);
  8. if (userInput.hasNextInt()) {
  9. int intInput = userInput.nextInt();
  10. int i;
  11. int intCharSquare;
  12. ArrayList<Integer> valueCharSquare = new ArrayList<>();
  13. // Generating the square of each character of the inputted integer and adding it to the valueCharSquare ArrayList
  14. String stringOfIntInput = String.valueOf(intInput);
  15. for (i = 0; i <= stringOfIntInput.length() - 1; i++) {
  16. intCharSquare = (int) Math.pow((int) stringOfIntInput.charAt(i), 2);
  17. valueCharSquare.add(intCharSquare);
  18. }
  19. // Storing the first value of the valueCharSquare ArrayList into a variable
  20. int result = valueCharSquare.get(0);
  21. // Converting the value of result into a string
  22. String.valueOf(result);
  23. /* Obtaining the other values of the valueCharSquare ArrayList
  24. * and storing them into otherResult variable
  25. * then converting otherResult into a string
  26. * then concatenating the values of result and otherResults
  27. */
  28. for (i = 1; i <= valueCharSquare.size() - 1; i++) {
  29. int otherResults = valueCharSquare.get(i);
  30. String.valueOf(otherResults);
  31. result = result + otherResults;
  32. }
  33. // Printing out the new value of result
  34. System.out.println(result);
  35. // This happens if the user input is a string
  36. } else if (userInput.hasNext()) {
  37. String stringInput = userInput.nextLine();
  38. if (stringInput.equals("quit")) {
  39. break;
  40. } else {
  41. continue;
  42. }
  43. }
  44. }
  45. }
  46. }
英文:

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.

  1. package com.company;
  2. import java.util.*;
  3. public class ExerciseB {
  4. public static void main (String[] args) {
  5. while (true) {
  6. System.out.println(&quot;Enter a digit or &#39;quit&#39; to quit: &quot;);
  7. Scanner userInput = new Scanner(System.in);
  8. if (userInput.hasNextInt()) {
  9. int intInput = userInput.nextInt();
  10. int i;
  11. int intCharSquare;
  12. ArrayList&lt;Integer&gt; valueCharSquare = new ArrayList&lt;&gt;();
  13. //Generating the square of each character of the inputted integer and adding it to the valueCharSquare ArrayList
  14. String stringOfIntInput= String.valueOf(intInput);
  15. for (i = 0; i &lt;= stringOfIntInput.length() - 1; i++) {
  16. intCharSquare = (int) Math.pow((int) stringOfIntInput.charAt(i),2);
  17. valueCharSquare.add(intCharSquare);
  18. }
  19. // Storing the first value of the valueCharSquare ArrayList into a variable
  20. int result = valueCharSquare.get(0);
  21. //Converting the value of result into a string
  22. String.valueOf(result);
  23. /* Obtaining the other values of the valueCharSquare ArrayList
  24. * and storing them into otherResult variable
  25. * then converting otherResult into a string
  26. * then concatenating the values of result and otherResults
  27. */
  28. for (i = 1; i &lt;= valueCharSquare.size() - 1; i++) {
  29. int otherResults = valueCharSquare.get(i);
  30. String.valueOf(otherResults);
  31. result = result + otherResults;
  32. }
  33. //printing out new value of result
  34. System.out.println(result);
  35. // This happens if the user input is a string
  36. } else if (userInput.hasNext()) {
  37. String stringInput = userInput.nextLine();
  38. if (stringInput.equals(&quot;quit&quot;)) {
  39. break;
  40. } else {
  41. continue;
  42. }
  43. }
  44. }
  45. }

}

答案1

得分: 1

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

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

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

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

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

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

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

  1. System.out.println(squareDigits(13579)); // prints: 19254981
  2. System.out.println(squareDigits(-2468)); // prints: -4163664
  3. System.out.println(squareDigits(0)); // prints: 0
  4. 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:

确定