我想在 java 中显示乘法 table 但只显示 1 到 20
I want to display multiplication table in java but only from 1 to 20
我当前的 java 程序显示从零到无穷大的乘法 table。
- 我只想限制从 1 到 20...我只想乘法 table 输出从 0 到 20。
- 即我不希望零或任何大于 20 的数字的乘法 table 作为输出。
- 是否可以仅使用循环来完成我想做的事情
- 如果不循环那么如何
- 我从 hackerrank 复制了这个问题和解决方案...
这是代码...
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(bufferedReader.readLine().trim());
for (int i = 0; i < 10; i) {
System.out.println(N " x "(i 1)
" = "(N * (i 1)));
}
bufferedReader.close();
}
}```
for (int i=1; i <= 10; i++){
int result = N*i;
if (result >= 1 && result <= 20) {
System.out.println(N + " x " + i + " = " + result);
}
}
似乎是要走的路...我是不是在某种程度上误解了这个问题?这看起来相当简单...
你的大部分代码都很好,你只需要添加一个条件来验证 1 到 20 的数字,你还需要从 1 而不是 0 开始循环,看看下面的代码,这将解决你的问题
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(bufferedReader.readLine().trim());
if (N >= 1 && N <= 20) {
for (int i = 1; i < 10; i++) {
System.out.println(N + " x " + (i + 1) + " = " + (N * (i + 1)));
}
} else {
System.out.println("Enter Numbers in between 1 to 20");
}
bufferedReader.close();
}
在for循环之前使用if语句
if(N >0 && N<=20){
for (int i=1; i<= 10; i++){
System.out.println(N + " x " + (i) + " = " + (N *i));
}
} else{
// Number should be greater than 0 and less than 21
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution {
public static void main(String[] args) throws IOException {
try(BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in))){
//Let say we are getting the limit of the table number from console
int endTableNumber = Integer.parseInt(bufferedReader.readLine().trim());
//As per condition table number neither above 20 nor zero
if(endTableNumber <=0 || endTableNumber > 20) {
System.err.println("Allowed Table printing limit is 1 to 20");
}
//setting offset table number as 1 as per condition
int startTableNumber = 1;
//outer for loop: table
for (int i=startTableNumber; i<= endTableNumber; i++){
System.out.println("Table : "+ i);
//outer for loop: table's entry
for(int j=1; j <= 20; j++ ) {
System.out.println(j + " x " + (i) + " = " + (j * (i)));
}
System.out.println("");
}
}
}
}
我当前的 java 程序显示从零到无穷大的乘法 table。
- 我只想限制从 1 到 20...我只想乘法 table 输出从 0 到 20。
- 即我不希望零或任何大于 20 的数字的乘法 table 作为输出。
- 是否可以仅使用循环来完成我想做的事情
- 如果不循环那么如何
- 我从 hackerrank 复制了这个问题和解决方案... 这是代码...
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(bufferedReader.readLine().trim());
for (int i = 0; i < 10; i) {
System.out.println(N " x "(i 1)
" = "(N * (i 1)));
}
bufferedReader.close();
}
}```
for (int i=1; i <= 10; i++){
int result = N*i;
if (result >= 1 && result <= 20) {
System.out.println(N + " x " + i + " = " + result);
}
}
似乎是要走的路...我是不是在某种程度上误解了这个问题?这看起来相当简单...
你的大部分代码都很好,你只需要添加一个条件来验证 1 到 20 的数字,你还需要从 1 而不是 0 开始循环,看看下面的代码,这将解决你的问题
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(bufferedReader.readLine().trim());
if (N >= 1 && N <= 20) {
for (int i = 1; i < 10; i++) {
System.out.println(N + " x " + (i + 1) + " = " + (N * (i + 1)));
}
} else {
System.out.println("Enter Numbers in between 1 to 20");
}
bufferedReader.close();
}
在for循环之前使用if语句
if(N >0 && N<=20){
for (int i=1; i<= 10; i++){
System.out.println(N + " x " + (i) + " = " + (N *i));
}
} else{
// Number should be greater than 0 and less than 21
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution {
public static void main(String[] args) throws IOException {
try(BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in))){
//Let say we are getting the limit of the table number from console
int endTableNumber = Integer.parseInt(bufferedReader.readLine().trim());
//As per condition table number neither above 20 nor zero
if(endTableNumber <=0 || endTableNumber > 20) {
System.err.println("Allowed Table printing limit is 1 to 20");
}
//setting offset table number as 1 as per condition
int startTableNumber = 1;
//outer for loop: table
for (int i=startTableNumber; i<= endTableNumber; i++){
System.out.println("Table : "+ i);
//outer for loop: table's entry
for(int j=1; j <= 20; j++ ) {
System.out.println(j + " x " + (i) + " = " + (j * (i)));
}
System.out.println("");
}
}
}
}