我如何在Java中进行检查,如果变量不是0或更小?

问题描述 投票:0回答:1

如何检查celkovyPocet是否不小于0?接下来是翻译。为什么不扣除这笔钱?

int VyberVojaky; //选择士兵int PocetVojaku; //士兵数量int celkovyPocet = 50; //总金额

do {


     System.out.println("Jaké si vybereš?");//What is your choice?
        System.out.println("Pešák-0(2)"); //Plebs
        System.out.println("Lučištník-1 (5)");//Archer
        System.out.println("Jezdectvo-2 (10)");//Cavalry
       System.out.println("Težká jednotka-3 (20)");//Special unit
       VyberVojaky =  Integer.parseInt(sc.nextLine());//ChooseSoldier

        System.out.println("A kolik jich chceš?");//How many soldiers do you want?
         PocetVojaku = Integer.parseInt(sc.nextLine());//numbofSoldiers
     for(int i=0;i < PocetVojaku;i++) {
         System.out.println(armadaHrace2[VyberVojaky]); //secondPlayersarmy[chooseSoldiers];

     }
        //total amount
        // limit for recruiting soldiers is 50,so if player will recruit more than 50 soldiers,The game should alert the player and select the soldiers again
       if(celkovyPocet > 50 ) {
           System.out.println("Naverboval jsi více jak 50 vojáků");//You've recruited more than 50 soldiers


       }

       celkovyPocet -=  PocetVojaku;//total amount -= numbofSoldiers

       System.out.println("Ještě můžete naverbovat: " +  celkovyPocet + " " + "vojáků");
                          //How many soldiers player can recruit

       Vojaci = armadaHrace2[VyberVojaky];

       Penize -= armadaHrace2[VyberVojaky].getCena();
       //money
       System.out.println("Vybral sis" + armadaHrace2[VyberVojaky]);//Your choice
    System.out.println("Zůstalo ti:" + Penize + " " +"peněz");//Your residue


              //total amount
    }while(  celkovyPocet != 0  );
    System.out.println(armadaHrace2[VyberVojaky]);//second Player's army
java arrays variables do-while
1个回答
0
投票

由于celkovyPocet是整数(int),如果celkovyPocet < 0小于0,则true将返回celkovyPocet,否则,false

© www.soinside.com 2019 - 2024. All rights reserved.