0

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

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

3 回答 3

2

正如错误消息告诉您的那样,Java 1.4.2中没有printf方法PrintStreams。当前的 JavaDocs 告诉我们这PrintStream#printf()是在 Java 1.5 中添加的。

使用现代版本的 Java(1.4 已经有十多年的历史了),或者使用printf().

于 2013-01-11T03:26:02.713 回答
2

printf仅从 1.5 版开始可用,1.4.2 版尚不可用。既然你在问(见评论),这会做:

for(int i = 0; i < 10; i ++)
{
    System.out.println((i + 1) + "\t" + frequency[i]);
}
于 2013-01-11T03:26:26.940 回答
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

于 2013-01-11T03:27:50.247 回答