Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.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新手。 我似乎不明白为什么这两个代码产生不同的输出 for (int x = 0; x < 5; x++) { for (int y = 1; y <=x ; y++) { System.out.print("x"); } for (int g = 4; g >= x; g--) { System.out.print("*"); } System.

我是Java新手。
我似乎不明白为什么这两个代码产生不同的输出

for (int x = 0; x < 5; x++) {
    for (int y = 1; y <=x ; y++) {
        System.out.print("x");
    }

    for (int g = 4; g >= x; g--) {
        System.out.print("*");
    }                       
    System.out.println();
}
请给我解释一下。

y
x
的区别是什么?从0开始,因此第一次迭代在您打印的第一个代码中具有条件
y
x
乘以每行中的“x”字符串

for(int y=1; y<=x; y++) {
    System.out.print("x");
}
for(int y=1; y<=5; y++) {
    System.out.print("x");
}
在第二个代码中,每行打印5次“x”字符串

for(int y=1; y<=x; y++) {
    System.out.print("x");
}
for(int y=1; y<=5; y++) {
    System.out.print("x");
}

for(int y=1;y在第一个示例中,y首先小于x=1,在下一次迭代中,它将小于x=2…因为x值随第一个for循环而变化


但是,对于第二个示例,您声明y必须小于5,这一点没有任何变化。

基本上,主要区别在于这一行:

for(int y=1; y<=x; y++)

for(int y=1;y它们是不同的,因为在第一种情况下,x根据以下因素从0到4变化:

对于(int x=0;x非常简单,它将运行5次,每次迭代其值将增加1,即从0增加到4

所以在第一个循环中,内循环的条件如下:

for (int y = 1; y <= x; y++) {
      System.out.print("x");
}
但在外循环的最后一部分,
x
的值为4,因此这相当于:

for (int y = 1; y <= 4; y++) {
     System.out.print("x");
}

for(int y=1;y在您的第一个代码中,您的
for(int y=1;y我用一个新的
stringRepeat
函数替换了您的两个内部for循环。这样可能更容易理解

public class ForLoopStarsAndCrosses {

    public static void main(String[] args) {
        /// the total length ot the x's and stars
        final int length = 5;
        // start with 1 x until one less than the length (in this case 4)
        for(int x = 1; x < length; x++) {
            // the number of stars equals the total length minus the number of x's
            final int nbStars = length - x;
            // add the x's and the stars
            final String output = stringRepeat("x", x) + stringRepeat("*", nbStars);
            System.out.println(output);
        }
        System.out.println();
    }

    /** Repeat the string s n times */
    private static String stringRepeat(String s, int n) {
        String result = "";
        for (int i = 0; i < n; i += 1) {
            result += s;
        }
        return result;
    }
}
loopstars和cross的公共类{
公共静态void main(字符串[]args){
///x和星星的总长度
最终整数长度=5;
//从1 x开始,直到长度小于1 x(在本例中为4)
对于(int x=1;x
首先,您应该了解值和变量之间的区别。在您编写5时,值是一个常量,但变量可以更改。

对于您的问题,第一个代码和第一轮

x=1,y循环(可以是for,while,do-while)的目的是在特定条件下执行同一组语句的次数。“for”是一个确定的循环,其中有一个以整数开头的索引,保持递增,直到达到该条件。“while”是一个不确定的循环,其中没有索引,将一直执行,直到达到条件。“do while”是一个类似于“while”的循环,将至少执行一次,然后为下一次迭代验证条件

基于上述细节,

for(int x=0; x<5; x++){
        for(int y=1; y<=x; y++){

for(int x=0; x<5; x++){
        for(int y=1; y<=5; y++){

用于(int x=0;x
x
在第一种情况下是一个固定值,在第二种情况下是一个不断变化的值。
x
在第一种情况下每次运行
y
循环时都会改变值,这与第二种情况不同。谢谢各位!我现在明白了。嗯,你们知道一些关于编程的聊天组吗?我可以经常问问题吗输出的变化也要归咎于y上的at for循环。一方面,他将条件修正为y
for (int y = 1; y <= 0; y++) {
      System.out.print("x");
}         
for (int y = 1; y <= 4; y++) {
     System.out.print("x");
}
public class ForLoopStarsAndCrosses {

    public static void main(String[] args) {
        /// the total length ot the x's and stars
        final int length = 5;
        // start with 1 x until one less than the length (in this case 4)
        for(int x = 1; x < length; x++) {
            // the number of stars equals the total length minus the number of x's
            final int nbStars = length - x;
            // add the x's and the stars
            final String output = stringRepeat("x", x) + stringRepeat("*", nbStars);
            System.out.println(output);
        }
        System.out.println();
    }

    /** Repeat the string s n times */
    private static String stringRepeat(String s, int n) {
        String result = "";
        for (int i = 0; i < n; i += 1) {
            result += s;
        }
        return result;
    }
}
for(int y=1; y<=x; y++){
                System.out.print("x");
            } 
for(int y=1; y<=5; y++){
            System.out.print("x");
        }
for(int x=0; x<5; x++){
        for(int y=1; y<=x; y++){

for(int x=0; x<5; x++){
        for(int y=1; y<=5; y++){