【问题标题】:Strange behavior of cycle for(64-bit Windows 7/java version 1.8.0 25)循环的奇怪行为(64 位 Windows 7/java 版本 1.8.0 25)
【发布时间】:2015-10-28 10:43:24
【问题描述】:

我有任务:制作一个 java 程序,它将打印从 000001 到 999999 的数字(这是电车的票号)并计算所有左右两侧相等的数字(例如:024033、125008、531900 等。 )。我写了代码:

 public class Cycle13 {

    public static void main(String[] args) {
        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;
        int e = 0;
        int f = 1;
        int current_b = 0;
        int current_c = 0;
        int current_d = 0;
        int current_e = 0;
        int current_f = 0;
        int counter = 0;

        for (int i = 1; i <= 1000000; ++i) {

            if (f >= 10) {
                current_f = 10;
                f = 0;
            }
            if (current_f >= 10) {
                ++e;
                current_f = 0;
            }

            if (e >= 10) {
                current_e = 10;
                e = 0;
            }
            if (current_e >= 10) {
                ++d;
                current_e = 0;
            }
            if (d >= 10) {
                current_d = 10;
                d = 0;
            }
            if (current_d >= 10) {
                ++c;
                current_d = 0;
            }
            if (c >= 10) {
                current_c = 10;
                c = 0;
            }
            if (current_c >= 10) {
                ++b;
                current_c = 0;
            }
            if (b >= 10) {
                current_b = 10;
                b = 0;
            }
            if (current_b >= 10) {
                ++a;
                current_b = 0;
            }
            if (a >= 10) {
                break;
            }
            System.out.print(a);
            System.out.print(b);
            System.out.print(c);
            System.out.print(d);
            System.out.print(e);
            System.out.print(f);

            ++f;
            if (a + b + c == d + e + f) {
                counter++;
            }
            System.out.println();
        }

        System.out.print(counter);
    }
 }

尽管它是正确的,但这段代码有一个奇怪的行为。我确信这一点,因为如果在循环中设置条件 i

【问题讨论】:

  • 我不能说别人,但你的问题对我来说不清楚
  • “奇怪的行为” - 什么奇怪的行为?它做了什么你没想到的?顺便说一句,这个词是“循环”,而不是“循环”。你能把你的问题说得更清楚吗?
  • 感谢您的建设性批评。我很困惑,因为当我的程序运行时,它会在控制台中打印来自 000001 的数字(即它必须是:000001、000002、000003 等)。但是,当程序终止时,控制台会显示来自 990001 的数字(即 990001、990002、990003 等)。

标签: java for-loop printing count cycle


【解决方案1】:

除了你的语法和几乎不理解你的问题,运行这段代码将得到你预期的结果,没有任何“奇怪”的行为;-)

public class Cycle13 {
    public static void main(String[] args) {
        int counter = 0;
        for(int a = 0; a < 10; a++){
            for(int b = 0; b < 10; b++){
                for(int c = 0; c < 10; c++){
                    for(int d = 0; d < 10; d++){
                        for(int e = 0; e < 10; e++){
                            for(int f = 0; f < 10; f++){
                                if(a+b+c == d+e+f){
                                   counter++;
                                }
                                System.out.print(a);
                                System.out.print(b);
                                System.out.print(c);
                                System.out.print(d);
                                System.out.print(e);
                                System.out.print(f);
                                System.out.println();
                            }
                        }
                    }
                }
            }
         }
      System.out.print(counter);
   }
}

【讨论】:

    猜你喜欢
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 2013-04-04
    • 2019-06-16
    • 2012-08-28
    相关资源
    最近更新 更多