我开发了一个平均等待时间程序,让用户输入进程,到达时间等。然后它告诉你需要多长时间,但它看起来有错误,我不知道如何解决它。有人能帮我吗。代码的错误行是:
Scanner sc = new Scanner(System.in);
错误在线程" main"中读取异常。 java.lang.Error:未解决的编译问题: 重复的局部变量sc
这是代码 package project3;
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"));
}
}
谢谢
答案 0 :(得分:1)
在main方法中,您声明一个新的Scanner sc两次。最初,第二次在do循环中。
要在do循环中解决此问题,只需删除此行:
Scanner sc = new Scanner(System.in);
正如您已经在上面声明的那样
答案 1 :(得分:1)
我相信错误信息已经足够清楚了。变量sc在同一代码块中的2个位置定义。一个在157号线,另一个在198号线。 我建议使用类似eclipse的IDE进行编码以避免此类错误。
答案 2 :(得分:0)
无需两次声明扫描仪。如果您这样做,请以不同的名称命名。
例如:
<datalist id="languages">
<option value="HTML">
<option value="CSS">
<option value="JavaScript">
<option value="Java">
<option value="Ruby">
<option value="PHP">
<option value="Go">
<option value="Erlang">
<option value="Python">
<option value="C">
<option value="C#">
<option value="C++">
</datalist>
<input type="text" list="languages">
第二
Scanner sc = new Scanner(System.in);
您需要声明两次的唯一原因之一是关闭扫描仪。在您声明新扫描仪之前,关闭扫描仪将不允许再次调用它。
Scanner keyboard = new Scanner(System.in);