2016-11-28 8 views
-3

私は単純なゲームをコードするために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; 
+0

コードが壊れています。コードの関連コンポーネントを含めてください。 – msagala25

+0

あなたが提供したコードは不完全です。 'phystrength'はmed、med1、med2、med3のメンバーです。 – DineMartine

+0

あなたの変数の名前をもっと意図してください。あなたはあなたの変数に '医者'と '看護師 'を持つことができます。次に、 'getStrength()'メソッドを宣言してそれぞれの現在の値を取得することができます。 –

答えて

0

class physician { 
    int phystrength; 
    int restoring; 
    String available; 
} 

class surgeon { 
    int phystrength; 
    int restoring; 
    String available; 
}  

これらはあなたが新しいクラスを作成しないように、あなたは一つのクラスのnewインスタンスを作成し、まったく同じオブジェクトです。

したがって、これらのうちの1つだけです。

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; 
+0

ありがとうございます!今はもっと明らかです。私のプログラミング能力を向上させるための初心者プログラマーとして私にお勧めするヒントやリソースはありますか? –

+0

公式のチュートリアルを読んでいるよりも、試行錯誤したほうがはるかに良くなりません。 https://docs.oracle。com/javase/tutorial /もちろん、誰もがその方法を学ぶことはできません。ビデオ、または適切な教育環境が良いかもしれません。 –

+0

ありがとうございます!私はウェブサイトを試みます。また助けてくれてありがとう! –

関連する問題