出于某种原因,它一直在说:
错误:' .class'预期
和
错误:';'预期
public class num1{
public static void main(String args[])
throws java.io.IOException {
char let;
num1 (char l) {
let=l;
}
l = (char) System.in.read();
}
}
答案 0 :(得分:2)
Num1是一个构造函数,因此在java中不允许将其保留在main.nested方法或方法内部。由于构造函数也是一种方法,因此您无法在main
中定义构造函数答案 1 :(得分:0)
上面评论过,但这里有一些帮助。
构造函数用于在实例化类时执行某些操作。您在代码中使用的方法public static void main()使该类可执行 - 这是两个非常不同的东西。你的类的构造函数看起来像这样
public num1(possible variables)
{
Things to do with said variables
}
我建议你在深入研究Java的深度之前先做一些阅读,以免在基本术语和语法的重要差异中迷失方向。
This引用是开始Java编码器的好地方。它将以一个非常有条理和良好解释的方式逐章介绍。
再次链接,以防万一: http://math.hws.edu/javanotes/index.html
答案 2 :(得分:0)
您的构造函数不会使用main()
方法,而是
public class num1{
public static void main(String args[]) throws IOException {
char let;
num1 (char l) {
let=l;
}
l = (char) System.in.read();
}
}
几乎可以肯定(并使用Java命名约定 - 所以Num1
)类似的东西,
public class Num1 {
char let; // <-- char is a primitive type. lowercase.
// Character is an object wrapper for char.
Num1 (char l) { // <-- package level permission
let=l;
}
public static void main(String args[]) throws IOException {
Num1 n = new Num1((char) System.in.read());
System.out.println(n);
}
@Override
public String toString() {
return String.valueOf(let);
}
}