英文:
how can I get the only entered values from an array?
问题
import java.util.Scanner;
class MultiplicationTable {
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
int arrSize = 32; // Maximum size for the array
int arr[] = new int[arrSize];
System.out.println("How many multiplication tables do you want to print?");
int n = obj.nextInt();
for (int i = 0; i < n; i++) {
System.out.println("Enter the number whose table you want:");
arr[i] = obj.nextInt();
}
System.out.println("Enter the range of the multiplier:");
int range = obj.nextInt();
for (int i = 0; i < n; i++) {
for (int j = 1; j <= range; j++) {
System.out.println(arr[i] + "x" + j + " = " + (arr[i] * j));
}
System.out.println(); // Empty line after each table
}
}
}
Output:
How many multiplication tables do you want to print?
3
Enter the number whose table you want:
2
Enter the number whose table you want:
5
Enter the number whose table you want:
6
Enter the range of the multiplier:
4
2x1 = 2
2x2 = 4
2x3 = 6
2x4 = 8
5x1 = 5
5x2 = 10
5x3 = 15
5x4 = 20
6x1 = 6
6x2 = 12
6x3 = 18
6x4 = 24
Please note that I've made modifications to your code to achieve the desired output. Let me know if you need any further assistance!
英文:
I am trying to make a program to print a multiplication table in which the user enters the number of table he wants (for example 3) , also enters the values of table he wants( 2,6,5 (random/unordered)), also enters the range of multiplier.
my code is printing all the elements present in the array.
please help me. ThankYou.
import java.util.Scanner;
class Addition
{
public static void main(String[] args) {
int j=0,k=0;
Scanner obj=new Scanner(System.in);
int arr[]=new int[32];
System.out.println("how many multiplication table do you want to print ? ");
int n=obj.nextInt();
for(int i=1;i<=n;i++)
{
System.out.println("Enter number whos table you want ");
for(j=1;j<=n;j++)
{
arr[j]=obj.nextInt(); break;
}
}
System.out.println("Enter range of multiplier");
int range=obj.nextInt();
for(int l=0;l<=arr[j];l++)
{
for(k=1;k<=range;k++)
{
System.out.println(" "+l+" * "+k+" = "+l*k);
}
}
}
}
>the output I want will look somewhat like this(
how many multiplication table do you want to print ? =3 and on entering numbers 2,5,6 and range=4)
2x1=2
2x2=4
2x3=6
2x4=8
5x1=5
5x2=10
5x3=15
5x4=20
6x1=6
6x2=12
6x3=18
6x4=24
</details>
# 答案1
**得分**: 0
我对您试图完成的内容进行了一些调整。您可以使用下面的代码片段:
```java
import java.util.ArrayList;
import java.util.Scanner;
class Addition {
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
ArrayList<Integer> typeMultiplicationTable = new ArrayList<Integer>();
System.out.println("要生成多少个乘法表?");
int n = obj.nextInt();
for (int i = 1; i <= n; i++) {
System.out.println("请输入您想要的乘法表的数字:");
typeMultiplicationTable.add(obj.nextInt());
}
System.out.println("请输入范围:");
int range = obj.nextInt();
for (int l = 0; l < typeMultiplicationTable.size(); l++) {
for (int k = 0; k <= range; k++) {
System.out.println(typeMultiplicationTable.get(l) + " * " + k + " = " + typeMultiplicationTable.get(l) * k);
}
}
}
}
如果您最终决定使用数组,您可以按照以下方式实现,我删除了一个for循环:
import java.util.Scanner;
class Addition {
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
System.out.println("要生成多少个乘法表?");
int n = obj.nextInt();
int arr[] = new int[n];
for (int j = 0; j < arr.length; j++) {
System.out.println("请输入您想要的乘法表的数字:");
arr[j] = obj.nextInt();
}
System.out.println("请输入范围:");
int range = obj.nextInt();
for (int l = 0; l < arr.length; l++) {
for (int k = 0; k <= range; k++) {
System.out.println(arr[l] + " * " + k + " = " + arr[l] * k);
}
}
}
}
英文:
I made a few adjustments to carry out what i think you trying to accomplish. you could use an arraylist seen below:
class Addition{
public static void main(String[] args) {
int j=0,k=0;
Scanner obj=new Scanner(System.in);
ArrayList<Integer> typeMultiplicationTable = new ArrayList<Integer>();
//used Arraylist instead of an array
//int arr[]=new int[32];
System.out.println("how many table? ");
int n=obj.nextInt();
for(int i=1;i<=n;i++)
{
System.out.println("Enter number whos table you want ");
for(j=0;j<=n;j++)
{
typeMultiplicationTable.add(obj.nextInt());
break;
//arr[j]=obj.nextInt(); break;
}
}
System.out.println("Enter range");
int range=obj.nextInt();
for(int l=0;l<=typeMultiplicationTable.size()-1;l++)
{
for(k=0;k<=range;k++)
{
System.out.println(" "+typeMultiplicationTable.get(l)+" * "+k+" = "+typeMultiplicationTable.get(l)*k);
}
}
}}
and if you do end up using an array, you could achieve it as follows - i ended up removing one of the for loops.
class Addition{
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
System.out.println("how many table? ");
int n=obj.nextInt();
int arr[]=new int[n];
for(int j=0;j<=arr.length-1;j++)
{
System.out.println("Enter number whos table you want ");
arr[j]=obj.nextInt();
}
System.out.println("Enter range");
int range=obj.nextInt();
for(int l=0;l<=arr.length-1;l++)
{
for(int k=0;k<=range;k++)
{
System.out.println(" "+arr[l]+" * "+k+" = "+arr[l]*k);
}
}
}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论