定义一个方法getSum(int start,int end){...},该方法完成获取[start-end]范围数据的和。

public class StartAndAnd {//创建类
    public static void main(String[] agrs){//调用main方法
        Scanner cs=new Scanner(System.in);//创建对象
        System.out.print("请输入start:");
        int start=cs.nextInt();//从键盘接收start的值
        System.out.print("请输入end:");
        int end=cs.nextInt();//从键盘接收end的值
        while(start>=end){//进入循环 防止输入错误
            System.out.print("输入start只能小于end,请重新输入");
            System.out.println();
            System.out.print("请输入start:");
            start=cs.nextInt();//重新接收start
            System.out.print("请输入end:");
            end=cs.nextInt();//重新接受end的值
        }
        getSum(start,end);//调用getSum方法  求出和
    }
    public static void getSum(int start,int end){//定义一个接收两个参数的方法,并求出两个参数之间各元素的和
        int count=0;//定义一个自变量      求和
        for(int i=start;i<=end;i++){//进入循环  求出总和
            count+=i;
        }
        System.out.println(count);//输出打印
    }
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Thread子类创建线程的实现代码: ``` public class SumThread extends Thread { private int start; private int end; private int sum; public SumThread(int start, int end) { this.start = start; this.end = end; } public int getSum() { return sum; } @Override public void run() { for (int i = start; i <= end; i++) { sum += i; System.out.println(Thread.currentThread().getName() + " computes sum of " + i); } } } public class Main { public static void main(String[] args) { SumThread[] threads = new SumThread[10]; for (int i = 0; i < threads.length; i++) { threads[i] = new SumThread(i * 10 + 1, (i + 1) * 10); threads[i].start(); } int result = 0; for (SumThread thread : threads) { try { thread.join(); result += thread.getSum(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("The result is " + result); } } ``` 使用Thread类直接创建线程对象的实现代码: ``` public class SumRunnable implements Runnable { private int start; private int end; private int sum; public SumRunnable(int start, int end) { this.start = start; this.end = end; } public int getSum() { return sum; } @Override public void run() { for (int i = start; i <= end; i++) { sum += i; System.out.println(Thread.currentThread().getName() + " computes sum of " + i); } } } public class Main { public static void main(String[] args) { Thread[] threads = new Thread[10]; for (int i = 0; i < threads.length; i++) { SumRunnable runnable = new SumRunnable(i * 10 + 1, (i + 1) * 10); threads[i] = new Thread(runnable); threads[i].start(); } int result = 0; for (Thread thread : threads) { try { thread.join(); SumRunnable runnable = (SumRunnable) thread.getRunnable(); result += runnable.getSum(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("The result is " + result); } } ``` 注意,为了能够看到线程交替输出,可以在run方法中加入一句打印语句,以便观察每个线程计算的过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值