1.随机生成一个访问磁道顺序
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class Course {
private Map<Integer,Integer> map;
int quantity;
public int getQuantity() {
return quantity;
}
public Course() {
map=new HashMap<>();
quantity=(new Random().nextInt(20))+1;//数量
for (int i = 0; i < quantity; i++) {
int n=new Random().nextInt(100);//磁道数
if(map.containsValue(n)){
i--;
}else{
map.put(i,n);
}
}
}
public Map<Integer, Integer> getMap() {
return map;
}
}
2.实现
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
public class Manage {
private static int location;
private static int choose=1;
private static Map<Integer,Integer> map;
private static int quantity;
public static void main(String[] args) {
location = new Random().nextInt(100);
Course course = new Course();
while (choose != 5) {
map=new HashMap<>();
for (Map.Entry<Integer,Integer> entry:course.getMap().entrySet()) {
int key=entry.getKey();
int value=entry.getValue();
map.put(key,value);
}
System.out.println(map);
System.out.println("磁盘调度算法选择");
System.out.println("请选择内存分配策略:1.先来先服务算法 2.最短寻道时间优先算法 3.扫描算法 4.循环扫描算法 5.退出(请输入一个数字1~4)");
Scanner scanner = new Scanner(System.in);
choose = scanner.nextInt();
int distance=0;
int now_location=location;
quantity=course.getQuantity();
if(choose==1){
for (int i = 0; i <quantity; i++) {
int value= map.get(i);
System.out.print("目前正在"+now_location+"道 ");
distance+=Math.abs(now_location-value);
now_location=value;
}
System.out.print("目前正在"+now_location+"道 ");
System.out.println("\n"+"寻道长度为"+distance);
map.clear();
}else if(choose==2){
for (int i = 0; i < quantity; i++) {
int min=200;
int key=0;
int value;
for (Map.Entry<Integer,Integer> entry:map.entrySet()) {
int x=Math.abs(entry.getValue()-now_location);
if(x<min){
min=x;
key=entry.getKey();
}
}
System.out.print("目前正在"+now_location+"道 ");
value= map.get(key);
distance+=Math.abs(now_location-value);
now_location=value;
map.remove(key);
}
System.out.print("目前正在"+now_location+"道 ");
System.out.println("\n"+"寻道长度为"+distance);
}else if(choose==3){
int flag=0;//1为正数,2为负数
for (int i = 0; i < quantity; i++) {
int min=200;
int key=-1;
int value;
int x;
int y;
for (Map.Entry<Integer,Integer> entry:map.entrySet()) {
x = entry.getValue() - now_location;
y = Math.abs(x);
if(flag==0) {
if (y < min) {
min = y;
key = entry.getKey();
}
}else if(flag==1&&x>0){
if (y < min) {
min = y;
key = entry.getKey();
}
}else if(flag==2&&x<0){
if (y < min) {
min = y;
key = entry.getKey();
}
}
}
if(key!=-1){
if((map.get(key)-now_location)>0){
flag=1;
}else {
flag=2;
}
}else{
flag=flag==1?2:1;
i--;
continue;
}
System.out.print("目前正在"+now_location+"道 ");
value= map.get(key);
distance+=Math.abs(now_location-value);
now_location=value;
map.remove(key);
}
System.out.print("目前正在"+now_location+"道 ");
System.out.println("\n"+"寻道长度为"+distance);
}else if(choose==4){
//规定磁头道数单向由小到大移动
for (int i = 0; i < quantity; i++) {
int min=200;
int key=-1;
int value;
int x;
int y;
for (Map.Entry<Integer,Integer> entry:map.entrySet()) {
x = entry.getValue() - now_location;
y = Math.abs(x);
if(x>0) {
if (y < min) {
min = y;
key = entry.getKey();
}
}
}
if(key==-1){
distance+=(99+99-now_location);
now_location=0;
i--;
continue;
}
System.out.print("目前正在"+now_location+"道 ");
value= map.get(key);
distance+=Math.abs(now_location-value);
now_location=value;
map.remove(key);
}
System.out.print("目前正在"+now_location+"道 ");
System.out.println("\n"+"寻道长度为"+distance);
}else{
continue;
}
}
System.out.println("退出");
}
}