Java 程序中的 DecimalFormat,我收到一条错误消息,提示找不到符号 twoDigitPattern
DecimalFormat in Java program, I get an error that says cannot find symbol twoDigitPattern
下面的代码已经过测试并且运行良好,直到我添加了 DecimalFormat。现在它出错并说找不到符号 "twoDigitPattern"。我尝试使用 + DecimalFormat(average)) 但它也出错了。
class IntegerInputWithCkAndAverage
{
public static void main( String[] args )
{
final int SENTINEL = 0;
int numEntered = 0;
int accumulator = 0;
int counter = 0;
double average = 0;
Scanner scan = new Scanner( System.in );
System.out.print( "Enter a integer, or 0 to end > " );
while( !scan.hasNextInt() ) {
String garbage = scan.nextLine();
System.out.
print( "Invalid input. Please enter an integer, or 0 to end" );
}
numEntered = Integer.parseInt( scan.nextLine() );
while( numEntered != SENTINEL ) {
accumulator = accumulator + numEntered;
counter++;
System.out.print( "Enter another integer, or 0 to end> " );
while( !scan.hasNextInt() ) {
String garbage = scan.nextLine();
System.out.print(
"Invalid input. Please enter an integer or 0 to end" );
}
numEntered = Integer.parseInt( scan.nextLine() );
}
average = (double) accumulator / counter;
DecimalFormat twoDigitPattern = new DecimalFormat( "#0.00" );
System.out.println(
"the total of the " + counter + "numbers entered is " +
accumulator + " and the average is " + twoDigitPattern( average ) );
}
}
改变
twoDigitPattern(average)
到
twoDigitPattern.format(average)
下面的代码已经过测试并且运行良好,直到我添加了 DecimalFormat。现在它出错并说找不到符号 "twoDigitPattern"。我尝试使用 + DecimalFormat(average)) 但它也出错了。
class IntegerInputWithCkAndAverage
{
public static void main( String[] args )
{
final int SENTINEL = 0;
int numEntered = 0;
int accumulator = 0;
int counter = 0;
double average = 0;
Scanner scan = new Scanner( System.in );
System.out.print( "Enter a integer, or 0 to end > " );
while( !scan.hasNextInt() ) {
String garbage = scan.nextLine();
System.out.
print( "Invalid input. Please enter an integer, or 0 to end" );
}
numEntered = Integer.parseInt( scan.nextLine() );
while( numEntered != SENTINEL ) {
accumulator = accumulator + numEntered;
counter++;
System.out.print( "Enter another integer, or 0 to end> " );
while( !scan.hasNextInt() ) {
String garbage = scan.nextLine();
System.out.print(
"Invalid input. Please enter an integer or 0 to end" );
}
numEntered = Integer.parseInt( scan.nextLine() );
}
average = (double) accumulator / counter;
DecimalFormat twoDigitPattern = new DecimalFormat( "#0.00" );
System.out.println(
"the total of the " + counter + "numbers entered is " +
accumulator + " and the average is " + twoDigitPattern( average ) );
}
}
改变
twoDigitPattern(average)
到
twoDigitPattern.format(average)