所以,对于类,我必须编写一个程序来确定有多少正整数
1)100万以下
2)数字中至少有一个7和一个9
3)必须使用蛮力法。
虽然答案应该是199262,但由于重复,我一直得到228530,有人能看看我哪里出错了吗谢谢!
相似但不相同的问题:Java - numbers with at least one 7 and one 9 in its digit

boolean sevNine = false; // a combination of seven and nine in a number
boolean oneNine;
boolean oneSeven;
int counter = 0;
for (int i = 0; i<1000000; i++) //Runs numbers 1-1000000
{
    oneSeven = false;
    oneNine = false;
    String number2 = " " + (i); //sets a nmber to a string
    int length = number2.length()  -1; //length goes up to the last character 0-j
    for (int j= 0; j <= length; j++) //looking for the first 7 or 9 in string
    {
        char a = number2.charAt(j); //sets char to the next "letter"
        if (a == '7' && oneSeven != true) //if the number is a 7 and there isnt already a seven
        {
            oneSeven = true; //now there is a seven,
            for (int k = j+1; k <= length; k++) //checks from the next char up to the length for a 9
            {
                char b = number2.charAt(k);
                if (b == '9')
                {
                     sevNine = true;
                }
            }
        }
        else if (a == '9' && oneNine != true)
        {
            oneNine = true;
            for (int l = j+1; l <= length; l++)
            {
                char b = number2.charAt(l);
                if (b == '7')
                {
                    sevNine = true;
                }
            }
        }
        if (sevNine == true)
        {
            counter++;
            sevNine = false;
            System.out.println(number2);

        }

    }
}
System.out.println(counter);

最佳答案

一旦sevNine设置为true并递增计数器,则不会中断循环,因此它会在同一个数字上的每个数字上迭代,即使它已经包含该数字…只需添加一个中断语句即可在每次递增计数器后退出每个循环上的for循环迭代。
这是密码。

public static void main(String[] args) {
    boolean sevNine = false; // a combination of seven and nine in a number
    boolean oneNine;
    boolean oneSeven;
    int counter = 0;
    for (int i = 0; i < 1000000; i++) // Runs numbers 1-1000000
    {
        oneSeven = false;
        oneNine = false;
        String number2 = " " + (i); // sets a nmber to a string
        int length = number2.length() - 1; // length goes up to the last character 0-j
        for (int j = 0; j <= length; j++) // looking for the first 7 or 9 in string
        {
            char a = number2.charAt(j); // sets char to the next "letter"
            if (a == '7' && oneSeven != true) // if the number is a 7 and there isnt already a seven
            {
                oneSeven = true; // now there is a seven,
                for (int k = j + 1; k <= length; k++) // checks from the next char up to the length for a 9
                {
                    char b = number2.charAt(k);
                    if (b == '9') {
                        sevNine = true;
                    }
                }
            } else if (a == '9' && oneNine != true) {
                oneNine = true;
                for (int l = j + 1; l <= length; l++) {
                    char b = number2.charAt(l);
                    if (b == '7') {
                        sevNine = true;
                    }
                }
            }
            if (sevNine == true) {
                counter++;
                sevNine = false;
                System.out.println(number2);
                break;
            }
        }
    }
    System.out.println(counter);
}

如果使用break语句运行,则应得到199262作为结果数。

08-17 19:07