我使用Java编写一个简单的游戏代码。我正在使用一个开关,并希望重新使用计算完成后获得的值,并再次显示菜单供用户选择,前提是用户再次选择相同的选项。这是存储前一个值并使用它而不是初始值来进行计算。游戏结束时,#phystrength"变量等于0.
这是我编写的代码的一部分:
import java.util.Scanner; 公共课hw3 {
public static void main(String []args) {
class physician {
int phystrength;
int restoring;
String available;
}
physician med = new physician ();
med.phystrength=100;
med.restoring=30;
med.available="busy";
class surgeon {
int phystrength;
int restoring;
String available;
}
surgeon med1 = new surgeon ();
med1.phystrength=100;
med1.restoring=25;
med1.available="busy";
class nurses {
int phystrength;
int restoring;
String available;
}
nurses med2 = new nurses ();
med2.phystrength=90;
med2.restoring=20;
med2.available="busy";
class anesthesia {
int phystrength;
int restoring;
String available;
}
anesthesia med3 = new anesthesia ();
med3.phystrength=100;
med3.restoring=30;
med3.available="busy";
//Declaring variables
int strength, strength1,strength2=0 ;
int numsurgeons=3;
int numphysician= 3;
int numnurses=10;
int numanesthesia=2;
int remsurgeons,remphysician,remnurses,remanesthesia=0;
Scanner scan = new Scanner (System.in); //Creating a Scanner object to read use input
System.out.println("Welcome to National Cheng Kung University (NCKU) Hospital");
System.out.println();
System.out.println("Here is the list of current available medical staff at NCKU hospital");
System.out.println();
System.out.println("\nAvaialable personnel are (including their physical strength):"
+"\n\t 3 Surgeons -> " + med1.phystrength
+ "\n\t 3 Physicians -> " + med.phystrength
+"\n\t 10 Nurses -> " + med2.phystrength
+ "\n\t 2 anesthesiologists -> " + med3.phystrength);
System.out.println("Please enter the NUMBER of the type of medical treatment you are in need of");
String selec;
while (true) { //Allows user to continue to choose from the menu
System.out.println ("\nPlease choose from the menu below:"
+ "\n\t Medical Treatment-0"
+ "\n\t Dressing Wrap-1"
+ "\n\t Surgery-2"
+ "\n\t Chemotherapy-3"
+ "\n\t Emergency-Surgery-4"
+ "\n\t First Aid-5"
+ "\n\t End Program-6");
selec = scan.next();
switch (selec)
{
case "0":
System.out.println("You have chosen Medical Treatment (General)");
strength = med.phystrength-20;
strength1=med2.phystrength-10;
remphysician=numphysician-1;
remnurses=numnurses-1;
if (strength <100){
System.out.println("Physician is: " + med.available);
}
System.out.println("Physician's current Physical strength is:" + strength);
System.out.println("Nurse's current physical strength is:"+ strength1);
System.out.println("Number of remaining Physicians is " + remphysician +" and number of remaining nurses is " + remnurses);
break;
答案 0 :(得分:0)
首先,这很糟糕,
class physician {
int phystrength;
int restoring;
String available;
}
class surgeon {
int phystrength;
int restoring;
String available;
}
这些是完全相同的对象,因此您不创建新类,而是创建一个类的new
个实例。
所以,只有其中一个。
class Medic {
public int phystrength, restoring;
boolean isAvailable; // true/false makes more sense than "busy" / "available"
public Medic (int strength, int restoring, boolean available) {
this.phystrength = strength;
this.restoring = restoring;
this.isAvailable = available;
}
}
其中很多。
Medic physician = new Medic(100, 30, false);
Medic surgeon = new Medic(100, 25, false);
// ... etc.
现在,就switch语句而言,尝试扫描整数,因为这是你提示的内容。
selec = scan.nextInt();
scan.nextLine(); // clear the newline
switch (selec) {
case 0: // Case on integers, instead
System.out.println("You have chosen Medical Treatment (General)");
如果您想修改“员工”的状态,您实际上需要直接更改其状态,而不是存储单独的变量。
physician.phystrength -= 20;
nurse.phystrength -= 10; // "x -= y" --> "x = x - y"
remphysician=numphysician-1;
remnurses=numnurses-1;