Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.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中的嵌套while循环不工作_Java - Fatal编程技术网

java中的嵌套while循环不工作

java中的嵌套while循环不工作,java,Java,用户输入 5 所需输出(使用while循环) 代码: while(temp1内部while循环后temp2的值将是a+1。由于您以后没有将其重置为1,您将不会再次进入此内部循环,因为下面的条件while(temp2代码注释解释了代码的错误 //assume temp1 equals 1 while(temp1 <= a){ temp2 = 1;//you're primarily forgetting to reset the temp2 count while(temp

用户输入

5
所需输出(使用while循环)

代码:


while(temp1内部
while
循环后
temp2
的值将是
a+1
。由于您以后没有将其重置为
1
,您将不会再次进入此内部循环,因为下面的条件
while(temp2代码注释解释了代码的错误

//assume temp1 equals 1
while(temp1 <= a){
    temp2 = 1;//you're primarily forgetting to reset the temp2 count
    while(temp2 <= a){
        temp = temp1*temp2;
        System.out.print(temp + " ");
        temp2++;
    }
    temp1++;
    System.out.println();
}
//假设temp1等于1
while(temp1)

有了这个答案,您不需要重置temp变量,如果我必须将问题分解为简单的单词,您将产生所需的结果

  • 用户将输入一个数字:“x”
  • 创建一个大小为arr[x][x]的二维矩阵
  • 每个元素的值都将是a[i][j]=i*j;//考虑(i=1;i
    inta=5;//或者以其他方式获得此值。
    //初始化你的值
    int temp1=1;
    int temp2=1;
    int temp;//这里只需要一个声明。
    
    而(temp1
    用于(int i=1;i它是如何工作的?输出是否错误?请发布整个函数,这只是其中的一部分。例如,您正在将内容分配给temp,但temp没有声明为什么您不使用
    for
    循环?或者您的调试器来调试您的代码?我怀疑
    temp2
    应该在某个地方重置为1。我不知道是谁对此投了反对票,但他给出了输入、预期输出和所需代码。他唯一缺少的是实际输出。@ArkaBhattacharjee没问题:)很高兴我能帮上忙。
    while (temp1 <= a) {
        while (temp2 <= a) {
            temp = temp2 * temp1;
            System.out.print(temp + " ");
            temp2++;
        }
        temp1++;
        System.out.println();
    }
    
    //assume temp1 equals 1
    while(temp1 <= a){
        temp2 = 1;//you're primarily forgetting to reset the temp2 count
        while(temp2 <= a){
            temp = temp1*temp2;
            System.out.print(temp + " ");
            temp2++;
        }
        temp1++;
        System.out.println();
    }
    
        int a = 5;
        int temp1 = 1;
        int temp2= 1;
        int temp = 1;
    
        while(temp1 <= a){
            while(temp2 <= a){
                temp = temp2*temp1;
                System.out.print(temp + " ");
                temp2++;
            }
            System.out.println();
            temp1++;
            temp2=1;
        }
    
        int userInput = 5;
        int answer = 0;
        for(int y = 0; y < userInput; y++){
    
            for(int x = 0; x < userInput; x++ ){
    
                answer = x * y;
                System.out.print(answer + " ");
            }
            System.out.println();
        }
    
        int a = 5; //Or however else you get this value.
    
        //Initialize your values
        int temp1 = 1;
        int temp2 = 1;
        int temp; //Only need a declaration here.
    
        while (temp1 <= a) {            
            while(temp2 <= a) {
                temp = temp1*temp2;
                System.out.print(temp + " ");
                temp1++;
                temp2++;
            }
            //This executes between inner loops
            temp2 = 1; //It's important to reset 
            System.out.println();
        }
    
        int a = 5;
    
        int row = 0;
        int col = 0;
    
        while (++row <= a) {            
            while(++col <= a) {
                System.out.print(row*col + " ");
            }
            col = 0;
            System.out.println();
        }
    
        for(int i=1; i<=a; i++){
            System.out.print(i);
            for(int j=2; j<=a; j++){
                int val = i*j;
                System.out.print(" " + val);
            }
            System.out.println();
        }