对于printf,“无法解决符号错误”

时间:2013-01-11 03:22:24

标签: java

此代码在编译期间导致错误...我不知道我做错了什么?

CoinFlippingContest.java:27: cannot resolve symbol
symbol : method printf (java.lang.String,int,int)
location: class java.io.PrintStream
System.out.printf(" %d \t %d",(i+1),frequency[i]);
       ^
1 error

一切都必须在Java 1.4.2中。这是代码:

//IMPORT JAVA UTILITIES AND IO
import java.io.*;
import java.util.*;
//DEFINE CLASS
public class CoinFlippingContest
{
public static void main( String args[] )
{
//GENERATES RANDOM NUMBER
Random randomNumbers = new Random(); 

int[] frequency = new int[10];
for(int i=0; i<10; i++)
frequency[i]=0;

int number;

//RESULTS FOR 6000
for ( int col = 1; col <= 6000; col++ )
{
number = 1 + randomNumbers.nextInt( 10 );
frequency[number]++;
}
//DISPLAY THE HEADINGS
System.out.println( "number\tFrequency" ); 
for(int i=0; i<10; i++)
System.out.printf(" %d \t %d",(i+1),frequency[i]);

//NUMBER OF HEADS AND TAILS IN TOTAL
int even = 0, odd = 0;
for(int i=0; i<10; i++)
{
if(i%2==0)
odd +=frequency[i];
else even += frequency[i];
}
//OUTPUT NUMBER OF HEADS AND TAILS EVEN AND ODDS
System.out.println("\nheads: "+even+"\ntails: "+odd);
}
}

3 个答案:

答案 0 :(得分:2)

正如错误消息告诉您的那样,there is no printf method of PrintStreams in Java 1.4.2。当前的JavaDocs告诉我们在Java 1.5中添加了PrintStream#printf()

使用现代版本的Java(1.4已超过十年),或使用printf()以外的方法。

答案 1 :(得分:2)

printf仅适用于1.5版本,尚未在1.4.2版本中提供。既然你在询问(见评论),那就可以了:

for(int i = 0; i < 10; i ++)
{
    System.out.println((i + 1) + "\t" + frequency[i]);
}

答案 2 :(得分:1)

printf不在PrintStream 1.4.2 API中。

http://docs.oracle.com/javase/1.4.2/docs/api/java/io/PrintStream.html

但是,例如,它在1.5 API(或更高版本)

http://docs.oracle.com/javase/1.5.0/docs/api/java/io/PrintStream.html