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

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

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

问题

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

我像这样开始了代码:

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

在我的驱动代码中:

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

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

long val = lcm_of_array_elements(element_array);

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

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

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

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

System.out.println(lcm_of_array_elements(element_array)); 
System.out.println(lcm_of_array_elements(element_array)); 
long k = lcm_of_array_elements(element_array);
System.out.println(k);

我的输出是:

60
1

但我需要它是:

60
60

以下是完整的代码:

// 检查4个数字的最小公倍数是否可被它们的和整除
public static long lcm_of_array_elements(int[] element_array) 
{ 
    long lcm_of_array_elements = 1; 
    int divisor = 2; 
    
    while (true) { 
        int counter = 0; 
        boolean divisible = false; 
        
        for (int i = 0; i < element_array.length; i++) { 
            
            if (element_array[i] == 0) { 
                return 0; 
            } 
            else if (element_array[i] < 0) { 
                element_array[i] = element_array[i] * (-1); 
            } 
            if (element_array[i] == 1) { 
                counter++; 
            } 
            
            if (element_array[i] % divisor == 0) { 
                divisible = true; 
                element_array[i] = element_array[i] / divisor; 
            } 
        } 
        
        if (divisible) { 
            lcm_of_array_elements = lcm_of_array_elements * divisor; 
        } 
        else { 
            divisor++; 
        } 
        
        if (counter == element_array.length) { 
            return lcm_of_array_elements; 
            
        } 
    } 
} 

// 主驱动代码
public static void main(String[] args) 
{ 
    Random rand = new Random();
    int f = 4;
    
    while(f == 4){
        int a = rand.nextInt(100);
        int b = rand.nextInt(100);
        int c = rand.nextInt(100);
        int d = rand.nextInt(100);
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
        int e = (a + b + c + d);
        int[] element_array = { a, b, c, d }; 
        System.out.println(e);
        System.out.println(lcm_of_array_elements(element_array)); 
        long k = lcm_of_array_elements(element_array);
        System.out.println(k);
       
        Scanner sc = new Scanner(System.in);
        int y = sc.nextInt();
        if(y % e == 0){
            System.out.println("SUCCESS");
            f = f + 1;
        }
        else{
            System.out.println("FAIL");
        }
    }
} 

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

英文:

I started the code like this and

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

In my driver code:

int[] element_array = { a, b, c, d }; 
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:

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:

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

System.out.println(lcm_of_array_elements(element_array)); 
System.out.println(lcm_of_array_elements(element_array)); 
long k=lcm_of_array_elements(element_array) ;
System.out.println(k);

My output is:

60
1

But I need it to be:

60
60

Here is the full code:

// To check if the LCM of 4 numbers is divisible by their sum
public static long lcm_of_array_elements(int[] element_array) 
{ 
long lcm_of_array_elements = 1; 
int divisor = 2; 
while (true) { 
int counter = 0; 
boolean divisible = false; 
for (int i = 0; i &lt; element_array.length; i++) { 
if (element_array[i] == 0) { 
return 0; 
} 
else if (element_array[i] &lt; 0) { 
element_array[i] = element_array[i] * (-1); 
} 
if (element_array[i] == 1) { 
counter++; 
} 
if (element_array[i] % divisor == 0) { 
divisible = true; 
element_array[i] = element_array[i] / divisor; 
} 
} 
if (divisible) { 
lcm_of_array_elements = lcm_of_array_elements * divisor; 
} 
else { 
divisor ++; 
} 
if (counter == element_array.length) { 
return lcm_of_array_elements; 
} 
} 
} 
// Driver Code 
public static void main(String[] args) 
{ 
Random rand = new Random() ;
int f = 4;
while(f == 4){
int a = rand.nextInt(100) ;
int b = rand.nextInt(100) ;
int c = rand.nextInt(100) ;
int d = rand.nextInt(100) ;
System.out.println(a) ;
System.out.println(b) ;
System.out.println(c) ;
System.out.println(d) ;
int e = (a + b + c + d) ;
int[] element_array = { a, b, c, d }; 
System.out.println(e) ;
System.out.println(lcm_of_array_elements(element_array)); 
long k = lcm_of_array_elements(element_array) ;
System.out.println(k) ;
Scanner sc = new Scanner(System.in) ;
int y = sc.nextInt();
if(y%e==0){
System.out.println(&quot;SUCCESS&quot;);
f = f + 1 ;
}
else{
System.out.println(&quot;FAIL&quot;);
}
}
} 
} 

答案1

得分: -1

import java.util.Random;
import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Random rand = new Random();
    int f = 4;

    while (f == 4) {
      int a = rand.nextInt(100);
      int b = rand.nextInt(100);
      int c = rand.nextInt(100);
      int d = rand.nextInt(100);
      System.out.println(a);
      System.out.println(b);
      System.out.println(c);
      System.out.println(d);
      int e = (a + b + c + d);
      int[] element_array = { a, b, c, d };
      System.out.println(e);
      //System.out.println(lcm_of_array_elements(element_array));
      long k = lcm_of_array_elements(element_array);
      System.out.println("K: " + k);

      Scanner sc = new Scanner(System.in);
      int y = sc.nextInt();
      if (y % e == 0) {
        System.out.println("SUCCESS");
        f = f + 1;
      } else {
        System.out.println("FAIL");
      }
    }
  }

  public static long lcm_of_array_elements(int[] element_array) {
    long lcm_of_array_elements = 1;
    int divisor = 2;
    long c = 1;

    while (true) {
      int counter = 0;
      boolean divisible = false;

      for (int i = 0; i < element_array.length; i++) {
        if (element_array[i] == 0) {
          return 0;
        } else if (element_array[i] < 0) {
          element_array[i] = element_array[i] * (-1);
        }
        if (element_array[i] == 1) {
          counter++;
        }

        if (element_array[i] % divisor == 0) {
          divisible = true;
          element_array[i] = element_array[i] / divisor;
        }
      }

      if (divisible) {
        c = c * divisor;
      } else {
        divisor++;
      }

      if (counter == element_array.length) {
        return c;
      }
    }
  }
}
英文:

Try

import java.util.Random; 
import java.util.Scanner; 
public class Main
{
public static void main (String[]args)
{
Random rand = new Random ();
int f = 4;
while (f == 4)
{
int a = rand.nextInt (100);
int b = rand.nextInt (100);
int c = rand.nextInt (100);
int d = rand.nextInt (100);
System.out.println (a);
System.out.println (b);
System.out.println (c);
System.out.println (d);
int e = (a + b + c + d);
int[] element_array = { a, b, c, d };
System.out.println (e);
//System.out.println (lcm_of_array_elements (element_array));
long k = lcm_of_array_elements (element_array);
System.out.println (&quot;K: &quot;+k);
Scanner sc = new Scanner (System.in);
int y = sc.nextInt ();
if (y % e == 0)
{
System.out.println (&quot;SUCCESS&quot;);
f = f + 1;
}
else
{
System.out.println (&quot;FAIL&quot;);
}
}
}
public static long lcm_of_array_elements (int[]element_array)
{
long lcm_of_array_elements = 1;
int divisor = 2;
long c=1;
while (true)
{
int counter = 0;
boolean divisible = false;
for (int i = 0; i &lt; element_array.length; i++)
{
if (element_array[i] == 0)
{
return 0;
}
else if (element_array[i] &lt; 0)
{
element_array[i] = element_array[i] * (-1);
}
if (element_array[i] == 1)
{
counter++;
}
if (element_array[i] % divisor == 0)
{
divisible = true;
element_array[i] = element_array[i] / divisor;
}
}
if (divisible)
{
c = c * divisor;
}
else
{
divisor++;
}
if (counter == element_array.length)
{
return c;
}
}
}
}

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:

确定