printf("%3d, ...) 在 eclipse juno 上不工作
printf("%3d, ...) not working on eclipse juno
package p;
import java.io.*;
public class fifo {
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int frames = 3, pointer = 0, fault = 0, reflen, def = 0;
int spaces[];
int reference[];
int mem_layout[][];
System.out.println("Reference String Length: ");
reflen = Integer.parseInt(br.readLine());
reference = new int[reflen];
mem_layout = new int[reflen][frames];
spaces = new int[frames];
for(int j = 0; j < frames; j++)
spaces[j] = 0;
System.out.println("Reference Numbers: ");
for(int i = 0; i < reflen; i++)
{
reference[i] = Integer.parseInt(br.readLine());
}
System.out.println();
for(int i = 0; i < reflen; i++)
{
int search = -1;
for(int j = 0; j < frames; j++)
{
if(spaces[j] == reference[i])
{
search = j;
def++;
break;
}
}
if(search == -1)
{
spaces[pointer] = reference[i];
fault++;
pointer++;
if(pointer == frames)
pointer = 0 ;
}
for(int j = 0; j < frames; j++)
mem_layout[i][j] = spaces[j];
}
for(int i = 0; i < frames; i++)
{
for(int j = 0; j < reflen; j++)
//System.out.printf("%3d ", reference);
System.out.printf("%3d ",mem_layout[j][i]); //ERROR UNDER PRINTF
System.out.println();
}
System.out.println("Fault: " + fault);
System.out.println("Default: " + def);
System.out.println("Page Fault Rate: " + fault + "/" + reflen + " = " + ((double)fault/reflen)*100 + "%" );
}
}
So we are told to continue our work at home. The code is working fine
in our computer lab that is using another version of eclipse. Im using
a juno and printf doesn't work anymore. Please help the submission is
tomorrow i don't know why it doesn't work anymore.
juno 过时了吗?我尝试执行消除错误的 eclipse 建议,但它会产生更多错误哈哈 :(
- 尝试
System.out.format();
而不是 System.out.printf();
- 尝试安装他们在实验室使用的另一个 Eclipse 版本
- 请告诉我们 error/exception Eclipse 显示的是什么
在评论中,您说错误信息是这样的:
The method format(String, Object[])in the type PrintStream is not applicable for the arguments (String, int)
这很奇怪。但是,一种可能的解释是您的 Eclipse 设置选择了 Java 的真正旧版本。在 Java 1.5 之前,不支持自动装箱。这会阻止编译器将 int
自动装箱为 Integer
。
启动 Eclipse 并打开 Window> 首选项。 Select Java>编译器首选项。看"Compiler compliance level"设置是,把它改成“1.8”。
然后使用 Project>Clean 重新编译所有内容。
你也可以改变这个:
System.out.printf("%3d ", mem_layout[j][i]);
对此:
System.out.printf("%3d ", new Object[]{
Integer.valueOf(mem_layout[j][i])});
这使得源代码与旧的 Java 编译器兼容。但这是一个糟糕的解决方案,IMO。
package p;
import java.io.*;
public class fifo {
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int frames = 3, pointer = 0, fault = 0, reflen, def = 0;
int spaces[];
int reference[];
int mem_layout[][];
System.out.println("Reference String Length: ");
reflen = Integer.parseInt(br.readLine());
reference = new int[reflen];
mem_layout = new int[reflen][frames];
spaces = new int[frames];
for(int j = 0; j < frames; j++)
spaces[j] = 0;
System.out.println("Reference Numbers: ");
for(int i = 0; i < reflen; i++)
{
reference[i] = Integer.parseInt(br.readLine());
}
System.out.println();
for(int i = 0; i < reflen; i++)
{
int search = -1;
for(int j = 0; j < frames; j++)
{
if(spaces[j] == reference[i])
{
search = j;
def++;
break;
}
}
if(search == -1)
{
spaces[pointer] = reference[i];
fault++;
pointer++;
if(pointer == frames)
pointer = 0 ;
}
for(int j = 0; j < frames; j++)
mem_layout[i][j] = spaces[j];
}
for(int i = 0; i < frames; i++)
{
for(int j = 0; j < reflen; j++)
//System.out.printf("%3d ", reference);
System.out.printf("%3d ",mem_layout[j][i]); //ERROR UNDER PRINTF
System.out.println();
}
System.out.println("Fault: " + fault);
System.out.println("Default: " + def);
System.out.println("Page Fault Rate: " + fault + "/" + reflen + " = " + ((double)fault/reflen)*100 + "%" );
}
}
So we are told to continue our work at home. The code is working fine in our computer lab that is using another version of eclipse. Im using a juno and printf doesn't work anymore. Please help the submission is tomorrow i don't know why it doesn't work anymore.
juno 过时了吗?我尝试执行消除错误的 eclipse 建议,但它会产生更多错误哈哈 :(
- 尝试
System.out.format();
而不是System.out.printf();
- 尝试安装他们在实验室使用的另一个 Eclipse 版本
- 请告诉我们 error/exception Eclipse 显示的是什么
在评论中,您说错误信息是这样的:
The method format(String, Object[])in the type PrintStream is not applicable for the arguments (String, int)
这很奇怪。但是,一种可能的解释是您的 Eclipse 设置选择了 Java 的真正旧版本。在 Java 1.5 之前,不支持自动装箱。这会阻止编译器将 int
自动装箱为 Integer
。
启动 Eclipse 并打开 Window> 首选项。 Select Java>编译器首选项。看"Compiler compliance level"设置是,把它改成“1.8”。
然后使用 Project>Clean 重新编译所有内容。
你也可以改变这个:
System.out.printf("%3d ", mem_layout[j][i]);
对此:
System.out.printf("%3d ", new Object[]{
Integer.valueOf(mem_layout[j][i])});
这使得源代码与旧的 Java 编译器兼容。但这是一个糟糕的解决方案,IMO。