7-25 雨刷程序功能扩展设计 (100 分)
在給定的汽車手動風擋玻璃雨刷程序的基礎上,對程序進行重構(Refactoring),使得程序可以對功能進行擴展。
輸入格式:
輸入共2行,第一行為一個整型數字,取值范圍為[1,2],其中1代表表1所描述的雨刷系統,2代表表2所描述的雨刷系統;第二行為若干個用一個或多個空格分開且以數字0結束的整型數字,取值范圍為[1,4],其中1代表控制桿升檔操作、2代表控制桿降檔操作、3代表刻度盤升刻度操作、4代表刻度盤降刻度操作、0代表操作結束(輸入時只要遇到0即認為輸入結束)。
輸出格式:
程序的輸出行數根據每一次對控制桿/刻度盤操作次數而定,每一次對控制桿/刻度盤進行了操作,則輸出一行數據。格式為:操作類型/控制桿當前檔位/刻度盤當前刻度/雨刷當前速度 其中,操作類型共四種,分別為Lever up、Lever down、Dial up、Dial down;控制桿當前檔位顯示中文內容,例如停止、間歇、低速、高速、超高速(表2);刻度盤當前刻度顯示為數值,例如1、2、3、4、5(4、5見表2);雨刷當前速度顯示為整型數值。
輸入樣例:
在這里給出一組輸入。例如:
輸出樣例:
在這里給出相應的輸出。例如:
Lever up/間歇/1/4
Lever up/低速/1/30
Lever up/高速/1/60
Lever down/低速/1/30
Dial up/低速/2/30
Lever down/間歇/2/6
Dial down/間歇/1/4
Dial up/間歇/2/6
Dial up/間歇/3/12
Lever up/低速/3/30
Lever down/間歇/3/12
想半天發現源碼在文件里
import java.util.Scanner; public class Main {public static void main(String[] args){int choice = 0;Scanner input = new Scanner(System.in);choice = input.nextInt();String[] strings = null;int[] ints=null;//根據不同的檔位要求進行換擋if (choice==1) {strings=new String[]{"停止","間歇","低速","高速"};ints=new int[]{1,2,3};}else if (choice==2){strings=new String[]{"停止","間歇","低速","高速","超高速"};ints=new int[]{1,2,3,4,5};}else {System.out.println("Wrong Format");System.exit(0);}Lever lever = new Lever(1);Dial dial = new Dial(1);Brush brush = new Brush(0);//此處傳入對應不同檔位所允許的最大值lever.setMod(strings.length);dial.setMod(ints.length);//Agent agent = new Agent(lever,dial,brush);choice= input.nextInt();while(choice != 0){switch(choice){case 1://Lever upagent.getLever().leverUp();System.out.print("Lever up/");break;case 2://Lever downagent.getLever().leverDown();System.out.print("Lever down/");break;case 3://Dial upagent.getDial().dialUp();System.out.print("Dial up/");break;case 4://Dial downagent.getDial().dialDown();System.out.print("Dial down/");break;case 0://TerminateSystem.exit(0);}if (choice>=1&&choice<=4){agent.dealSpeed();//Get brush’s speedSystem.out.println(strings[agent.getLever().getPos()-1]+"/"+ints[agent.getDial().getPos()-1]+"/"+agent.getBrush().getSpeed());}choice = input.nextInt();}} } class Lever {private int pos;//檔位private int mod;public Lever(){}public Lever(int pos){this.pos = pos;}public int getPos() {return pos;}public void setMod(int mod) {this.mod = mod;}//升檔public void leverUp() {if(this.pos < mod){this.pos ++;}}//降檔public void leverDown(){if(this.pos > 1){this.pos --;}} } class Dial {private int pos;//刻度private int mod;public Dial(){}public Dial(int pos){this.pos = pos;}public int getPos() {return pos;}public void setMod(int mod) {this.mod = mod;}//升刻度public void dialUp() {if(this.pos < mod){this.pos ++;}}//降刻度public void dialDown(){if(this.pos > 1){this.pos --;}} } class Brush {private int speed;public Brush(){}public Brush(int speed){this.speed = speed;}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;} } //為了減小實體類耦合性,采用中介模式,設計Agent代理類 class Agent {//聚合型類設計private Lever lever;private Dial dial;private Brush brush;public Agent(){}public Agent(Lever lever,Dial dial,Brush brush){this.lever = lever;this.dial = dial;this.brush = brush;}public Lever getLever() {return lever;}public void setLever(Lever lever) {this.lever = lever;}public Dial getDial() {return dial;}public void setDial(Dial dial) {this.dial = dial;}public Brush getBrush() {return brush;}public void setBrush(Brush brush) {this.brush = brush;}//主要業務邏輯:根據控制桿檔位、刻度盤刻度計算雨刷擺動速度public void dealSpeed() {int speed = 0;switch (this.lever.getPos()) {case 1://停止speed = 0;break;case 2://間歇switch (this.dial.getPos()) {case 1://刻度1speed = 4;break;case 2://刻度2speed = 6;break;case 3://刻度3speed = 12;break;case 4:speed = 15;break;case 5:speed = 20;break;}break;case 3://低速speed = 30;break;case 4://高速speed = 60;break;case 5:speed = 90;break;}this.brush.setSpeed(speed);} }總結
以上是生活随笔為你收集整理的7-25 雨刷程序功能扩展设计 (100 分)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 光与夜之恋陪伴系统开启攻略
- 下一篇: 剑网三手游怎么自绝经脉