简体   繁体   English

在 Eclipse 中编译 Java 代码时出现多个错误

[英]Getting multiple errors compiling Java code in Eclipse

I'm new to programming, and I'm using Eclipse to make some easy programs like calculators and vote counting.我是编程新手,我正在使用 Eclipse 来制作一些简单的程序,比如计算器和计票。 (Don't mind the language, it's Portuguese and i'm from Brazil.) (别介意语言,它是葡萄牙语,我来自巴西。)

So as you can see in the image with "Questão1.java."正如您在带有“Questão1.java”的图像中看到的那样。 Class opened compiles perfectly, and the "Questão2.java" shows quite a lot of errors, and I have absolutely no idea what it means.打开的类编译完美,“Questão2.java”显示了很多错误,我完全不知道它是什么意思。

This one is giving a lot of errors:这个给出了很多错误:

这个给出了很多错误

This one compiles perfectly, no errors and results as expected:这个编译完美,没有错误和预期的结果:

这个编译完美,没有错误和预期的结果。

Thanks everyone for answering, i found out the error and it was indeed the "printf"...and also i'll remember to never post codes as images next time, again thank you guys.感谢大家的回答,我发现了错误,它确实是“printf”……而且我会记住下次永远不要将代码发布为图像,再次感谢你们。

The problem is that you use printf() instead of print() or println() .问题是您使用printf()而不是print()println()

printf() adds formatting to whatever you are trying to print, and the String that determines how your output should be formatted uses characters like % , which you also use in the class that throws errors. printf()为您尝试打印的任何内容添加格式,并且确定输出格式的字符串使用%之类的字符,您也在引发错误的类中使用这些字符。

The error错误

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = ' '
    at [and so on...]
    at Questao2_Lista[bla bla bla].main(Questao2.java:18)

can be read in the following way:可以通过以下方式阅读:

The first line defines what kind of exception was thrown, in this instance it is an "UnknownFormatConversionException".第一行定义了抛出的异常类型,在本例中它是“UnknownFormatConversionException”。

The lines below it are called a "stacktrace", that show where exactly the exception was thrown and the "way" it got propagated up the call-stack.它下面的行称为“堆栈跟踪”,它显示了抛出异常的确切位置以及它在调用堆栈中向上传播的“方式”。

It looks like this is the only error, I can't see any of the other errors you talked about so I guess you just assumed that every line was a separate error?看起来这是唯一的错误,我看不到您谈到的任何其他错误,所以我猜您只是假设每一行都是一个单独的错误?

The problem with "Questão2.java" is the System.out.printf statement. “Questão2.java”的问题在于 System.out.printf 语句。 The statement has a '%' which is a special character.该语句有一个“%”,它是一个特殊字符。 % is used as a preceding character for a place holder % 用作占位符的前导字符

Eg: %d in the string will be replaced with a number passed as an argument.例如:字符串中的 %d 将替换为作为参数传递的数字。

int lines =10;
System.out.printf ("There are %d lines", lines);

Will come as:将作为:

There are 10 lines 

If its just a print statement Use System.out.println instead of System.out.printf如果它只是一个打印语句使用 System.out.println 而不是 System.out.printf

尝试在 Questão2.java 的第 18 行中使用System.out.print()System.out.println()代替System.out.printf()

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

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