因为循环没有做我想要的 [英] For loop not doing what I want

查看:76
本文介绍了因为循环没有做我想要的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将两个列表相互比较。循环遍历两个列表中的每个索引并将值相互比较。

1.一个位置不匹配:如果loop1 [i]!= loop2 [j]和i == j,则值为我在loop1中与loop2中的值不匹配。

2.匹配但不在位置:loop1 [i] == loop2 [j]和i!= j,值在两个循环中都很常见但是在错误的位置。

3.如果第一次出现不止一次,多个值不匹配。



问题是当我测试时,我得到错误的输出,例如循环1 = 1,2,3,4,5,6,7和loop2 = 1,2,3,5,5,7。它打印出第一个结果。正确

问题在这里:

loop1 = 1,2,3,4,5,6,7

loop2 = 1,2, 4,3,5,6,7,应该打印第二个结果,但打印第一个结果。

例如

I am trying to compare two lists to one another. Loop through each index in both lists and compare the values to each other.
1. One position does not match: if loop1[i] != loop2[j] and i ==j, value at i in loop1 does not match value in loop2.
2. match but not at position: loop1[i]== loop2[j] and i!=j, value is common in both loops but is at wrong position.
3. if the first occurs more than once, multiple values dont match.

Problem is when i test it, i get the wrong output e.g loop 1 = 1,2,3,4,5,6,7 and loop2 = 1,2,3,5,5,7. It prints the first outcome. Correct
problem is here:
loop1 = 1, 2, 3, 4, 5, 6, 7
loop2 = 1, 2, 4, 3, 5, 6, 7, supposed to print second outcome but prints first outcome instead.
Eg

selectedindices = List.of(1, 2, 3, 4, 5, 6)


indicesofpositionguessed = List.of(1, 2, 3, 5, 5, 6


when you compare the two, its supposed to be: "User guess at position 3 is correct but at wrong position" but instead is "User guess at position 3 did not match selected"





由于某种原因,该方法返回一个字符串,但不能识别for循环中返回的字符串,但是让我在for循环之外放置一个return语句。为什么?有没有办法解决它?



我尝试了什么:





Also for some reason the method returns a string but does not recognize the returned strings in the for loop but makes me put a return statement outside of the for loop. Why? Is there any way to get around it?

What I have tried:

public String guessPosition(List<Integer> indicesOfPositionGuessed) {
       int count = 0;
       for (int i = 0; i < indicesOfPositionGuessed.size(); i++) {
           Integer item1 = indicesOfPositionGuessed.get(i);
           for (int j = 0; j < selectedIndicies.size(); j++) {
               Integer item2 = selectedIndicies.get(j);
               if (i != j) {
                   if (item1.equals(item2) == true) {
                       return "User guess at position " + i + " is correct but at wrong position";
                   }
               }
               if (i == j) {
                   if (item1.equals(item2) == false) {
                       count++;
                       return "User guess at position " + i + " did not match selected";
                   }
               }

           }
       }
       if(count > 0)
           return "User guess did not match in two positions";
       return null;
   }

推荐答案

引用:

当你比较两者时,它应该是:用户猜测位置3是正确但位置错误但是用户猜测位置3不匹配选择

when you compare the two, its supposed to be: "User guess at position 3 is correct but at wrong position" but instead is "User guess at position 3 did not match selected"



我没有在代码中看到任何明显的解释答案,知道发生了什么的方法是使用调试器并且2列表是你所期望的,然后设置一个断点并观察代码执行。

-----

你的代码没有你想象的那样,或者你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码在做什么,你的任务是与它应该做的比较。

调试器中没有魔法,它不知道你的cpde应该做什么,它没有找到bug,它只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



http://docs.oracle .com / javase / 7 / docs / technotes / tools / windows / jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

调试器只是向你展示你的代码正在做什么,你的任务是与它应该做什么进行比较。


I don't see anything obvious in code to explain that answer, the way to know what is going on is to use the debugger and that the 2 lists are what you expect, then set a breakpoint and watch the code perform.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


嗯,这很简单,当 i = 3 j = 3 条件(1)满足且函数返回。

条件(2)将是 i = 3 j = 4 时满意,但函数不会达到这样的点。



也就是说,对于每个猜测的索引,你必须在选定的数组中搜索匹配,以便检查条件(2)。在不成功的搜索中,自动满足条件(1)。
Well, it is simple, when i=3 and j=3 condition (1) is satisfied and the function returns.
Condition (2) would be satisfied when i=3 and j=4 but the function won't reach such a point.

That is, for each of the guessed indices you have to search in the selected ones array for a match, in order to check condition (2). On unsucessful search, condition (1) is automatically satisfied.


这篇关于因为循环没有做我想要的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆