【问题标题】:I want to find even and odd number using two thread evenCount and oddCount我想使用两个线程 evenCount 和 oddCount 找到偶数和奇数
【发布时间】:2020-07-20 19:42:40
【问题描述】:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author user
 */



class evenCount extends Thread {
    public void run(){
        for(int i = 2; i >=20; i++)
        {
            try 
            {
                Thread.sleep(500);
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
            System.out.println(i);
        }
    }
}

class oddCount extends Thread {
    
    public void run(){
    for(int i = 21; i>=31; i++)
    {
        try
        {
            Thread.sleep(700);
        }
        
        catch(Exception e)
        {
            System.out.println(e);
        }
        
        System.out.println(i);
    }
    }
}

class mainThread{

    public static void main(String args[]){
        evenCount even = new evenCount();
        oddCount odd  = new oddCount();
        
        even.run();
        odd.run();
    }
}

```````````````````描述````````````````````````

我想找到偶数和奇数,但输出显示如下:

线程“main”中的异常 java.lang.RuntimeException:无法编译的源代码 - 错误的 sym 类型:线程。 在 evenCount.(Thread.java:16) 在 mainThread.main(Thread.java:56) /Users/user/Library/Caches/NetBeans/8.2/executor-sn-ps/run.xml:53:Java 返回:1 构建失败(总时间:0 秒)

有时它显示构建成功但不显示结果(当我使用 Runnable 时)

【问题讨论】:

标签: java


【解决方案1】:

它没有显示任何输出,因为你的 for 循环条件是错误的。

for(int i = 2; i >=20; i++) for(int i = 21; i>=31; i++)

两个 for 循环都无法通过。

应该是i和i

至少通过上述更改,您将获得输出。

【讨论】:

    猜你喜欢
    • 2013-05-17
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多