Java I';I’我想看看一个较小的字符串是否包含在另一个较大的字符串中,我想拆分该字符串
我正在研究一种算法,它可以帮助预测电脑在石头布剪刀鞋游戏中会做什么。基本上我所做的就是把它玩了100次,记下了它所有的动作。我把它变成了一根绳子 我所做的是寻找计算机行为的模式,就像我对人一样。在前四轮中,我的算法只是猜测。然而,在前四轮之后,我用计算机的动作将它们与训练数据进行比较。如果我能找到模式,我期待下一步。例如,假设部分数据如下所示: R p p S S p R 我发现电脑上写着RPS,我的算法会建议我使用剪刀,因为上次它就是这么做的 我所做的是将训练数据保存为字符串,获取输入数据并将其保存为字符串,然后尝试拆分训练数据字符串,但它似乎不起作用 如何使用较大字符串中包含的较小字符串拆分字符串,以便保存并打印较大字符串中包含的较小字符串后的下一个字符Java I';I’我想看看一个较小的字符串是否包含在另一个较大的字符串中,我想拆分该字符串,java,string,Java,String,我正在研究一种算法,它可以帮助预测电脑在石头布剪刀鞋游戏中会做什么。基本上我所做的就是把它玩了100次,记下了它所有的动作。我把它变成了一根绳子 我所做的是寻找计算机行为的模式,就像我对人一样。在前四轮中,我的算法只是猜测。然而,在前四轮之后,我用计算机的动作将它们与训练数据进行比较。如果我能找到模式,我期待下一步。例如,假设部分数据如下所示: R p p S S p R 我发现电脑上写着RPS,我的算法会建议我使用剪刀,因为上次它就是这么做的 我所做的是将训练数据保存为字符串,获取输入数据并将
Scanner scan = new Scanner(System.in);
Random rand = new Random();
int rock = 50;
int paper = 32;
int sicsors = 18;
//it should pick rock 1/2 the time, paper 1/3 the time, and scissors 1/5 the time
//Look at history and look at past four.
//If you can find a pattern then whatever comes next use
//char[] trainingData = "beep".toCharArray();
//char[] gameInput = "beep".toCharArray();
String userInput = "";
String trainingDataString = "RRRRSPPPSRRRP";
ArrayList<String> trainingData = new ArrayList<String>();
ArrayList<String> gameInput = new ArrayList<String>();
ArrayList<String> testingData = new ArrayList<String>();
System.out.println("Lets start!");
System.out.println("Play 100 roudds of RPC!");
System.out.println("Press 1 to continue");
int x = scan.nextInt();
scan.nextLine();
int rounds = 0;
boolean patternedContained = true;
while (rounds < 100) {
rounds++;
//choose based on overall perctanges
if (rounds <= 4 || patternedContained == false) {
int compChoice = rand.nextInt(100);
//System.out.println(compChoice);
if(compChoice <= 20) {
System.out.println("Use Rock. Enter the robots choice (R P S). ");
userInput = scan.nextLine();
gameInput.add(userInput);
}
else if (compChoice > 20 && compChoice <= 32) {
System.out.println("Use Paper. Enter the robots choice (R P S).");
userInput = userInput + scan.nextLine();
gameInput.add(userInput);
}
else {
System.out.println("Use Scissors. Enter the robots choice (R P S).");
userInput = userInput + scan.nextLine();
gameInput.add(userInput);
}
}
else if (rounds > 4) {
//check if pattern is contained
//if it is
if(trainingDataString.toLowerCase().contains(userInput.toLowerCase())) {
String[] startHere = trainingDataString.split(userInput);
String gotCha = startHere[0];
System.out.println(gotCha);
}
}
}
System.out.println(userInput);
}
}
Scanner scan=新的扫描仪(System.in);
Random rand=新的Random();
int rock=50;
整版纸=32;
int-sicsors=18;
//它应该选择石头1/2的时间,纸1/3的时间,剪刀1/5的时间
//看看历史,看看过去的四年。
//如果你能找到一个模式,那么无论下一步使用什么
//char[]trainingData=“beep”.toCharArray();
//char[]gameInput=“beep”.toCharArray();
字符串userInput=“”;
字符串trainingDataString=“RRRRSPPPSRRP”;
ArrayList trainingData=新建ArrayList();
ArrayList gameInput=新建ArrayList();
ArrayList testingData=新建ArrayList();
System.out.println(“让我们开始吧!”);
System.out.println(“播放100 roudds的RPC!”);
System.out.println(“按1继续”);
int x=scan.nextInt();
scan.nextLine();
整数=0;
布尔模式包含=真;
while(轮数<100){
回合++;
//根据总体百分比进行选择
如果(rounds使用indexOf
查找小字符串在大字符串中的起始位置:
int littleStringPos = bigString.indexOf(littleString);
然后,通过将小字符串的长度添加到其起始位置,可以找到紧跟在小字符串后面的字符的索引:
int charAfterIdx = littleStringIdx + littleString.length();
然后使用charAt
选择字符:
char charAfterLittleString = bigString.charAt(charAfterIdx);
您应该准备好大字符串不包含小字符串的可能性,这将由返回结果-1的indexOf
指示。因此,删除中间变量并包含安全检查的正确实现可能如下所示:
int littleStringPos = bigString.indexOf(littleString);
if (littleStringPos >= 0)
System.out.println("The character after thelittle string is: " +
bigString.charAt(littleStringIdx + littleString.length()));
else
System.out.println("The little string isn't there!");
去除标记的机器学习