繁体   English   中英

开关未执行

[英]Switch not executing

我不确定是否曾问过这个问题或与之类似的问题,但我进行了搜索,但一无所获。 我刚刚在学校开始编程,但是老师的项目给了我们,我可以很快完成,而且有点太简单了,所以我开始研究Oracle的Java教程。 经过大量的实验,我决定制作一个基于MGSV TPP的基于文本的隐形游戏。 无论如何,这已经足够了。 我的问题是代码中的开关没有执行。 娜达 我已经一遍又一遍地查看我的代码,但是我不能出什么问题。 这是我第一次使用开关。 这是带有开关的代码片段:

public void shoot() {
    Random rand = new Random();
    String farshot = this.shoot[rand.nextInt(this.shoot.length)];
    if(reload == 1) {
     if(supressor > 1) {
        if(dbme<=75) {
            System.out.println("Great Job! He's out cold!");
                if(dbme == Guard1)
                    Guard1 = 0;
                else if(dbme == Guard2)
                    Guard2 = 0;
                else if(dbme == Guard3)
                    Guard4 = 0;
                else if(dbme == Guard4)
                    Guard4 = 0;
                else if(dbme == Guard5)
                    Guard5 = 0;
                else if(dbme == Guard6)
                    Guard6 = 0;
                else if(dbme == Guard7)
                    Guard7 = 0;
                else if(dbme == Guard8)
                    Guard8 = 0;
                dbme = 0;
                reload = 0;
                supressor--; 
        }
      } else {  System.out.println("You're suppressor had worn out! You alerted the guards! But Great shot, you knocked him out!");                 
                if(dbme == Guard1)
                    Guard1 = 0;
                else if(dbme == Guard2)
                    Guard2 = 0;
                else if(dbme == Guard3)
                    Guard4 = 0;
                else if(dbme == Guard4)
                    Guard4 = 0;
                else if(dbme == Guard5)
                    Guard5 = 0;
                else if(dbme == Guard6)
                    Guard6 = 0;
                else if(dbme == Guard7)
                    Guard7 = 0;
                else if(dbme == Guard8)
                    Guard8 = 0;
                dbme = 0;
                reload = 0;
                supressor--;
                alert = 1;
                actions();
        }
            if(dbme > 75)
                System.out.println("He's too faraway. but...");
            switch (farshot) {
                case "headshot":
                    System.out.println("Wow! You're a great shot! He's out cold! He's now unmarked");
                    if(dbme == Guard1)
                        Guard1 = 0;
                    else if(dbme == Guard2)
                        Guard2 = 0;
                    else if(dbme == Guard3)
                        Guard4 = 0;
                    else if(dbme == Guard4)
                        Guard4 = 0;
                    else if(dbme == Guard5)
                        Guard5 = 0;
                    else if(dbme == Guard6)
                        Guard6 = 0;
                    else if(dbme == Guard7)
                        Guard7 = 0;
                    else if(dbme == Guard8)
                        Guard8 = 0;
                    dbme = 0;
                    break;
                case "miss":
                    if(supressor != 0)
                        System.out.println("YOU MISSED! But Your shot was suppressed, so you're safe!");
                    else {
                        System.out.println("YOU MISSED! You've alerted everyone of your presence!!!!");
                        reload = 0;
                        combat(); 
                    }
                    break;
                case "alert":
                    System.out.println("It wasn't a headshot, but he doesn't know anything yet. He's alerted, so becareful!");
                    break;
                }
            dbme = 0;
            actions();
                reload = 0;
    } else { System.out.println("Reload your gun first!");
             markedAction();
    }
}

我不确定您是否还需要其他任何内容,但这是完整的代码:

package metalgear;

import java.util.Scanner;
import java.util.Random;

public class MetalGear {
     //Arrays
     String[] objects = {"Food, Water, Ammo, Suppressor, Mags"};
     String[] shoot = {"miss, headshot, alert"};

    //Player
     int health = 150;
     int ammo = 10;
     int food = 5; //5 packets
     int water = 2; //2 liters
     int damage = 20;
     int xp;
     int reload;
     int supressor = 10;
     int mags = 1;
     int dbme;
     String recon = "";

    //Enemy
     int distanceFromEnemyGuard = 200;
     int returnDamage;
     int foodDrop = 30;
     int waterDrop = 20;
     int ammoDrop = 50;
     int emags = 15;
     int esupp = 10;
     int alert = 0;

     int Guard1 = -1;
     int Guard2 = -1;
     int Guard3 = -1;
     int Guard4 = -1;
     int Guard5 = -1;
     int Guard6 = -1;
     int Guard7 = -1;
     int Guard8 = -1;

     int locations = 0;
     public  void enter() {
        java.util.Scanner keyboard = new java.util.Scanner(System.in);
    keyboard.nextLine();
    }

     public static void main(String[] args) {
        MetalGear a=new MetalGear();
        a.intro();
        a.Page1();
     }

      public  void intro()
    {
        java.util.Scanner input = new java.util.Scanner(System.in);
            String x;
        System.out.println("Welcome to a Text-Based Mission of MGSV-The Phantom Pain");
        System.out.print("Enter your name... ");
            x = input.next();
        System.out.println("");
        System.out.print("Hello " + x + ". " + "I'm Ocelot, and I'll be breifing you on your mission today. [Press Enter]");
    enter();
    System.out.print("Your job is to succesfully infiltrate the Soviet outpost located ");
    enter();
    System.out.print("at the village of Di Wialo Kalai in Afghanistan.");
            enter();
            System.out.print("Currently, you have 150 health, you do 20 damage,");
            enter();
            System.out.print("Have 10 bullets in your gun, and 5 food packets,");
            enter();
            System.out.print("2 liters of water, and 1 magazine(10 bullets) in your ");
            enter();
            System.out.println("Backpack. Use your supplies wisely.");
            System.out.println("");
}
              public  void Page1() {
        System.out.println("The chopper has dropped you at the best recon point at the edge of Di Wialo Kalai");
            actions();
              }
      //Action Methods
      public  void actions() {
            Scanner in = new Scanner(System.in);
            String y;
            System.out.println("");
            System.out.println("Now, You... (Type help for a list of commands)");
            y = in.next();
            if(y.equalsIgnoreCase("help"))
                help();
                else if (y.equalsIgnoreCase("Recon")) 
                recon();
                else if (y.equalsIgnoreCase("Reload")) 
                    reload();
                else if (y.equalsIgnoreCase("Backpack"))
                    backpack();
                else if (y.equalsIgnoreCase("iDroid"))
                    iDroid();
                else if (y.equalsIgnoreCase("Mark"))
                    marking();
                else {
                    System.out.println("You can't do that!");
                    System.out.println("");
                    actions();
            }
    }

    public void backpack() {
               Scanner in = new Scanner(System.in);
                String x;
                boolean n = true;
                while(n) {
                    System.out.println("You have " + this.food + " packets");
                System.out.println("You have " + this.water + " liters of water");
                System.out.println("You have " + this.mags + " magazines");
                    System.out.println("You can 'eat', 'drink', or 'load mag', or exit");
                    x = in.next();
                if(x.equalsIgnoreCase("eat")) {
                    if(this.food >= 1) {
                    this.food--;
                    this.health += 30;
                        System.out.println("");
                        System.out.println("You now have " + health + " health");}
                    else  {
                        System.out.println(""); System.out.println("You don't have enough food"); }
                    }
                    else if(x.equalsIgnoreCase("drink")) {
                        this.damage +=5;
                        this.water--;
                        System.out.println("");
                        System.out.println("You will now do " + damage + " damage"); }
                        else if(x.equalsIgnoreCase("load mag")) {
                            this.ammo = 10;
                            this.mags--;
                            System.out.println(""); System.out.println("Your now has full ammo(20 shots)");
                            System.out.println("");
                        }
                        else if(x.equalsIgnoreCase("exit")) 
                            actions();
                        else System.out.println("YOu can't do that!"); System.out.println("");
                backpack(); } 
    }
    public  void iDroid() {
        Scanner in = new Scanner(System.in);
        System.out.println("You have " + health + " health left");
        System.out.println("You still have " + supressor + " suppressed shots left.");
        System.out.println("You can currently do " + damage + " damage");
        System.out.println("And your gun's magazine currently has " + ammo + " ammo left" );
        if(!"".equals(recon))
            System.out.println(recon);
        else
            System.out.println("You haven't obtained any intel files yet");
        System.out.println("");
        if((Guard1 != 1) && (Guard2 != 1) && (Guard3 != 1) && (Guard4 != 1) && (Guard5 != 1) &&  (Guard6 != 1) && (Guard7 != 1) && (Guard8 != 1)) {    
            if(Guard1 == 0) System.out.println("Guard 1 was eliminated"); else System.out.println("Guard 1 is " + Guard1 + " meters away."); 
          if(Guard2 == 0) System.out.println("Guard 2 was eliminated"); else System.out.println("Guard 2 is " + Guard2 + " meters away."); 
           if(Guard3 == 0) System.out.println("Guard 3 was eliminated"); else System.out.println("Guard 3 is " + Guard3 + " meters away.");
          if(Guard4 == 0) System.out.println("Guard 4 was eliminated"); else System.out.println("Guard 4 is " + Guard4 + " meters away."); 
           if(Guard5 == 0) System.out.println("Guard 5 was eliminated"); else System.out.println("Guard 5 is " + Guard5 + " meters away."); 
          if(Guard6 == 0) System.out.println("Guard 6 was eliminated"); else System.out.println("Guard 6 is " + Guard6 + " meters away."); 
           if(Guard7 == 0) System.out.println("(Guard 7 was eliminated"); else System.out.println("Guard 7 is " + Guard7 + " meters away."); 
          if(Guard8 == 0) System.out.println("Guard 8 was eliminated"); else System.out.println("Guard 8 is " + Guard8 + " meters away.");  }
          else System.out.println("You haven't done any recon yet!");
        System.out.println("");
          if(locations != 0)
              marking();
          else actions();
    }
    public  void reload() {
            if (this.ammo>1) {
               if(this.reload==0) {
                   this.reload++;
                   this.ammo--;
                   System.out.println("Locked and Loaded!");
                   System.out.println("You now have " + ammo + " ammo left");
                   System.out.println("");
               }
                   else if(this.reload==1) {
                   System.out.println("You're gun is already loaded!");
                   System.out.println(""); }

            }
    else { System.out.println("You don't have any ammo!"); 
        System.out.println(""); }
                   if(locations == 2)
                    markedAction();
                   else actions();            
    }

    public  void help() {
                   System.out.println("Recon-Spots the number of enemies and each of their distances from you");
                   System.out.println("Reload- Relaod your weapon. YOu need to do so after EACH shot.");
                   System.out.println("Backpack- Look, equip, or use your supplies");
                   System.out.println("iDroid- Tells you your health, your reconed enemies, and intel you have obtained.");
                   System.out.println("Mark- Marks a Guard, allowing for interaction with that Guard");
                   System.out.println("");
                   actions();
               }
    public  void recon() {
            Scanner in = new Scanner(System.in);
            Random rand = new Random();
             if((Guard1 == -1) && (Guard2 == -1) && (Guard3 == -1) && (Guard4 == -1) && (Guard5 == -1) &&  (Guard6 == -1) && (Guard7 == -1) && (Guard8 == -1)) {    
             Guard1 = rand.nextInt(this.distanceFromEnemyGuard);
             Guard2 = rand.nextInt(this.distanceFromEnemyGuard);
             Guard3 = rand.nextInt(this.distanceFromEnemyGuard);
             Guard4 = rand.nextInt(this.distanceFromEnemyGuard);
             Guard5 = rand.nextInt(this.distanceFromEnemyGuard);
             Guard6 = rand.nextInt(this.distanceFromEnemyGuard);
             Guard7 = rand.nextInt(this.distanceFromEnemyGuard);
             Guard8 = rand.nextInt(this.distanceFromEnemyGuard);
            System.out.println("Guard 1 is " + Guard1 + " meters away.");
            System.out.println("Guard 2 is " + Guard2 + " meters away.");
            System.out.println("Guard 3 is " + Guard3 + " meters away.");
            System.out.println("Guard 4 is " + Guard4 + " meters away.");
            System.out.println("Guard 5 is " + Guard5 + " meters away.");
            System.out.println("Guard 6 is " + Guard6 + " meters away.");
            System.out.println("Guard 7 is " + Guard7 + " meters away.");
            System.out.println("Guard 8 is " + Guard8 + " meters away.");
            actions();
             }
             else   System.out.println("You already reconed the guards! Check your iDroid for their locations!");
             actions();
    }

    public void marking() {
        locations = 1;
        Scanner in = new Scanner(System.in);
        String abs;
        if ((Guard1 != -1) && (Guard2 != -1) && (Guard3 != -1) && (Guard4 != -1) && (Guard5 != -1) &&  (Guard6 != -1) && (Guard7 != -1) && (Guard8 != -1))  {
            System.out.println("Which Guard would you like to mark?");
                abs = in.next();
            if(abs.equalsIgnoreCase("1")) 
                dbme = Guard1;
            else if(abs.equalsIgnoreCase("2")) 
                dbme = Guard2;
            else if(abs.equalsIgnoreCase("3"))
                dbme = Guard2;
            else if(abs.equalsIgnoreCase("4"))
                dbme = Guard4;
            else if(abs.equalsIgnoreCase("5"))
                dbme = Guard5;
            else if(abs.equalsIgnoreCase("6"))
                dbme = Guard6;
            else if(abs.equalsIgnoreCase("7"))
                dbme = Guard7;
            else if(abs.equalsIgnoreCase("8"))
                dbme = Guard8;
            else if(abs.equalsIgnoreCase("idroid")) {
                iDroid();
            }
            else if(abs.equalsIgnoreCase("exit")) {
                System.out.println("No one was marked");
                locations = 0;
                actions();
            }
            else { System.out.println("That's not a Guard!");
                   marking();
                }
        }
        else { System.out.println("Do some Recon first! YOu can't mark guards without knowing their locations!");
             locations = 0;
             actions(); }      
        System.out.println("");
        if(dbme != 0)
            markedAction();

    }
    //----------------------------------------------------------------------------------------------------------
    //Marked ACtions
    public void markedAction() {
        locations = 2;
        Scanner in = new Scanner(System.in);
        System.out.println("The Guard has been marked. (Type 'help' for another list of commands)");
        String x;
        x = in.next();
        if(x.equalsIgnoreCase("shoot"))
            shoot();
        else if(x.equalsIgnoreCase("CQC"))
            CQC();
        else if(x.equalsIgnoreCase("sneak"))
            sneak();
        else if(x.equalsIgnoreCase("unmark")) {
            System.out.println("The Guard has been unmarked");
            dbme = 0;
            System.out.println("Returning to Actions");
            actions();
        }
            else if(x.equalsIgnoreCase("reload"))
                reload();
            else if(x.equalsIgnoreCase("help"))
                help2();

            else { System.out.println("You can't do that"); markedAction();
            }

        locations = 0;

    }
    public void shoot() {
    Random rand = new Random();
    String farshot = this.shoot[rand.nextInt(this.shoot.length)];
    if(reload == 1) {
     if(supressor > 1) {
        if(dbme<=75) {
            System.out.println("Great Job! He's out cold!");
                if(dbme == Guard1)
                    Guard1 = 0;
                else if(dbme == Guard2)
                    Guard2 = 0;
                else if(dbme == Guard3)
                    Guard4 = 0;
                else if(dbme == Guard4)
                    Guard4 = 0;
                else if(dbme == Guard5)
                    Guard5 = 0;
                else if(dbme == Guard6)
                    Guard6 = 0;
                else if(dbme == Guard7)
                    Guard7 = 0;
                else if(dbme == Guard8)
                    Guard8 = 0;
                dbme = 0;
                reload = 0;
                supressor--; 
        }
      } else {  System.out.println("You're suppressor had worn out! You alerted the guards! But Great shot, you knocked him out!");                 
                if(dbme == Guard1)
                    Guard1 = 0;
                else if(dbme == Guard2)
                    Guard2 = 0;
                else if(dbme == Guard3)
                    Guard4 = 0;
                else if(dbme == Guard4)
                    Guard4 = 0;
                else if(dbme == Guard5)
                    Guard5 = 0;
                else if(dbme == Guard6)
                    Guard6 = 0;
                else if(dbme == Guard7)
                    Guard7 = 0;
                else if(dbme == Guard8)
                    Guard8 = 0;
                dbme = 0;
                reload = 0;
                supressor--;
                alert = 1;
                actions();
        }
            if(dbme > 75)
                System.out.println("He's too faraway. but...");
            switch (farshot) {
                case "headshot":
                    System.out.println("Wow! You're a great shot! He's out cold! He's now unmarked");
                    if(dbme == Guard1)
                        Guard1 = 0;
                    else if(dbme == Guard2)
                        Guard2 = 0;
                    else if(dbme == Guard3)
                        Guard4 = 0;
                    else if(dbme == Guard4)
                        Guard4 = 0;
                    else if(dbme == Guard5)
                        Guard5 = 0;
                    else if(dbme == Guard6)
                        Guard6 = 0;
                    else if(dbme == Guard7)
                        Guard7 = 0;
                    else if(dbme == Guard8)
                        Guard8 = 0;
                    dbme = 0;
                    break;
                case "miss":
                    if(supressor != 0)
                        System.out.println("YOU MISSED! But Your shot was suppressed, so you're safe!");
                    else {
                        System.out.println("YOU MISSED! You've alerted everyone of your presence!!!!");
                        reload = 0;
                        combat(); 
                    }
                    break;
                case "alert":
                    System.out.println("It wasn't a headshot, but he doesn't know anything yet. He's alerted, so becareful!");
                    break;
                }
            dbme = 0;
            actions();
                reload = 0;
    } else { System.out.println("Reload your gun first!");
             markedAction();
    }
}
    public void CQC() {
    }
    public void sneak() {
    }
    public void combat() {
    }
    public void help2() {
    }
}

感谢您的所有回复!

在代码的顶部,您声明了字符串数组,但每个数组仅包含一个字符串:

String[] objects = {"Food, Water, Ammo, Suppressor, Mags"};
String[] shoot = {"miss, headshot, alert"};

因此,当您初始化farshot变量时

String farshot = this.shoot[rand.nextInt(this.shoot.length)];

它将始终包含值miss, headshot, alert 这与您要测试的任何情况都不匹配。

我假设您要执行以下操作:

String[] objects = {"Food", "Water", "Ammo", "Suppressor", "Mags"};
String[] shoot = {"miss", "headshot", "alert"};

不知道它是否可以解决您的所有问题,但这只是一个开始。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM