Java 无法运行线程程序
我正在写一个简单的程序来解释线程 为什么它会显示以下错误。 谁能帮帮我吗Java 无法运行线程程序,java,Java,我正在写一个简单的程序来解释线程 为什么它会显示以下错误。 谁能帮帮我吗 public class Myth extends Thread { public void run() { int val=65; try { for(int i=0;i<26;i++) { System.out.println((char)val); val++; sleep(500); } } catch
public class Myth extends Thread {
public void run() {
int val=65;
try {
for(int i=0;i<26;i++) {
System.out.println((char)val);
val++;
sleep(500);
}
}
catch(InterruptedException e) {
System.out.println(e);
}
// error pops up on this bracket saying class interface or enum expected.
// error in this line says-- illegal start of expression
public static void main(String args[]) {
Myth obj=new Myth();
obj.start();
}
}
}
public类扩展线程{
公开募捐{
int-val=65;
试一试{
对于(int i=0;i而言,run()
方法未正确关闭。请在System.out.println(e);
之后添加一个额外的关闭荣誉。您应该可以走了。您必须平衡打开和关闭的花括号
public class Myth extends Thread{
public void run(){
int val=65;
try{
for(int i=0;i<26;i++){
System.out.println((char)val);
val++;
sleep(500);
}
}catch(InterruptedException e){
System.out.println(e);
}
}
public static void main(String args[]){
Myth obj=new Myth();
obj.start();
}
}
public类扩展线程{
公开募捐{
int-val=65;
试一试{
对于(int i=0;i下面是更正的代码原因是您的运行
方法的主体未关闭
public class Myth extends Thread {
public void run() {
int val = 65;
try {
for (int i = 0; i < 26; i++) {
System.out.println((char) val);
val++;
sleep(500);
}
}
catch (InterruptedException e) {
System.out.println(e);
}
}
public static void main(String args[]) // error in this line says-- illegal
// start of expression
{
Myth obj = new Myth();
obj.start();
}
}
public类扩展线程{
公开募捐{
int-val=65;
试一试{
对于(int i=0;i<26;i++){
System.out.println((char)val);
val++;
睡眠(500);
}
}
捕捉(中断异常e){
系统输出打印ln(e);
}
}
publicstaticvoidmain(字符串args[])//此行中的错误为--非法
//开始表达
{
神话obj=新神话();
obj.start();
}
}
将main
方法放在mystry.run()方法中。其中,它不应是类的静态函数
public class Myth extends Thread {
public void run(){
int val=65;
try {
for(int i=0;i<26;i++)
{
System.out.println((char)val);
val++;
sleep(500);
}
}catch(InterruptedException e){
System.out.println(e);
}
// error pops up on this bracket saying class interface or enum expected.
// error in this line says-- illegal start of expression
}
public static void main(String args[]){
Myth obj=new Myth();
obj.start();
}
}
public类扩展线程{
公开募捐{
int-val=65;
试一试{
对于(int i=0;i你的run方法没有正确地用大括号括起来。关闭并编译它,这样就可以了。每当你打开一个大括号时,关闭大括号是一个很好的做法。然后你开始在它们之间编写代码。这应该可以帮助你避免这种令人尴尬的愚蠢错误。今年的问题是格式化沃德找到了……你!对愚蠢的格式设置感到抱歉。匆忙完成了。:p感谢很多mthmulders它真的很有帮助。很高兴提供帮助,欢迎使用堆栈溢出。如果此答案或任何其他答案解决了你的问题,请将其标记为已接受。非常感谢你的帮助mthmulders非常感谢。你的帖子很有帮助。非常感谢Sakthisundar的帮助非常感谢塔哈·艾哈迈德·汗的帮助