英文:
my output is showing only the last input in array object
问题
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("要创建的 X 类对象数量:");
int choose = sc.nextInt();
X obj[] = new X[choose];
if (choose >= 2 && choose % 2 == 0) {
int input;
for (int i = 0; i < choose; i++) {
obj[i] = new X(); // Create new X object
System.out.print("为第 " + ordinal(i + 1) + " 个对象输入奇数值: ");
input = sc.nextInt();
obj[i].odd = input;
}
} else {
System.out.println("输入数量无效,至少要求创建两个对象且数量为偶数。");
return;
}
for (int k = 0; k < choose; k++) {
System.out.println(obj[k].odd);
}
}
public static String ordinal(int i) {
int mod100 = i % 100;
int mod10 = i % 10;
if (mod10 == 1 && mod100 != 11) {
return i + "st";
} else if (mod10 == 2 && mod100 != 12) {
return i + "nd";
} else if (mod10 == 3 && mod100 != 13) {
return i + "rd";
} else {
return i + "th";
}
}
}
class X {
int odd;
}
输出应该如下所示:
要创建的 X 类对象数量:4
为第 1 个对象输入奇数值: 5
为第 2 个对象输入奇数值: 4
为第 3 个对象输入奇数值: 3
为第 4 个对象输入奇数值: 2
5
4
3
2
在代码中的主要更改是在循环中创建了新的 X
对象,并将其分配给 obj[i]
,这样每个对象都有自己的实例变量。之前的问题在于没有为数组中的每个元素分配新的对象,导致操作的是同一个静态变量。
英文:
i am just practicing how to use array object,in the program below i have allowed only even number of object is creatable,the code is given below,please see the output below to understand the error
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("the number of object you want to create for class X : ");
int choose = sc.nextInt();
X obj[] = new X[choose];
if(choose>=2 && choose%2==0)
{
int input;
for(int i=0;i<choose;i++)
{
System.out.print("insert the value for the "+ordinal(i+1)+" obj against odd: ");
input=sc.nextInt();
obj[i].odd=input;
}
}
else return;
for(int k=0;k<choose;k++)
{
System.out.println(obj[k].odd);
}
}
public static String ordinal(int i) {
int mod100 = i % 100;
int mod10 = i % 10;
if (mod10 == 1 && mod100 != 11) {
return i + "st";
} else if (mod10 == 2 && mod100 != 12) {
return i + "nd";
} else if (mod10 == 3 && mod100 != 13) {
return i + "rd";
} else {
return i + "th";
}
}
}
class X
{
static int odd;
}
the output is showing something like this below
the number of object you want to create for class X : 4
insert the value for the 1st obj against odd: 5
insert the value for the 2nd obj against odd: 4
insert the value for the 3rd obj against odd: 3
insert the value for the 4th obj against odd: 2
2
2
2
2
while I am expecting my output to be
the number of object you want to create for class X : 4
insert the value for the 1st obj against odd: 5
insert the value for the 2nd obj against odd: 4
insert the value for the 3rd obj against odd: 3
insert the value for the 4th obj against odd: 2
5
4
3
2
please advice me what change I need to bring in my code thank you
答案1
得分: 1
你的类 X
的数据成员是 static
,这意味着它属于类,而不是特定的实例,而且在每次循环迭代中都会被重写。相反地,你应该将其作为实例成员(可能需要一个构造函数来初始化它):
class X
{
int odd;
public X(int odd) {
this.odd = odd;
}
}
然后在循环的每次迭代中创建一个新实例:
for (int i = 0; i < choose; i++) {
System.out.print("insert the value for the " + ordinal(i + 1) + " obj against odd: ");
int input = sc.nextInt();
obj[i] = new X(input);
}
英文:
Your class X
's data member is static
, meaning it belongs to the class, not a specific instance, and you just keep overwriting it in each iteration of the loop. Instead, you should have it as an instance member (and probably have a constructor to initialize it:
class X
{
int odd;
public X(int odd) {
this.odd = odd;
}
}
And then create a new instance in each iteration of the loop:
for (int i = 0; i < choose; i++) {
System.out.print("insert the value for the " + ordinal(i + 1) + " obj against odd: ");
int input = sc.nextInt();
obj[i] = new X(input);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论