我正在尝试创建一个程序来查找 2 个数字之间所有连续数字的总和,我已经创建了一个代码,但我收到这些错误。 任何帮助或摆脱这些错误的方法将不胜感激。 谢谢
Exception in thread "main" java.util.IllegalFormatPrecisionException: 1
at java.util.Formatter$FormatSpecifier.checkInteger(Formatter.java:2984)
at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2729)
at java.util.Formatter.parse(Formatter.java:2560)
at java.util.Formatter.format(Formatter.java:2501)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at question15.Question15.main(Question15.java:29)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package question15;
/**
*
* @author MatthewsMacbook
*/
import java.util.Scanner;
public class Question15 {
private static double sumOfTheNumbers (double num1, double num2)
{
double sum= ((num2* (num2 + 1))/2)-((num1-1)* ((num1-1)+1)/2);
return sum;
}
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println("This program finds the sum of all consecutive numbers between 2 numbers");
System.out.println("please enter the first number");
double enteredInt1=keyboard.nextDouble();
System.out.println("please enter the second number");
double enteredInt2=keyboard.nextDouble();
double end= sumOfTheNumbers (enteredInt1, enteredInt2);
System.out.printf(" the sum of the numbers between %.1f"+"and : %.1d is: %.1f units", enteredInt1, enteredInt2, end);
}
}
您的打印语句中有拼写错误。它
System.out.printf(" the sum of the numbers between %.1f"+"and : %.1d is: %.1f units", enteredInt1, enteredInt2, end);
它应该是:
System.out.printf(" the sum of the numbers between %.1f"+"and : %.1f is: %.1f units", enteredInt1, enteredInt2, end);