Java System.out.println 中的错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24476756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error in System.out.println
提问by Shagufta Oliveyu Methwani
Is there any error in the following code? It shows cant find symbol, symbol: class out location: class System. In the log, it show a lot of errors, including
下面的代码有错误吗?它显示找不到符号,符号:类出位置:类系统。在日志中,它显示了很多错误,包括
java.lang.ClassFormatError: Method "" in class Area has illegal signature "(Ljava/lang/Object;)Ljava/lang/System$out$println;"
java.lang.ClassFormatError:类Area中的方法“”具有非法签名“(Ljava/lang/Object;)Ljava/lang/System$out$println;”
import java.util.*;
class Area
{
double pi=3.14;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of r");
int r=sc.nextInt();
System.out.println("enter the value h");
int h=sc.nextInt();
void areaOfCircle()
{
double area1=pi*r*r;
System.out.println("area of circle="+area1);
}
void areaOfCylinder()
{
double area2=2*pi*r*(r+h);
System.out.println("area of cylinder="+area2);
}
public static void main(String args[])
{
Area a=new Area();
a.areaOfCircle();
a.areaOfCylinder();
}
}
采纳答案by Mandar Pandit
The statement System.out.println("");
should be written in some block. It cannot be written directly in class.
该语句System.out.println("");
应该写在某个块中。不能直接写在课堂上。
public class ClassName {
System.out.println("this statement gives error"); // Error!!
}
Either it should be inside curly braces {...}
like:
它应该在花括号内,{...}
例如:
{ System.out.println("this works fine"); }
This way is an initializer block.
这种方式是一个初始化块。
Or it should be written in a method like:
或者它应该写成这样的方法:
public void methodName(){
System.out.println("inside a method, prints fine");
}
Your complete programshould be like:
你的完整程序应该是这样的:
public class Area {
double pi = 3.14;
int r;
int h;
void areaOfCircle() {
double area1 = pi * r * r;
System.out.println("area of circle=" + area1);
}
void areaOfCylinder() {
double area2 = 2 * pi * r * (r + h);
System.out.println("area of cylinder=" + area2);
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of r");
Area a = new Area();
a.r = sc.nextInt();
System.out.println("enter the value h");
a.h = sc.nextInt();
a.areaOfCircle();
a.areaOfCylinder();
}
}
回答by Hyman
You can't place code outside methods in Java. You have
您不能在 Java 中的方法之外放置代码。你有
System.out.println("Enter the value of r");
which is not belonging to anything. Fix these issues and the problem will go away.
不属于任何东西。解决这些问题,问题就会消失。
Just for curiosity, how should code outside methods be called and from what according to you? What I mean is that the execution is made by a code flow which starts from a entry point (the main
method in Java) and jumps to methods called, eventually spawning other threads. Code which doesn't reside inside a method is not reachable nor it leads to anything.
只是出于好奇,根据您的说法,应该如何调用外部方法的代码以及从什么调用?我的意思是,执行是由一个代码流完成的,该代码流从一个入口点(main
Java 中的方法)开始并跳转到调用的方法,最终产生其他线程。不驻留在方法中的代码是不可访问的,也不会导致任何事情。
回答by JB Nizet
The following code
以下代码
System.out.println("Enter the value of r");
int r=sc.nextInt();
System.out.println("enter the value h");
int h=sc.nextInt();
must be inside a method. Not directly in the class. Classes can contain field and method declarations, but not arbitrary code.
必须在方法内部。不是直接在课堂上。类可以包含字段和方法声明,但不能包含任意代码。
回答by Ysr Shk
You are missing the java basics.
您缺少 Java 基础知识。
Problem:
问题:
Processing statements should be in function only.
处理语句应该只在函数中。
Solution:
解决方案:
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of r");
int r=sc.nextInt();
System.out.println("enter the value h");
int h=sc.nextInt();
Post the above code either in function or constructor by doing changes according to your requirements.
通过根据您的要求进行更改,在函数或构造函数中发布上述代码。
回答by SparkOn
In java does not work like this any behaviour you want to implement you must do it inside a block or a method
在 Java 中,您要实现的任何行为都不会像这样工作,您必须在块或方法中执行它
It needs to be inside an executable block of code to be executed. Otherwise there's no way to know when to execute it.
它需要在要执行的可执行代码块内。否则没有办法知道什么时候执行它。
Remember that a class can only have attributes or methods.Attributes are the properties of the class and methods represent the behaviour of the class.So every implementation goes inside a method or a block.
请记住,一个类只能有属性或方法。属性是类的属性,方法代表类的行为。所以每个实现都在方法或块中。
The only things allowed outside method and constructor declarations are declarations of fields. Since
唯一允许在方法和构造函数声明之外的东西是字段声明。自从
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of r");
int r=sc.nextInt();
System.out.println("enter the value h");
int h=sc.nextInt();
is not a field declaration, it's not allowed.
不是字段声明,这是不允许的。