重用开关/案例中的变量存储值

奥·埃尔南德斯(O. Hernandez)

我正在使用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;
板球运动员

首先,这很糟糕,

 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;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章