简体   繁体   English

For循环乘法表

[英]Multiplication Table For Loop

This is the code I wrote;这是我写的代码; it's going into an infinite loop and I don't know why..它进入无限循环,我不知道为什么..

import java.io.*;

public class Multi{
    public static void main(String args[])throws IOException{

    int num;
    BufferedReader inpt = new BufferedReader (new InputStreamReader (System.in));

    System.out.print("Enter a number: ");
    num=Integer.parseInt(inpt.readLine());

    int z,x,y;

    while (num>=1 || num<=11){
        for(z=1; z<=num; z++){
            for(x=1; x<=z; x++){

                y=z*x;

                System.out.print(y+" ");

            }
            System.out.println();
        }

    }

  }
}

The output I want to show in this one is that when a person inputs a number it will display a multiplication table.我想在这个输出中显示的是,当一个人输入一个数字时,它会显示一个乘法表。

eg例如

Enter a number: 5

Result:

 - 1 2 3 4 5
 - 2 4 6 8 10
 - 3 6 9 12 15
 - 4 8 12 16 20
 - 5 10 15 20 25

Enter a number: 3

 - 1 2 3
 - 2 4 6
 - 3 6 9

Your while condition will never be false: 您的while条件永远不会为假:

while (num>=1 || num<=11)

Every possible number is >= 1 or <= 11. I guess you meant "and" instead of "or". 每个可能的数字都是> = 1 <=11。我猜您是说“和”而不是“或”。

Also, you need to put the code that sets num inside the while-loop. 另外,您需要将设置num的代码放入while循环中。

//To get a multiplication table for the number get from user 


#include<stdio.h>
int main() {
        
    int i;
    int num;
    scanf("%d",&num);

    for(i=1; i<=10; i++){
        printf("%d\n",i*num);
    }
    
    
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM