Java匹配不同的数组
因此,我尝试在两个不同的用户输入之间获取基本输入值,如Java匹配不同的数组,java,Java,因此,我尝试在两个不同的用户输入之间获取基本输入值,如a或b,将它们匹配,并打印该循环的分数,然后还打印累计分数。我觉得我做得不错,但每次我的分数都会打印出0的值。因此,让我们假设键值是aaa,and值是aba。这将打印出值2,并继续运行,同时将分数重置回0 InputStreamReader reader = new InputStreamReader(System.in); BufferedReader input1 = new BufferedReader(reader);
a
或b
,将它们匹配,并打印该循环的分数,然后还打印累计分数。我觉得我做得不错,但每次我的分数都会打印出0的值。因此,让我们假设键值是aaa
,and值是aba
。这将打印出值2,并继续运行,同时将分数重置回0
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input1 = new BufferedReader(reader);
BufferedReader input2 = new BufferedReader(reader);
String inputValue;
String[] key;
String[] ans;
int totalScore = 0;
inputValue = input1.readLine();
key = inputValue.split("\\s+");
while(true){
int score = 0;
inputValue = input2.readLine();
ans = inputValue.split("\\s+");
if(key[0] == ans[0])
score += 1;
if(key[1] == ans[1])
score += 1;
if(key[2] == ans[2])
score += 1;
totalScore = score;
System.out.print(score + "\n");
System.out.print(totalScore + "\n");
}
你在比较字符串;使用
.equals()
而不是=
例如:
if (key[0].equals(ans[0])) {}
请记住:(几乎)始终使用
.equals()
进行字符串比较。谢谢,我认为它应该是==我想检查并查看的内容如果(键[0]。equals(“a”)|键[0]。equals(“a”)
捕获大写或小写?。equalsIgnoreCase(“a”)将捕获a和a