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

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

how can I get the only entered values from an array?

问题

  1. import java.util.Scanner;
  2. class MultiplicationTable {
  3. public static void main(String[] args) {
  4. Scanner obj = new Scanner(System.in);
  5. int arrSize = 32; // Maximum size for the array
  6. int arr[] = new int[arrSize];
  7. System.out.println("How many multiplication tables do you want to print?");
  8. int n = obj.nextInt();
  9. for (int i = 0; i < n; i++) {
  10. System.out.println("Enter the number whose table you want:");
  11. arr[i] = obj.nextInt();
  12. }
  13. System.out.println("Enter the range of the multiplier:");
  14. int range = obj.nextInt();
  15. for (int i = 0; i < n; i++) {
  16. for (int j = 1; j <= range; j++) {
  17. System.out.println(arr[i] + "x" + j + " = " + (arr[i] * j));
  18. }
  19. System.out.println(); // Empty line after each table
  20. }
  21. }
  22. }

Output:

  1. How many multiplication tables do you want to print?
  2. 3
  3. Enter the number whose table you want:
  4. 2
  5. Enter the number whose table you want:
  6. 5
  7. Enter the number whose table you want:
  8. 6
  9. Enter the range of the multiplier:
  10. 4
  11. 2x1 = 2
  12. 2x2 = 4
  13. 2x3 = 6
  14. 2x4 = 8
  15. 5x1 = 5
  16. 5x2 = 10
  17. 5x3 = 15
  18. 5x4 = 20
  19. 6x1 = 6
  20. 6x2 = 12
  21. 6x3 = 18
  22. 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.

  1. import java.util.Scanner;

class Addition
{
public static void main(String[] args) {

  1. int j=0,k=0;
  2. Scanner obj=new Scanner(System.in);
  3. int arr[]=new int[32];
  4. System.out.println(&quot;how many multiplication table do you want to print ? &quot;);
  5. int n=obj.nextInt();
  6. for(int i=1;i&lt;=n;i++)
  7. {
  8. System.out.println(&quot;Enter number whos table you want &quot;);
  9. for(j=1;j&lt;=n;j++)
  10. {
  11. arr[j]=obj.nextInt(); break;
  12. }
  13. }
  14. System.out.println(&quot;Enter range of multiplier&quot;);
  15. int range=obj.nextInt();
  16. for(int l=0;l&lt;=arr[j];l++)
  17. {
  18. for(k=1;k&lt;=range;k++)
  19. {
  20. System.out.println(&quot; &quot;+l+&quot; * &quot;+k+&quot; = &quot;+l*k);
  21. }
  22. }
  23. }

}

>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)

  1. 2x1=2
  2. 2x2=4
  3. 2x3=6
  4. 2x4=8
  5. 5x1=5
  6. 5x2=10
  7. 5x3=15
  8. 5x4=20
  9. 6x1=6
  10. 6x2=12
  11. 6x3=18
  12. 6x4=24
  13. </details>
  14. # 答案1
  15. **得分**: 0
  16. 我对您试图完成的内容进行了一些调整。您可以使用下面的代码片段:
  17. ```java
  18. import java.util.ArrayList;
  19. import java.util.Scanner;
  20. class Addition {
  21. public static void main(String[] args) {
  22. Scanner obj = new Scanner(System.in);
  23. ArrayList<Integer> typeMultiplicationTable = new ArrayList<Integer>();
  24. System.out.println("要生成多少个乘法表?");
  25. int n = obj.nextInt();
  26. for (int i = 1; i <= n; i++) {
  27. System.out.println("请输入您想要的乘法表的数字:");
  28. typeMultiplicationTable.add(obj.nextInt());
  29. }
  30. System.out.println("请输入范围:");
  31. int range = obj.nextInt();
  32. for (int l = 0; l < typeMultiplicationTable.size(); l++) {
  33. for (int k = 0; k <= range; k++) {
  34. System.out.println(typeMultiplicationTable.get(l) + " * " + k + " = " + typeMultiplicationTable.get(l) * k);
  35. }
  36. }
  37. }
  38. }

如果您最终决定使用数组,您可以按照以下方式实现,我删除了一个for循环:

  1. import java.util.Scanner;
  2. class Addition {
  3. public static void main(String[] args) {
  4. Scanner obj = new Scanner(System.in);
  5. System.out.println("要生成多少个乘法表?");
  6. int n = obj.nextInt();
  7. int arr[] = new int[n];
  8. for (int j = 0; j < arr.length; j++) {
  9. System.out.println("请输入您想要的乘法表的数字:");
  10. arr[j] = obj.nextInt();
  11. }
  12. System.out.println("请输入范围:");
  13. int range = obj.nextInt();
  14. for (int l = 0; l < arr.length; l++) {
  15. for (int k = 0; k <= range; k++) {
  16. System.out.println(arr[l] + " * " + k + " = " + arr[l] * k);
  17. }
  18. }
  19. }
  20. }
英文:

I made a few adjustments to carry out what i think you trying to accomplish. you could use an arraylist seen below:

  1. class Addition{
  2. public static void main(String[] args) {
  3. int j=0,k=0;
  4. Scanner obj=new Scanner(System.in);
  5. ArrayList&lt;Integer&gt; typeMultiplicationTable = new ArrayList&lt;Integer&gt;();
  6. //used Arraylist instead of an array
  7. //int arr[]=new int[32];
  8. System.out.println(&quot;how many table? &quot;);
  9. int n=obj.nextInt();
  10. for(int i=1;i&lt;=n;i++)
  11. {
  12. System.out.println(&quot;Enter number whos table you want &quot;);
  13. for(j=0;j&lt;=n;j++)
  14. {
  15. typeMultiplicationTable.add(obj.nextInt());
  16. break;
  17. //arr[j]=obj.nextInt(); break;
  18. }
  19. }
  20. System.out.println(&quot;Enter range&quot;);
  21. int range=obj.nextInt();
  22. for(int l=0;l&lt;=typeMultiplicationTable.size()-1;l++)
  23. {
  24. for(k=0;k&lt;=range;k++)
  25. {
  26. System.out.println(&quot; &quot;+typeMultiplicationTable.get(l)+&quot; * &quot;+k+&quot; = &quot;+typeMultiplicationTable.get(l)*k);
  27. }
  28. }
  29. }}

and if you do end up using an array, you could achieve it as follows - i ended up removing one of the for loops.

  1. class Addition{
  2. public static void main(String[] args) {
  3. Scanner obj=new Scanner(System.in);
  4. System.out.println(&quot;how many table? &quot;);
  5. int n=obj.nextInt();
  6. int arr[]=new int[n];
  7. for(int j=0;j&lt;=arr.length-1;j++)
  8. {
  9. System.out.println(&quot;Enter number whos table you want &quot;);
  10. arr[j]=obj.nextInt();
  11. }
  12. System.out.println(&quot;Enter range&quot;);
  13. int range=obj.nextInt();
  14. for(int l=0;l&lt;=arr.length-1;l++)
  15. {
  16. for(int k=0;k&lt;=range;k++)
  17. {
  18. System.out.println(&quot; &quot;+arr[l]+&quot; * &quot;+k+&quot; = &quot;+arr[l]*k);
  19. }
  20. }
  21. }}

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:

确定