英文:
How do I set a variable from a loop in to my array and then show the array in a different class?
问题
Good evening, im struggeling again...
I want a loop from my first class to calculate the value x and then put the value x in my Array and then display the array in my main class. Whats my mistake? I would be so thankfull if any advise could solve this issue.
```java
import java.util.Arrays;
public class Data {
public double for1() {
double x = 2;
for (int i = 0; i < 8; i++) {
x = x + x;
}
return x;
}
double[] arr = { x, 3, 4, 4, -5, 4, 6, 2 }; // x cannot be resolved to a variable
}
//-------------
import java.util.Arrays;
public class Javaapp {
public static void main(String[] args) {
Data a = new Data();
a.for1();
System.out.println(Arrays.toString(a.arr));
}
}
英文:
Good evening, im struggeling again...
I want a loop from my first class to calculate the value x and then put the value x in my Array and then display the array in my main class. Whats my mistake? I would be so thankfull if any advise could solve this issue.
import java.util.Arrays;
public class Data {
public double for1() {
double x = 2;
for (int i = 0; i < 8; i++) {
x = x + x;
}
return x;
}
double[] arr = { x, 3, 4, 4, -5, 4, 6, 2 }; // x cannot be resolved to a variable
}
//-------------
import java.util.Arrays;
public class Javaapp {
public static void main(String[] args) {
Data a = new Data();
a.for1();
System.out.println(Arrays.toString(a.arr));
}
}
答案1
得分: 2
以下是您要的翻译内容:
有一种可能性是在数组赋值中调用 for1
函数。如果这样做,您就不需要在 main
程序中调用它:
import java.util.Arrays;
class Data {
public double for1() {
double x = 2;
for (int i = 0; i < 8; i++) {
x = x + x;
}
return x;
}
double[] arr = { for1(), 3, 4, 4, -5, 4, 6, 2 };
}
public class Main
{
public static void main(String args[])
{
Data a = new Data();
System.out.println(Arrays.toString(a.arr));
}
}
英文:
One possibility would be to call the for1
function in your array assignment. If you do that, you wouldn't need to call it in the main
routine:
import java.util.Arrays;
class Data {
public double for1() {
double x = 2;
for (int i = 0; i < 8; i++) {
x = x + x;
}
return x;
}
double[] arr = { for1(), 3, 4, 4, -5, 4, 6, 2 };
}
public class Main
{
public static void main(String args[])
{
Data a = new Data();
System.out.println(Arrays.toString(a.arr));
}
}
答案2
得分: 1
问题是,变量 x
只能从方法 for1()
内部访问。解决方法是将变量 x
移出该方法,或在调用方法后将 x
放入数组中。
我建议的代码如下,只要先调用 for1()
方法,就能正常工作。
import java.util.Arrays;
class Data {
public double for1() {
double x = 2;
for (int i = 0; i < 8; i++) {
x = x + x;
}
// 将数组的第一个索引设置为新的 x 变量
arr[0] = x;
return x;
}
// 为现在的占位符值
double[] arr = { 2, 3, 4, 4, -5, 4, 6, 2 };
}
public class Main {
public static void main(String[] args) {
Data a = new Data();
a.for1();
System.out.println(Arrays.toString(a.arr));
}
}
英文:
The issue is, the variable x
is only accessible from within the method for1()
. A solution for this would be to move the variable x
outside of the method, or put x
into the array after the method is called.
what I would recommend is the code below, it will work as long as for1()
is called first.
import java.util.Arrays;
class Data {
public double for1() {
double x = 2;
for (int i = 0; i < 8; i++) {
x = x + x;
}
//we set the first index in the array to our new x variable
arr[0] = x;
return x;
}
// v place holder value for now
double[] arr = { 2, 3, 4, 4, -5, 4, 6, 2 };
}
public class Main {
public static void main(String[] args) {
Data a = new Data();
a.for1();
System.out.println(Arrays.toString(a.arr));
}
}
答案3
得分: 0
以下是翻译好的部分:
主要问题在于变量x对于arr数组不可访问。这是因为x仅存在于for1()函数的范围内。解决这个问题的最简单方法有:
1)将x变成全局变量
private double x = 2;
public void for1(){
for (int i = 0; i < 8; i++) {
x = x + x;
}
}
double[] arr = { x, 3, 4, 4, -5, 4, 6, 2 };
2)让for1()函数返回x,正如大多数人已经建议的那样
public double for1(){
double x = 2;
for (int i = 0; i < 8; i++) {
x = x + x;
}
return x;
}
double[] arr = { for1(), 3, 4, 4, -5, 4, 6, 2 };
英文:
The main problem here is that the variable x is not accessible to the arr array. This is because x only exists in the scope of the for1() function. The simplest ways to get around this are:
- Make x a global variable
private double x = 2;
public void for1(){
for (int i = 0; i < 8; i++) {
x = x + x;
}
}
double[] arr = { x, 3, 4, 4, -5, 4, 6, 2 };
- Make for1() return x like most people have already suggested
public double for1(){
double x = 2;
for (int i = 0; i < 8; i++) {
x = x + x;
}
return x;
}
double[] arr = { for1(), 3, 4, 4, -5, 4, 6, 2 };
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论