Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:for循环工作不正常_Java_For Loop - Fatal编程技术网

Java:for循环工作不正常

Java:for循环工作不正常,java,for-loop,Java,For Loop,大家好,我是Java初学者,我的英语不好,希望你们能理解我的问题: public static void main(String[] args) { int i, a, b, c, d, yil = 1999, rt = 0; do{ //loop searching for 1976 for( i = 1900; i < 2000; i++){ //separate "i" to the dig

大家好,我是Java初学者,我的英语不好,希望你们能理解我的问题:

public static void main(String[] args) {

    int i, a, b, c, d, yil = 1999, rt = 0;


    do{
            //loop searching for 1976
            for( i = 1900; i < 2000; i++){
            //separate "i" to the digits
            a = i / 1000;
            b = i % 1000 / 100;
            c = i % 1000 % 100 / 10;
            d = i % 1000 % 100 % 10;
            rt = a + b + c + d;
            }}
            //while rt=23 and i=1976 equation will be correct then exit the loop and print the 1976.
            while( rt == yil - i );
        System.out.println("Yıl = " + i );

}
publicstaticvoidmain(字符串[]args){
int i,a,b,c,d,yil=1999,rt=0;
做{
//1976年的循环搜索
对于(i=1900;i<2000;i++){
//将“i”分隔为数字
a=i/1000;
b=i%1000/100;
c=i%1000%100/10;
d=i%1000%100%10;
rt=a+b+c+d;
}}
//当rt=23和i=1976时,方程将正确,然后退出循环并打印1976。
而(rt==yil-i);
System.out.println(“Yıl=“+i”);
}

但当我运行程序时,它总是显示2000而不是1976。

你的混乱增量可能会隐藏它,但你无法摆脱
for
循环。它总是走到最后,即
i=2000

for( i = 1900; i < 2000; i++){
   ... // no break in there
}
...
System.out.println("Yıl = " + i );

你有没有尝试过单步执行程序,添加一些跟踪/打印语句,等等?我想我在这里有点迷路了,你在搜索1976,但是循环结构很奇怪,问题的一般版本是什么?根据这里的逻辑,这是当rt=23 rt:23 yil-I:50(1950)rt==yil-1时?谢谢你的回答。我明白了,那我怎么才能找到这一年呢?有什么建议吗?
        int i, a, b, c, d, yil = 1999, rt = 0;
        //loop searching for 1976
        for( i = 1900; i < 2000; i++){
            //separate "i" to the digits
            a = i / 1000;
            b = i % 1000 / 100;
            c = i % 1000 % 100 / 10;
            d = i % 1000 % 100 % 10;
            rt = a + b + c + d;
            if (rt==yil-i) break;
        }
        System.out.println("Yıl = " + i );  }
Yıl = 1976