如何将一个长整型变量分配给Java中上一个命令的输出?

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

How to assign a long variable to an output in the previous command in Java?

问题

我是你的中文翻译,以下是翻译好的内容:

我像这样开始了代码:

  1. public static long lcm_of_array_elements(int[] element_array) {
  2. long lcm_of_array_elements = 1;
  3. ....
  4. if (counter == element_array.length) {
  5. return lcm_of_array_elements;

在我的驱动代码中:

  1. int[] element_array = { a, b, c, d };
  2. System.out.println(lcm_of_array_elements(element_array));

在这里,我得到了正确的值,但我想为这个值分配一些变量以执行一些操作。我尝试了以下方法:

  1. long val = lcm_of_array_elements(element_array);

然而,这只返回了最初定义的值:1。所以我尝试了这个,但是它显示了一个错误:

  1. long val = lcm_of_array_elements(element_array).Last.val;

我能否请你提供一个命令来将值存储在某个变量中?

让我们取这些值:a=2,b=3,c=4,d=5

  1. System.out.println(lcm_of_array_elements(element_array));
  2. System.out.println(lcm_of_array_elements(element_array));
  3. long k = lcm_of_array_elements(element_array);
  4. System.out.println(k);

我的输出是:

  1. 60
  2. 1

但我需要它是:

  1. 60
  2. 60

以下是完整的代码:

  1. // 检查4个数字的最小公倍数是否可被它们的和整除
  2. public static long lcm_of_array_elements(int[] element_array)
  3. {
  4. long lcm_of_array_elements = 1;
  5. int divisor = 2;
  6. while (true) {
  7. int counter = 0;
  8. boolean divisible = false;
  9. for (int i = 0; i < element_array.length; i++) {
  10. if (element_array[i] == 0) {
  11. return 0;
  12. }
  13. else if (element_array[i] < 0) {
  14. element_array[i] = element_array[i] * (-1);
  15. }
  16. if (element_array[i] == 1) {
  17. counter++;
  18. }
  19. if (element_array[i] % divisor == 0) {
  20. divisible = true;
  21. element_array[i] = element_array[i] / divisor;
  22. }
  23. }
  24. if (divisible) {
  25. lcm_of_array_elements = lcm_of_array_elements * divisor;
  26. }
  27. else {
  28. divisor++;
  29. }
  30. if (counter == element_array.length) {
  31. return lcm_of_array_elements;
  32. }
  33. }
  34. }
  35. // 主驱动代码
  36. public static void main(String[] args)
  37. {
  38. Random rand = new Random();
  39. int f = 4;
  40. while(f == 4){
  41. int a = rand.nextInt(100);
  42. int b = rand.nextInt(100);
  43. int c = rand.nextInt(100);
  44. int d = rand.nextInt(100);
  45. System.out.println(a);
  46. System.out.println(b);
  47. System.out.println(c);
  48. System.out.println(d);
  49. int e = (a + b + c + d);
  50. int[] element_array = { a, b, c, d };
  51. System.out.println(e);
  52. System.out.println(lcm_of_array_elements(element_array));
  53. long k = lcm_of_array_elements(element_array);
  54. System.out.println(k);
  55. Scanner sc = new Scanner(System.in);
  56. int y = sc.nextInt();
  57. if(y % e == 0){
  58. System.out.println("SUCCESS");
  59. f = f + 1;
  60. }
  61. else{
  62. System.out.println("FAIL");
  63. }
  64. }
  65. }

如果你有更多问题,可以随时问我。

英文:

I started the code like this and

  1. public static long lcm_of_array_elements(int[] element_array) {
  2. long lcm_of_array_elements = 1;
  3. ....
  4. if (counter == element_array.length) {
  5. return lcm_of_array_elements;

In my driver code:

  1. int[] element_array = { a, b, c, d };
  2. System.out.println(lcm_of_array_elements(element_array));

Here, I was getting the right value but I want to assign some variable to this value to perform some operations. I tried the following:

  1. long val=lcm_of_array_elements(element_array) ;

However this only returned the initially defined value : 1.
So I tried, this but it displayed an error:

  1. long val=lcm_of_array_elements(element_array).Last.val ;

Could I please get a command to store the value in some variable?

Let us take values: a=2,b=3,c=4,d=5

  1. System.out.println(lcm_of_array_elements(element_array));
  2. System.out.println(lcm_of_array_elements(element_array));
  3. long k=lcm_of_array_elements(element_array) ;
  4. System.out.println(k);

My output is:

  1. 60
  2. 1

But I need it to be:

  1. 60
  2. 60

Here is the full code:

  1. // To check if the LCM of 4 numbers is divisible by their sum
  2. public static long lcm_of_array_elements(int[] element_array)
  3. {
  4. long lcm_of_array_elements = 1;
  5. int divisor = 2;
  6. while (true) {
  7. int counter = 0;
  8. boolean divisible = false;
  9. for (int i = 0; i &lt; element_array.length; i++) {
  10. if (element_array[i] == 0) {
  11. return 0;
  12. }
  13. else if (element_array[i] &lt; 0) {
  14. element_array[i] = element_array[i] * (-1);
  15. }
  16. if (element_array[i] == 1) {
  17. counter++;
  18. }
  19. if (element_array[i] % divisor == 0) {
  20. divisible = true;
  21. element_array[i] = element_array[i] / divisor;
  22. }
  23. }
  24. if (divisible) {
  25. lcm_of_array_elements = lcm_of_array_elements * divisor;
  26. }
  27. else {
  28. divisor ++;
  29. }
  30. if (counter == element_array.length) {
  31. return lcm_of_array_elements;
  32. }
  33. }
  34. }
  35. // Driver Code
  36. public static void main(String[] args)
  37. {
  38. Random rand = new Random() ;
  39. int f = 4;
  40. while(f == 4){
  41. int a = rand.nextInt(100) ;
  42. int b = rand.nextInt(100) ;
  43. int c = rand.nextInt(100) ;
  44. int d = rand.nextInt(100) ;
  45. System.out.println(a) ;
  46. System.out.println(b) ;
  47. System.out.println(c) ;
  48. System.out.println(d) ;
  49. int e = (a + b + c + d) ;
  50. int[] element_array = { a, b, c, d };
  51. System.out.println(e) ;
  52. System.out.println(lcm_of_array_elements(element_array));
  53. long k = lcm_of_array_elements(element_array) ;
  54. System.out.println(k) ;
  55. Scanner sc = new Scanner(System.in) ;
  56. int y = sc.nextInt();
  57. if(y%e==0){
  58. System.out.println(&quot;SUCCESS&quot;);
  59. f = f + 1 ;
  60. }
  61. else{
  62. System.out.println(&quot;FAIL&quot;);
  63. }
  64. }
  65. }
  66. }

答案1

得分: -1

  1. import java.util.Random;
  2. import java.util.Scanner;
  3. public class Main {
  4. public static void main(String[] args) {
  5. Random rand = new Random();
  6. int f = 4;
  7. while (f == 4) {
  8. int a = rand.nextInt(100);
  9. int b = rand.nextInt(100);
  10. int c = rand.nextInt(100);
  11. int d = rand.nextInt(100);
  12. System.out.println(a);
  13. System.out.println(b);
  14. System.out.println(c);
  15. System.out.println(d);
  16. int e = (a + b + c + d);
  17. int[] element_array = { a, b, c, d };
  18. System.out.println(e);
  19. //System.out.println(lcm_of_array_elements(element_array));
  20. long k = lcm_of_array_elements(element_array);
  21. System.out.println("K: " + k);
  22. Scanner sc = new Scanner(System.in);
  23. int y = sc.nextInt();
  24. if (y % e == 0) {
  25. System.out.println("SUCCESS");
  26. f = f + 1;
  27. } else {
  28. System.out.println("FAIL");
  29. }
  30. }
  31. }
  32. public static long lcm_of_array_elements(int[] element_array) {
  33. long lcm_of_array_elements = 1;
  34. int divisor = 2;
  35. long c = 1;
  36. while (true) {
  37. int counter = 0;
  38. boolean divisible = false;
  39. for (int i = 0; i < element_array.length; i++) {
  40. if (element_array[i] == 0) {
  41. return 0;
  42. } else if (element_array[i] < 0) {
  43. element_array[i] = element_array[i] * (-1);
  44. }
  45. if (element_array[i] == 1) {
  46. counter++;
  47. }
  48. if (element_array[i] % divisor == 0) {
  49. divisible = true;
  50. element_array[i] = element_array[i] / divisor;
  51. }
  52. }
  53. if (divisible) {
  54. c = c * divisor;
  55. } else {
  56. divisor++;
  57. }
  58. if (counter == element_array.length) {
  59. return c;
  60. }
  61. }
  62. }
  63. }
英文:

Try

  1. import java.util.Random;
  2. import java.util.Scanner;
  3. public class Main
  4. {
  5. public static void main (String[]args)
  6. {
  7. Random rand = new Random ();
  8. int f = 4;
  9. while (f == 4)
  10. {
  11. int a = rand.nextInt (100);
  12. int b = rand.nextInt (100);
  13. int c = rand.nextInt (100);
  14. int d = rand.nextInt (100);
  15. System.out.println (a);
  16. System.out.println (b);
  17. System.out.println (c);
  18. System.out.println (d);
  19. int e = (a + b + c + d);
  20. int[] element_array = { a, b, c, d };
  21. System.out.println (e);
  22. //System.out.println (lcm_of_array_elements (element_array));
  23. long k = lcm_of_array_elements (element_array);
  24. System.out.println (&quot;K: &quot;+k);
  25. Scanner sc = new Scanner (System.in);
  26. int y = sc.nextInt ();
  27. if (y % e == 0)
  28. {
  29. System.out.println (&quot;SUCCESS&quot;);
  30. f = f + 1;
  31. }
  32. else
  33. {
  34. System.out.println (&quot;FAIL&quot;);
  35. }
  36. }
  37. }
  38. public static long lcm_of_array_elements (int[]element_array)
  39. {
  40. long lcm_of_array_elements = 1;
  41. int divisor = 2;
  42. long c=1;
  43. while (true)
  44. {
  45. int counter = 0;
  46. boolean divisible = false;
  47. for (int i = 0; i &lt; element_array.length; i++)
  48. {
  49. if (element_array[i] == 0)
  50. {
  51. return 0;
  52. }
  53. else if (element_array[i] &lt; 0)
  54. {
  55. element_array[i] = element_array[i] * (-1);
  56. }
  57. if (element_array[i] == 1)
  58. {
  59. counter++;
  60. }
  61. if (element_array[i] % divisor == 0)
  62. {
  63. divisible = true;
  64. element_array[i] = element_array[i] / divisor;
  65. }
  66. }
  67. if (divisible)
  68. {
  69. c = c * divisor;
  70. }
  71. else
  72. {
  73. divisor++;
  74. }
  75. if (counter == element_array.length)
  76. {
  77. return c;
  78. }
  79. }
  80. }
  81. }

huangapple
  • 本文由 发表于 2020年9月27日 14:52:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64085698.html
匿名

发表评论

匿名网友

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

确定