我的输出只显示数组对象中的最后一个输入。

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

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(&quot;the number of object you want to create for class X : &quot;);
int choose = sc.nextInt();
X obj[] = new X[choose];
if(choose&gt;=2 &amp;&amp; choose%2==0)
{
int input;
for(int i=0;i&lt;choose;i++)
{
System.out.print(&quot;insert the value for the &quot;+ordinal(i+1)+&quot; obj against odd: &quot;);
input=sc.nextInt();
obj[i].odd=input;
}
}
else return;
for(int k=0;k&lt;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 &amp;&amp; mod100 != 11) {
return i + &quot;st&quot;;
} else if (mod10 == 2 &amp;&amp; mod100 != 12) {
return i + &quot;nd&quot;;
} else if (mod10 == 3 &amp;&amp; mod100 != 13) {
return i + &quot;rd&quot;;
} else {
return i + &quot;th&quot;;
}
}
}
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 &lt; choose; i++) {
    System.out.print(&quot;insert the value for the &quot; + ordinal(i + 1) + &quot; obj against odd: &quot;);
    int input = sc.nextInt();
    obj[i] = new X(input);
}

huangapple
  • 本文由 发表于 2020年4月10日 15:53:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/61136130.html
匿名

发表评论

匿名网友

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

确定