如何从数组中获取仅输入的值?

huangapple go评论74阅读模式
英文:

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(&quot;how many multiplication table do you want to print ? &quot;);
	int n=obj.nextInt();
	for(int i=1;i&lt;=n;i++)
	{
		System.out.println(&quot;Enter number whos table you want &quot;);
		for(j=1;j&lt;=n;j++)
		{
			arr[j]=obj.nextInt(); break;
		}
	}
	System.out.println(&quot;Enter range of multiplier&quot;);
	int range=obj.nextInt();
	
	for(int l=0;l&lt;=arr[j];l++)
	{
		for(k=1;k&lt;=range;k++)
		{
			System.out.println(&quot; &quot;+l+&quot; * &quot;+k+&quot; = &quot;+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&lt;Integer&gt; typeMultiplicationTable = new ArrayList&lt;Integer&gt;();
    
    //used Arraylist instead of an array
    //int arr[]=new int[32]; 
    System.out.println(&quot;how many table? &quot;);
    int n=obj.nextInt();
    
    for(int i=1;i&lt;=n;i++)
    {
        System.out.println(&quot;Enter number whos table you want &quot;);
        for(j=0;j&lt;=n;j++)
        {
            typeMultiplicationTable.add(obj.nextInt());
            break;
            //arr[j]=obj.nextInt(); break;
        }
    }
    System.out.println(&quot;Enter range&quot;);
    int range=obj.nextInt();
    
    for(int l=0;l&lt;=typeMultiplicationTable.size()-1;l++)
    {
        for(k=0;k&lt;=range;k++)
        {
            System.out.println(&quot; &quot;+typeMultiplicationTable.get(l)+&quot; * &quot;+k+&quot; = &quot;+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(&quot;how many table? &quot;);
    int n=obj.nextInt();
    
    int arr[]=new int[n]; 

        for(int j=0;j&lt;=arr.length-1;j++)
        {
            System.out.println(&quot;Enter number whos table you want &quot;);
            arr[j]=obj.nextInt();
        }
    
    System.out.println(&quot;Enter range&quot;);
    int range=obj.nextInt();
    
    for(int l=0;l&lt;=arr.length-1;l++)
    {
        for(int k=0;k&lt;=range;k++)
        {
            System.out.println(&quot; &quot;+arr[l]+&quot; * &quot;+k+&quot; = &quot;+arr[l]*k);
        }
    }
}}

huangapple
  • 本文由 发表于 2020年9月6日 23:58:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63766118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定