使用循环算法来计算所需时间

发布于 2024-10-18 03:19:01 字数 1954 浏览 3 评论 0原文

也许有人感觉很友好,并且会觉得这个脑筋急转弯很有趣……但我觉得也许我开始让自己感到困惑。

其目的是使用循环算法计算完成所有进程所需的时间。我让它提示用户输入时间量,然后提示用户要计算多少个进程。从那里,我根据进程的数量抛出一个 for 语句来分配进程到达时间和突发时间。

对于那些不熟悉的人来说,时间量是在切换到下一个之前要处理多少个周期,突发是完成该过程需要多少个周期,当然到达时间是在它之前完成了多少个周期到达。算法很简单,不过是为了展示CPU的调度情况。如果有人能提供帮助那就太好了!我很失落。我想用 C# 来做这件事,但我的 C# 编程技能不足以做到这一点。

我遇到的两个问题都在我的 if 语句中,我开始迷失自己,无论出于何种原因,它在使用 diff < 编译时给了我一个错误。 parrive.get[ii] 或 diff < parrive.get(ii) 或者甚至在 if 语句开头将 parrive.get[ii] 分配给另一个变量并使用另一个变量(如图所示)...

import java.io.*;
import java.util.*;

public class Thread{       

    public Thread() {
        inputexecute();
    }

    public void inputexecute(){

        Scanner x = new Scanner(System.in);
        int xx = 0;

        String choice = null;
        ArrayList parrive = new ArrayList();
        ArrayList pburst = new ArrayList();

        while (true){
            System.out.println("Enter the time quantum: ");
            int quant = x.nextInt();
            System.out.println("Enter the number of processes: ");
            int pnum = x.nextInt();

            for( int i=0; i<pnum; i++)
            {
                System.out.println("Enter the arival for p"+i+": ");
                int arrive = x.nextInt();
                parrive.add(arrive);

                System.out.println("Enter the burst time: ");
                int burst = x.nextInt();
                pburst.add(burst);    


            }

            int dif;
            for(int ii=0; ii < pnum; ii++)
            {
                int asw == parrive.get[ii];

                if (asw < quant)
                {
                    dif = quant - asw;
                }                    
                if (quant < asw)
                {
                    asw = asw - quant;
                }
                if (dif > 0)
                {

                }
            }
        } /* end while*/
    } /* end exec input*/
} /* class thread */

Maybe someone out there is feeling friendly and would find this brain teaser interesting.. but I feel like maybe I started confusing myself.

The object of this is to calculate, using the round robin algorithm, the time it would take for all of the processes to complete. I have it prompt the user for the time quantum, and then how many processes it would like to calculate. From there, I throw a for statement based on however many processes there are to assign the process arival time and the burst time.

For those who arent familiar, the time quantum is how many cycles it will process before it switches to the next, the burst is how many cycles it takes to complete that process, and of course the arival time is how many cycles have completed before it arives. Simple algorithm, but it's to show how CPU's schedule. IF ANYONE COULD HELP AT ALL THAT WOULD BE FANTASTIC! I'm so lost. I wanted to do this in C#, but my programming skills are less than sufficent in C# for that.

The two problems I am having are in my if statements, I started to lose myself, and for whatever reason, it gives me an error when compiling with dif < parrive.get[ii] or dif < parrive.get(ii) OR even assigning parrive.get[ii] to another variable at the beginning of my if statements and using another variable(as shown)...

import java.io.*;
import java.util.*;

public class Thread{       

    public Thread() {
        inputexecute();
    }

    public void inputexecute(){

        Scanner x = new Scanner(System.in);
        int xx = 0;

        String choice = null;
        ArrayList parrive = new ArrayList();
        ArrayList pburst = new ArrayList();

        while (true){
            System.out.println("Enter the time quantum: ");
            int quant = x.nextInt();
            System.out.println("Enter the number of processes: ");
            int pnum = x.nextInt();

            for( int i=0; i<pnum; i++)
            {
                System.out.println("Enter the arival for p"+i+": ");
                int arrive = x.nextInt();
                parrive.add(arrive);

                System.out.println("Enter the burst time: ");
                int burst = x.nextInt();
                pburst.add(burst);    


            }

            int dif;
            for(int ii=0; ii < pnum; ii++)
            {
                int asw == parrive.get[ii];

                if (asw < quant)
                {
                    dif = quant - asw;
                }                    
                if (quant < asw)
                {
                    asw = asw - quant;
                }
                if (dif > 0)
                {

                }
            }
        } /* end while*/
    } /* end exec input*/
} /* class thread */

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木槿暧夏七纪年 2024-10-25 03:19:01

你的错误是你使用相等运算符(==)而不是赋值

                            int asw == parrive.get[ii];

应该是

                            int asw = parrive.get[ii];

Your error is that you are using equality operator (==) instead of assignment

                            int asw == parrive.get[ii];

should be

                            int asw = parrive.get[ii];
ˇ宁静的妩媚 2024-10-25 03:19:01

这样写

List<Integer> parrive = new ArrayList<Integer>();

for(int asw: parrive) {
    int dif = Math.abs(asw - quant);
    if (dif == 0) continue;
    // if dif > 0

}

我认为

asw = asw - quant;

应该

dif = asw - quant;

The way I would write it

List<Integer> parrive = new ArrayList<Integer>();

for(int asw: parrive) {
    int dif = Math.abs(asw - quant);
    if (dif == 0) continue;
    // if dif > 0

}

I am assuming

asw = asw - quant;

should be

dif = asw - quant;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文