简体   繁体   English

printf“无法解决符号错误”

[英]“Cannot resolve symbol error” for printf

This code is causing an error during compiling...I don't know what I am doing wrong? 该代码在编译期间导致错误...我不知道我在做什么错?

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

Everything has to be in Java 1.4.2. 一切都必须使用Java 1.4.2。 Here is the code: 这是代码:

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

Exactly as the error message tells you, there is no printf method of PrintStreams in Java 1.4.2 . 就像错误消息告诉您的那样, Java 1.4.2中没有PrintStreams printf方法 The current JavaDocs tell us that PrintStream#printf() was added in Java 1.5. 当前的JavaDocs告诉我们Java 1.5中添加了PrintStream#printf()

Use a modern version of Java (1.4 is over a decade old), or use a method other than printf() . 使用现代版本的Java(1.4已有十多年的历史了),或使用printf()以外的方法。

printf is only available from version 1.5 on, not yet in 1.4.2. 仅从1.5版开始提供printf ,而1.4.2版中尚未提供。 Since you are asking (see comment), this would do: 由于您正在询问(请参阅评论),因此可以:

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

printf is not in the PrintStream 1.4.2 API. printf不在PrintStream 1.4.2 API中。

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

But it is, for example, in the 1.5 API (or higher) 但这是在1.5 API(或更高版本)中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM