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);
}
}
請添加Java標籤給它.... – ha9u63ar 2014-12-07 15:28:18