如何对字符串中的某些内容进行if else-if测试并删除或更改某些内容

时间:2017-08-15 23:41:37

标签: java arrays random int set

我所说的是这样的:

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,我试图解决这个问题。

2 个答案:

答案 0 :(得分:0)

你可以像这样内联:

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

答案 1 :(得分:0)

您的问题是当您生成随机数时,因为您正在生成0到199之间的数字(是的,读取Random类的nextInt()方法,它返回0到给定范围之间的数字,第56和57行是200。

如果要限制它,则需要在生成数字后进行检查,或者根据某些内容限制范围。例如,如果用户可以选择模式,那么请说出一个名为&#34; gameMode&#34;的变量。这是一个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种,最好使用开关结构或者除了以下情况之外的其他方式 - 否则,但它并不重要。

int bound = 200;

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

哎呀,你甚至可以使用你的小Random对象随机选择游戏模式,你可以在if-else或case语句中添加更多代码来预定义&#34; gameModeString&#34;保持游戏模式的字符串。

此外,在随机数上使用模数运算符也可以限制它的范围。模数运算符在将左操作数除以右后返回余数。 I.E. 200%30将返回20。

使用模数将始终遵循以下形式:

A%B介于0和B-1之间 因此,任何模数30都将返回0到29之间的数字。您可以对随机数进行修改,例如:

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

关于它。我建议多读一些随机数生成器(或者在Java中仔细阅读Random API)和模数运算。