簡體   English   中英

如何進行if-if測試字符串中的內容並刪除或更改內容

[英]How to make an if else-if test for something in a string and delete or change something

我在說的是這樣的:

if (game == tdm) {
  scoret1 = scoret1 - 100;
}

如果有幫助,我的代碼如下:

import java.util.Random;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;
import java.util.Collections;
import java.util.List;

public class codGame {
  public static void main(String[] args) {
    Random me = new Random();
    ArrayList<String> gun = new ArrayList(Arrays.asList("KN-44","XR2","HVK-30","ICR-1","Man-O-War","Sheiva","M8A7"));
    ArrayList<String> player = new ArrayList(Arrays.asList("BlazeDaBlur","CoolCreeper707","BlurWINSTheGame","QTD","DomIsWhiteTiger","AdvisoryStorm","CadenGaming10","Andrew","gotem","tgg","hahsal","TheOneAndTheOnly20"));
    ArrayList<String> attachments = new ArrayList(Arrays.asList("Quickdraw","Grip","Extended Mag","Fast Mag","Rapid Fire","Suppressor","Long Barrel"));
    ArrayList<String> optics = new ArrayList(Arrays.asList("Reflex","Recon","Varix-3","Boa-3","Thermal","ELO"));
    ArrayList<String> maps = new ArrayList(Arrays.asList("Aquarium","Breach","Combine","Evac","Exodus","Fringe","Havoc","Hunted","Infection","Metro","Redwood","Stronghold","Nuk3town"));
    ArrayList<String> modes = new ArrayList(Arrays.asList("TDM","Domination","Free-For-All"));

    List<String> players = Arrays.asList("BlazeDaBlur","CoolCreeper707","BlurWINSTheGame","QTD","DomIsWhiteTiger","AdvisoryStorm","CadenGaming10","Andrew","gotem","tgg","hahsal","TheOneAndTheOnly20");

    Set guns = new HashSet(Arrays.asList(gun));
    Set play = new HashSet(Arrays.asList(player));
    Set att = new HashSet(Arrays.asList(attachments));
    Set opt = new HashSet(Arrays.asList(optics));
    Set map = new HashSet(Arrays.asList(maps));
    Set mode = new HashSet(Arrays.asList(modes));

    Collections.shuffle(players);
    Collections.shuffle(player);
    for (int i=0; i < 11; ++i) {
    player.remove(0);
    }
    Collections.shuffle(gun);
    for (int i=0; i < 6; ++i) {
    gun.remove(0);
    }
    Collections.shuffle(optics);
    for (int i=0; i < 5; ++i) {
    optics.remove(0);
    }
    Collections.shuffle(attachments);
    for (int i=0; i < 5; ++i) {
    attachments.remove(0);
    }
    Collections.shuffle(maps);
    for (int i=0; i < 12; ++i) {
    maps.remove(0);
    }
    Collections.shuffle(modes);
    for (int i=0; i < 2; ++i) {
    modes.remove(0);
    }

    int scoret1 = me.nextInt(200);
    int scoret2 = me.nextInt(200);

    List<String> blackops = players.subList(0, 6);
    List<String> cdp = players.subList(6, 12);

    System.out.println("BlackOps3 game.");
    System.out.println("");
    System.out.println("CDP team: " + cdp);
    System.out.println("");
    System.out.println("Black Ops team: " + blackops);
    System.out.println("");
    System.out.println("You are: " + play);
    System.out.println("");
    System.out.println("Your gun: " + guns + ", your attachments: " + att + ", and your optic: " + opt);
    System.out.println("");
    System.out.println("The map: " + map);
    System.out.println("");
    System.out.println("The mode: " + mode);
    System.out.println("");
    System.out.println("The score: " + scoret1 + " to " + scoret2);
    }
}

為了更詳細一點,如果您曾經玩過BO3,您可能知道Free-For-All的得分限制為30,有時如果我對此進行編譯,則游戲模式為Free-For-All,我得到:得分:180至100,我正在嘗試解決此問題。

您可以像這樣直接內聯:

System.out.println("The score: " + 
(mode.equals("tdm") ? scoret1 - 100 : scoret1) +
" to " + scoret2);

您的問題是當您生成隨機數時,由於您生成的是0到199之間的數字(是的,請閱讀Random類的nextInt()方法,它將返回介於0和給定界限之間的數字,第56行為200和57)。

如果要限制它,則需要在生成數字后檢查,或者根據某些情況限制范圍。 例如,如果用戶可以選擇模式,則假設有一個名為“ gameMode”的變量,它是一個int,並且用戶可以從幾種游戲模式中進行選擇,可以使用if語句或切換結構:

int bound = 200; //default bound for Random.nextint(bound)

switch(userMode){
    case 1: //user picked game mode 1
        bound = 31; //scores between 0 and 30 
        break;
    case 2: //game mode 2
        bound = 61; //scores between 0 and 60
        break;
    case 3: //game mode 3
        bound = 101;
        break;
    //no default, we already have a default of 200 as the bound
}

您還可以以類似的方式使用if-else語句,這實際上取決於多少個模式,我想說,如果您有2-3個以上的模式,則最好使用switch結構或if-else之外的其他方式,但是不要非常重要。

int bound = 200;

if(userMode == 1)
    bound = 31;
else if(userMode == 2)
    bound = 61;
else if(userMode == 3)
    bound = 101;

哎呀,您甚至可以使用您的小Random對象來隨機選擇游戲模式,您可以在if-else或case語句中添加更多代碼,以預定義“ gameModeString”來保存游戲模式的字符串。

同樣,對隨機數使用模運算符也會限制其范圍。 模運算符在將左操作數除以右后返回余數。 IE 200%30將返回20。

使用模數將始終遵循以下形式:

%B在0到B-1之間,因此任何模數30都將返回0到29之間的數字。您可以對隨機數進行mod運算,例如:

int score1 = (me.nextInt(200) % 31); //returns integer between 0 and 30

就是這樣 我建議多讀一些隨機數生成器(或細讀Java中的Random API)和模數運算。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM