2014-12-07 168 views
1

我想创建一个程序来查找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); 

    } 

} 
+0

请添加Java标签给它.... – ha9u63ar 2014-12-07 15:28:18

回答

1

您在打印声明中有错字。这是

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); 
相关问题