java - 主线程异常

标签 java multithreading exception program-entry-point

我正在尝试创建一个程序来查找两个数字之间的所有连续数字的总和,并且我已经创建了一个代码,但我收到了这些错误。任何帮助或摆脱这些错误的方法将不胜感激。谢谢

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);

关于java - 主线程异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27344161/

相关文章:

c++ - 抛出 std::out_of_range 异常。 "expected type-specifier"

c# - 如果我不等待异步任务,您能解释一下为什么没有捕获到异常吗?

java - 礼品登记循环程序

java - JSP 不在 linux 上创建 servlet 类

java - 如何根据屏幕分辨率缩放 libgdx 中的 Sprite ?

ios - GCD : What happens when two threads want to execute blocks on the main thread at the same time

python - 循环完成后引发 for 循环中遇到的第一个异常

java - 在java中完全覆盖克隆对象

python - 使用 python 进行线程/While 循环

java - 合并锁会降低性能吗?