如何在条件和循环时修复此问题 [英] How do I fix this while condition and for loop

查看:58
本文介绍了如何在条件和循环时修复此问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚编程while循环的正确方法,一旦所有黑色大理石的条件被排序到数组的开头变为真,它应该退出但是只能通过使用if来休息语句。



第二个问题是方法main中的最后一个for循环,它不应该是必需的并且没有意义但是让分类器迭代额外的时间并修复了一个案例,其中跳过了一个白色大理石,但仍然以某种方式遇到了所有黑色大理石的状况。



任何有关改进的帮助都将受到赞赏。

I am trying to figure out the correct way to program the while loop which should exit once the condition of all the black marbles being sorted to the beginning of the array becomes true but can only do it by using an if with a break statement.

The second issue is the last for loop in method main which shouldn't be needed and doesn't really make sense but gets the sorter to iterate extra times and fixes one case where a white marble is skipped but the condition of all the black marbles being in the beginning is still met somehow.

Any help with improvements would be appreciated.

import java.util.Arrays;

public class lostMyMarbles{
    public static void main(String[] args){
        String[] marbles = {"wMarble", "bMarble", "wMarble", "wMarble", "bMarble", "bMarble"};
        //String[] marbles = {"bMarble", "bMarble", "bMarble", "bMarble", "bMarble", "wMarble"};
        //String[] marbles = {"wMarble", "wMarble", "wMarble", "wMarble", "wMarble", "bMarble"};
        //String[] marbles = {"wMarble", "wMarble", "wMarble", "wMarble", "wMarble", "wMarble"};
        //String[] marbles = {"wMarble", "wMarble", "bMarble", "wMarble", "wMarble", "bMarble"};
        
        int counter = 0;
        int newCount = 0;
        for(String element: marbles){
            if(element.equals("bMarble"))
                counter++;
        }
        while(newCount != counter){
            for(int i = 0; i < counter; i++){
                if(marbles[i].equals("bMarble")){
                    newCount++;
                    if(newCount == counter)
                        break;
                }
            }
            if(newCount != counter){
                for(int i = 0; i < counter; i++)
                    sorter(marbles);
            }
        }
        System.out.println(Arrays.toString(marbles));
    }
    public static void sorter(String[] marbles){
        for(int i = 1; i < marbles.length; i++){
                if(marbles[i].equals("bMarble") && (marbles[i-1]).equals("wMarble")){
                    String temp = marbles[i];
                    marbles[i] = marbles[i-1];
                    marbles[i-1] = temp;
                }
        }
    }
}





我尝试了什么:



我尝试使用布尔条件并使用在循环内递增的计数器退出但似乎必须满足条件某种方式(范围问题?)。

最后一个for循环阻止我的一个测试用例提供不正确的输出,即使所有黑色弹珠的状态都在开始时......但是我不记得我的推理理由从那里开始>。>



What I have tried:

I have tried using a boolean condition and using the counter that is incremented inside the loop to exit but seems like the condition has to be met on the outside somehow (scope issue?).
The last for loop prevents one of my test cases from providing the incorrect output even though the condition of all black marbles being in the beginning being met... but I can't remember my reasoning for putting it there to start with >.>

推荐答案

newCount 永远不能等于计数器,因为循环的第二个仅比较第一个计数器字符串,所以它将只看到一个值为bMarble的人。您必须每次迭代完整列表。
newCount can never equal counter, because the second for loop only compares the first counter strings, so it will only see one with the value "bMarble". You must iterate the complete list each time.


这篇关于如何在条件和循环时修复此问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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