使用链表循环。输入不能使用多个进程
Round Robin by using Linked List. The input cannot use more than one process
图片显示有逻辑错误。我只能输入一个过程。如果我添加更多,就会出现错误。系统说我有一个超出边界的数组。我真的需要帮助解决这个问题。之所以把链表转成数组是因为我对链表没有任何专业知识
import java.util.*;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
LinkedList<Integer> cpuburst = new LinkedList<>();
LinkedList<Integer> priority = new LinkedList<>();
LinkedList<String> process = new LinkedList<>();
LinkedList<Integer> nextTime = new LinkedList<>();
int clockTime = 0;
double totalWaitTime = 0;
int quit, quantum = 2;
int processesComplete = 0;
do {
System.out.print("input process");
process.add(sc.next());
System.out.print("input cpu_burst");
cpuburst.add(sc.nextInt());
if (cpuburst.add(0)) {
processesComplete++;
}
System.out.print("input priority");
priority.add(sc.nextInt());
nextTime.add(0);
System.out.print("more?");
quit = sc.nextInt();
} while (quit != 0);
String[] Process = process.toArray(new String[process.size()]);
Integer[] cpu_burst = cpuburst.toArray(new Integer[cpuburst.size()]);
Integer[] Priority = priority.toArray(new Integer[priority.size()]);
Integer[] next = nextTime.toArray(new Integer[nextTime.size()]);
for (int i = 0; i < next.length; i++) {
System.out.println(Process[i] + "\t\t" + cpu_burst[i] + "\t\t" + Priority[i]);
}
int roundRobinIndex = 0;
System.out.println(" | Process | CPU Burst | Priority | Time | Clock Time | Wait Time |");
while (processesComplete < cpu_burst.length) {
if (cpu_burst[roundRobinIndex] > 0) {
int time = Math.min(quantum, cpu_burst[roundRobinIndex]);// compare value
cpu_burst[roundRobinIndex] -= time;
if (cpu_burst[roundRobinIndex] == 0)
processesComplete++;
int waitTime = clockTime - next[roundRobinIndex];
totalWaitTime += waitTime;
System.out.println(" | " + Process[roundRobinIndex] + " | " + cpu_burst[roundRobinIndex]
+ " | " + Priority[roundRobinIndex] + " | " + time + " | " + clockTime
+ " | " + waitTime + " |");
//clockTime += quantum;
clockTime += time;
next[roundRobinIndex] = clockTime;
}
roundRobinIndex = (roundRobinIndex + 1) % cpu_burst.length;
}
System.out.println("Average wait time" + totalWaitTime / cpu_burst.length);
}
}
好吧,我认为这段代码有几个问题,但要回答这个问题:您会收到 IndexOutOfBounds 错误,因为每次添加进程时,当您调用
if (cpuburst.add(0)) {
processesComplete++;
}
您正在向 cpuburst-list 添加一个附加项(值 0),因此该列表的长度是其他列表的两倍,在输入值和零之间交替。后来,你写了
if (cpu_burst[roundRobinIndex] > 0) {
//...some code here...
}
roundRobinIndex = (roundRobinIndex + 1) % cpu_burst.length;
这意味着,其中的代码将在用户输入 cpuburst 值时第一次执行,然后下一次不会执行,因为有一个零和第三次(当您测试时两个进程),它会再次进入 if-clause 但你在
处得到 indexOutOfBounds 错误
int waitTime = clockTime - next[roundRobinIndex];
因为下一个只有两个条目。
图片显示有逻辑错误。我只能输入一个过程。如果我添加更多,就会出现错误。系统说我有一个超出边界的数组。我真的需要帮助解决这个问题。之所以把链表转成数组是因为我对链表没有任何专业知识
import java.util.*;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
LinkedList<Integer> cpuburst = new LinkedList<>();
LinkedList<Integer> priority = new LinkedList<>();
LinkedList<String> process = new LinkedList<>();
LinkedList<Integer> nextTime = new LinkedList<>();
int clockTime = 0;
double totalWaitTime = 0;
int quit, quantum = 2;
int processesComplete = 0;
do {
System.out.print("input process");
process.add(sc.next());
System.out.print("input cpu_burst");
cpuburst.add(sc.nextInt());
if (cpuburst.add(0)) {
processesComplete++;
}
System.out.print("input priority");
priority.add(sc.nextInt());
nextTime.add(0);
System.out.print("more?");
quit = sc.nextInt();
} while (quit != 0);
String[] Process = process.toArray(new String[process.size()]);
Integer[] cpu_burst = cpuburst.toArray(new Integer[cpuburst.size()]);
Integer[] Priority = priority.toArray(new Integer[priority.size()]);
Integer[] next = nextTime.toArray(new Integer[nextTime.size()]);
for (int i = 0; i < next.length; i++) {
System.out.println(Process[i] + "\t\t" + cpu_burst[i] + "\t\t" + Priority[i]);
}
int roundRobinIndex = 0;
System.out.println(" | Process | CPU Burst | Priority | Time | Clock Time | Wait Time |");
while (processesComplete < cpu_burst.length) {
if (cpu_burst[roundRobinIndex] > 0) {
int time = Math.min(quantum, cpu_burst[roundRobinIndex]);// compare value
cpu_burst[roundRobinIndex] -= time;
if (cpu_burst[roundRobinIndex] == 0)
processesComplete++;
int waitTime = clockTime - next[roundRobinIndex];
totalWaitTime += waitTime;
System.out.println(" | " + Process[roundRobinIndex] + " | " + cpu_burst[roundRobinIndex]
+ " | " + Priority[roundRobinIndex] + " | " + time + " | " + clockTime
+ " | " + waitTime + " |");
//clockTime += quantum;
clockTime += time;
next[roundRobinIndex] = clockTime;
}
roundRobinIndex = (roundRobinIndex + 1) % cpu_burst.length;
}
System.out.println("Average wait time" + totalWaitTime / cpu_burst.length);
}
}
好吧,我认为这段代码有几个问题,但要回答这个问题:您会收到 IndexOutOfBounds 错误,因为每次添加进程时,当您调用
if (cpuburst.add(0)) {
processesComplete++;
}
您正在向 cpuburst-list 添加一个附加项(值 0),因此该列表的长度是其他列表的两倍,在输入值和零之间交替。后来,你写了
if (cpu_burst[roundRobinIndex] > 0) {
//...some code here...
}
roundRobinIndex = (roundRobinIndex + 1) % cpu_burst.length;
这意味着,其中的代码将在用户输入 cpuburst 值时第一次执行,然后下一次不会执行,因为有一个零和第三次(当您测试时两个进程),它会再次进入 if-clause 但你在
处得到 indexOutOfBounds 错误int waitTime = clockTime - next[roundRobinIndex];
因为下一个只有两个条目。