猿问

Java 对整数求和

我想从用户那里读取一个数字,然后将输入的数字的最后七位相加。做这个的最好方式是什么?这是我的代码,但不幸的是它不起作用:


class ersteAufgabe {

  public static void main (String[] args)

  {

              Scanner s = new Scanner(System.in);

              double [] a = new double[10];

              for (int i = 0;i<10;i++)

              {

                  a[i]=s.nextInt();


                 }

              s.close();

              System.out.println(a[0]);

  }

}

我只想读取一个数字并将其用作数组。只是现在他希望我有 10 个输入。


繁星淼淼
浏览 132回答 2
2回答

烙印99

public static int lastDigitsSum(int total) {&nbsp; &nbsp; try (Scanner scan = new Scanner(System.in)) {&nbsp; &nbsp; &nbsp; &nbsp; String str = scan.next();&nbsp; &nbsp; &nbsp; &nbsp; int count = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = str.length() - 1, j = 0; i >= 0 && j < total; i--, j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Character.isDigit(str.charAt(i)))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count += str.charAt(i) - '0';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException("Input is not a number: " + str);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return count;&nbsp; &nbsp; }}

茅侃侃

首先,您必须识别输入的值是否为数字并且至少包含 7 位数字。除非您必须输出错误消息。将输入的值转换为字符串并使用类 Character.isDigit(); 检查字符是否为数字。然后,您可以使用 String 类中的一些方法,例如 substring(..)。最后使用错误/有效值进行单元测试,以查看您的代码是否健壮。完成后使用 finally { br.close() } 关闭 BufferedReader 和 Resources。将您的代码推送到方法中并使用实例类 erste-Aufgabe(第一个练习)。当您真正完成时,将 JFrame 用于 GUI 应用程序。private static final int SUM_LAST_DIGITS = 7;public void minimalSolution() {&nbsp; &nbsp; String enteredValue = "";&nbsp; &nbsp; showInfoMessage("Please enter your number with at least " + SUM_LAST_DIGITS + " digits!");&nbsp; &nbsp; try (Scanner scan = new Scanner(System.in)) {&nbsp; &nbsp; &nbsp; &nbsp; enteredValue = scan.next();&nbsp; &nbsp; &nbsp; &nbsp; if (enteredValue.matches("^[0-9]{" + SUM_LAST_DIGITS + ",}$")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showInfoMessage(enteredValue, lastDigitsSum(enteredValue));&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showErrorMessage(enteredValue);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } catch(Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; showErrorMessage(e.toString());&nbsp; &nbsp; }}public int lastDigitsSum(String value) {&nbsp; &nbsp; int count = 0;&nbsp; &nbsp; for (int i = value.length() - 1, j = 0; i >= 0 && j < SUM_LAST_DIGITS; i--, j++)&nbsp; &nbsp; &nbsp; &nbsp; count += value.charAt(i) - '0';&nbsp; &nbsp; return count;}public void showInfoMessage(String parMessage) {&nbsp; &nbsp; System.out.println(parMessage);}public void showInfoMessage(String parValue, int parSum) {&nbsp; &nbsp; System.out.println("Your entered value: [" + parValue + "]");&nbsp; &nbsp; System.out.println("The summed value of the last 7 digits are: [" + parSum + "]");}public void showErrorMessage(String parValue) {&nbsp; &nbsp; System.err.println("Your entered value: [" + parValue + "] is not a valid number!");}
随时随地看视频慕课网APP

相关分类

Java
我要回答