[英]how can I get the only entered values from an array?
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.我正在尝试制作一个程序来打印一个乘法表,其中用户输入他想要的表数(例如 3),还输入他想要的表的值(2,6,5(随机/无序)),也进入乘数范围。 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) { 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)
我想要的输出看起来有点像这样(你想打印多少个乘法表?=3 并输入数字 2,5,6 和 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
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.如果你最终使用了一个数组,你可以按如下方式实现它 - 我最终删除了一个 for 循环。
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);
}
}
}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.