私は単純なゲームをコードするためにJavaを使用しています。私はスイッチを使用しており、計算が完了した後に得られた値を再利用したいと考えています。また、ユーザーが同じオプションを再度選択すると、メニューが再び表示されます。これは前の値を格納し、初期値の代わりにその値を使用して計算を行います。 「phystrength」変数はスイッチ/ケース内の変数格納値の再利用
この0は、私が書いたコードの一部です等しいときにゲームが終了します。
インポート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;
コードが壊れています。コードの関連コンポーネントを含めてください。 – msagala25
あなたが提供したコードは不完全です。 'phystrength'はmed、med1、med2、med3のメンバーです。 – DineMartine
あなたの変数の名前をもっと意図してください。あなたはあなたの変数に '医者'と '看護師 'を持つことができます。次に、 'getStrength()'メソッドを宣言してそれぞれの現在の値を取得することができます。 –