我開發了一個平均等待時間程序,用戶輸入進程,到達時間等。然後它告訴你需要多長時間,但它看起來像是有錯誤,我不知道如何解決它。有人能幫我嗎。的碼的錯誤行是:有些東西與我的java程序無法正常工作
Scanner sc = new Scanner(System.in);
誤差在線程讀取異常「主要」 java.lang.Error的:未解決問題彙編: 重複局部變量SC
這裏是代碼 包項目3;
import java.util.Random;
import java.util.Scanner;
public class AveragaWaitingTime {
static int Min(int b[], int a[], int tbt, int r, int n,int large[]) {
int j = 0;
int min = tbt;
int l=0;//finding larger number of process in queue
for (int i = n - 1; i >= 0; i--) {
if (b[i] < min && b[i] > 0 && r >= a[i]) {
min = b[i];
l++;
j = i;
}
}
if(large[0]<l)
large[0]=l;
return j;
}
static int Shortesfinishtime(int n,int p[],int at[],int bt[],int bt2[],int wt[],int tat[])
{
int tbt = 0, large[] = {0};
for (int i = 0; i < n; i++) {
tbt = tbt + bt[i];
}
int time[] = new int[tbt];
int k = 0;
int q2 = 0;
System.out.println("Gantt Chart");
System.out.print("|");
//bt[0] = bt[0] - 1;
for (int i = 0; i < tbt; i++) {
int q = Min(bt, at, tbt, i, n,large);
if (q != q2) {
System.out.print(" p[" + p[q] + "]\t|");
time[k++] = i;
wt[q] = i;
tat[q] = i + bt[q];
}
bt[q] = bt[q] - 1;
q2 = q;
}
time[k] = tbt;
System.out.println();
System.out.print("0\t");
for (int i = 0; i <= k; i++) {
System.out.print(time[i] + "\t");
}
double awt=0;//average waiting time
for(int i=0;i<n;i++)
{
awt=awt+wt[i];
}
awt=awt/n;
System.out.println("\n AVERAGE WAITING TIME"+awt);
return large[0];//returning max number of process in queue at same time
}
static int shortestsizetime(int n,int process[],int ptime[],int wtime[])
{
int temp, total=0;
float avg=0;
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++) {
if(ptime[i]>ptime[j])
{
temp = ptime[i];
ptime[i] = ptime[j];
ptime[j] = temp;
temp = process[i];
process[i] = process[j];
process[j] = temp;
}
}
}
wtime[0] = 0;
for(int i=1;i<n;i++)
{
wtime[i] = wtime[i-1]+ptime[i-1];
total = total + wtime[i];
}
avg = (float)total/n;
System.out.println("P_ID P_TIME W_TIME");
for(int i=0;i<n;i++)
{
System.out.println(process[i]+"\t"+ptime[i]+"\t"+wtime[i]);
}
System.out.println("Total Waiting Time: "+total);
System.out.println("Average Waiting Time: "+avg);
return n-1;
}
public static void main(String argv[])
{
//variable declaration
int n=100;//number of processes
int p[]=new int[n];
int a[]=new int[n];//to store arrival time of 100 processes
int b[]=new int[n];//to store service/burst/processing time of 100 processes
int b2[]=new int[n];//to store service/burst/processing time of 100 processes
int w[]=new int[n];//to store waiting time of 100 processes
int tat[]=new int[n];//to store turaround time of 100 processes
int i,j;
int quanta;//time slice for round robin
double round_robin,fcfs;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the no of processes");
n=sc.nextInt();
System.out.println("Enter the arrival time");
for(i=0;i<=n;i++);
{
a[i]= sc.nextInt();
}
System.out.println("Enter the Burst time");
for(i=0;i<=n;i++)
{
b[i]=sc.nextInt();
if(b[i]<0){ b[i]=b2[i]=-1*b[i];}
}
Random r= new Random(); //to generate random number
//generating randomly arrival time from 0 to 100
for(i=0;i<n;i++)
{
p[i]=i+1;
a[i]=r.nextInt()%101; //randomly number between 0 to 100 inclusive
}
//randomly burst/processing times from 0 to 100]
for(i=0;i<n;i++);
{
b[i]=(int)r.nextInt()%101; //random number from 0 to 100 inclusive
b2[i]=b2[i];
if(b[i]<0){ b[i]=b2[i]=-1*b[i];}
}
String ans1="";
do
{
int c;
String ans="Y";
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1: Shortestfinish time algorithm \n 2: Shortest size algorithm ");
c=sc.nextInt();
if(c==1)
{
System.out.println("Shortest finish time:");
//calling shortest finish time algorithm
System.out.println("Longest number of processes in queue:"+Shortesfinishtime(n,p,a,b,b2,w,tat));
}
else
{
System.out.println("Shortest size:");
//calling shortest size algorithm
System.out.println("Longest number of process in queue:"+shortestsizetime(n,p,b,w));
}
System.out.println("Do you want to continue(y/n)");
ans1 = sc.next();
}while(ans1.equals("y"));
}
}
謝謝
你不需要在你的'do'循環中聲明'Scanner sc = new Scanner(System.in);'。 – Zircon
你在'main()'方法中聲明瞭'Scanner sc'兩次。刪除第二個。 – Andreas