Java:如何创建10只猫?当 Cat ("cat" + i) = "cat name" 时不像其他语言那样工作

发布于 2024-10-01 11:02:48 字数 837 浏览 0 评论 0 原文

使用循环,我可以创建

My cat is: Cat1
...
My cat is: Cat1

但是,当我尝试使用时,

  Cat ("cat"+i) = new Cat("Cat" + i);

我犯了错误......

那么,纠正我的代码以生成

cat1 ... cat10 cat instances?


public class TestCat{ 
  public static void main(String [] args){ 

 for (int i=1; i<10; i++){     
   //Cat ("cat"+i) = new Cat("Cat 1");
   Cat cat1 = new Cat("Cat 1");  
   System.out.println("My cat is:  " + cat1 ); 
  }
 } 
} 

class Cat{

 static String catName;
 public Cat(String catName){
   this.catName=catName;
 }
 public String toString(){
   return catName;
}
}

对不起......我应该说

最简单方法是什么如何创建十个10 Cat实例.....cat1,...cat2.....因为在其他语言中,我可以使用“cat”||i = ...,来创建不同的变量,我想知道如何我可以在 Java 中做类似的事情......

换句话说,我想通过考虑循环信息来命名我将要创建的实例。

using loop, I can create

My cat is: Cat1
...
My cat is: Cat1

However, when I tried to use

  Cat ("cat"+i) = new Cat("Cat" + i);

I'm making mistakes....

So, what is the simplist way to correct my code to produce

cat1 ... cat10 cat instances?


public class TestCat{ 
  public static void main(String [] args){ 

 for (int i=1; i<10; i++){     
   //Cat ("cat"+i) = new Cat("Cat 1");
   Cat cat1 = new Cat("Cat 1");  
   System.out.println("My cat is:  " + cat1 ); 
  }
 } 
} 

class Cat{

 static String catName;
 public Cat(String catName){
   this.catName=catName;
 }
 public String toString(){
   return catName;
}
}

Sorry....I should say

How to create ten 10 Cat instances.....cat1, ...cat2.....because in other languages, I can use "cat"||i = ..., to create different varaibles, I wonder how I could do similar things in Java....

In other words, I want to name the instances I'm going to create by taking the loop information into account.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

毁梦 2024-10-08 11:02:48

如果您不知道自己会养多少只猫,请使用收藏。

public class TestCat
{ 
  public static void main(String [] args)
  { 

    Cat[] cats = new Cat[10];    
    Vector catsUnlimited = new Vector(10);    
    Cat myCat = null;

    for (int i=1; i<11; i++)
    {     
    myCat = new Cat("Cat" + i); 
      cats[i-1]= myCat; 

      catsUnlimited.addElement(new Cat("Cat" + i));

      System.out.println("My cat is:  " + cats[i-1] ); 
    }

    System.out.println("Known cats");
    for (int x = 0; x < catsUnlimited.size(); x++)
    {        
     System.out.println("Cat #" + (x+1) + ":" +(Cat)catsUnlimited.get(x));
    }    
  } 
} 

注意:这段代码产生了不正确的结果,让我感到困惑,直到我仔细查看 Cat 类。

My cat is:  Cat1
My cat is:  Cat2
My cat is:  Cat3
My cat is:  Cat4
My cat is:  Cat5
My cat is:  Cat6
My cat is:  Cat7
My cat is:  Cat8
My cat is:  Cat9
My cat is:  Cat10
Known cats
Cat #1:Cat10
Cat #2:Cat10
Cat #3:Cat10
Cat #4:Cat10
Cat #5:Cat10
Cat #6:Cat10
Cat #7:Cat10
Cat #8:Cat10
Cat #9:Cat10
Cat #10:Cat10

罪魁祸首就是:

public class Cat
{
     static String catName;

删除static,你就成功了。

Use a collection if you don't know how many cats you'll have.

public class TestCat
{ 
  public static void main(String [] args)
  { 

    Cat[] cats = new Cat[10];    
    Vector catsUnlimited = new Vector(10);    
    Cat myCat = null;

    for (int i=1; i<11; i++)
    {     
    myCat = new Cat("Cat" + i); 
      cats[i-1]= myCat; 

      catsUnlimited.addElement(new Cat("Cat" + i));

      System.out.println("My cat is:  " + cats[i-1] ); 
    }

    System.out.println("Known cats");
    for (int x = 0; x < catsUnlimited.size(); x++)
    {        
     System.out.println("Cat #" + (x+1) + ":" +(Cat)catsUnlimited.get(x));
    }    
  } 
} 

Note: This code produces incorrect results and had me stumped until I looked closer at the Cat class.

My cat is:  Cat1
My cat is:  Cat2
My cat is:  Cat3
My cat is:  Cat4
My cat is:  Cat5
My cat is:  Cat6
My cat is:  Cat7
My cat is:  Cat8
My cat is:  Cat9
My cat is:  Cat10
Known cats
Cat #1:Cat10
Cat #2:Cat10
Cat #3:Cat10
Cat #4:Cat10
Cat #5:Cat10
Cat #6:Cat10
Cat #7:Cat10
Cat #8:Cat10
Cat #9:Cat10
Cat #10:Cat10

Here was the culprit:

public class Cat
{
     static String catName;

Remove the static and you are golden.

柠栀 2024-10-08 11:02:48

这看起来像是一个学术问题,所以我会这样处理它。你需要在某个地方存放十只猫。您可以做到这一点的一个地方是在数组中..

public class TestCat{ 
  public static void main(String [] args){ 

    Cat[] cats = new Cat[10];

    // Create my cats
    for (int i= 0; i < 10; i++) {     
      cats[i] = new Cat("Cat " + i + 1);  
    }

    // Print them out
    for (Cat aCat : cats) {
      System.out.printLn("My Cat is: " + aCat);
    }
  } 
} 

This looks like an academic problem, so I'll approach it as such. You need to store ten cats somewhere. One place you can do that is in an array..

public class TestCat{ 
  public static void main(String [] args){ 

    Cat[] cats = new Cat[10];

    // Create my cats
    for (int i= 0; i < 10; i++) {     
      cats[i] = new Cat("Cat " + i + 1);  
    }

    // Print them out
    for (Cat aCat : cats) {
      System.out.printLn("My Cat is: " + aCat);
    }
  } 
} 
樱&纷飞 2024-10-08 11:02:48
for (int i=1; i<10; i++){     
   Cat cat = new Cat("Cat" + i);  
   System.out.println("My cat is:  " + cat ); 
}
for (int i=1; i<10; i++){     
   Cat cat = new Cat("Cat" + i);  
   System.out.println("My cat is:  " + cat ); 
}
梦中的蝴蝶 2024-10-08 11:02:48
public class TestCat
{ 
  public static void main(String [] args)
  { 

    Cat[] cats = new Cat[10];
    for (int i=1; i<11; i++)
    {     
      cats[i-1]= new Cat("Cat" + i);

      System.out.println("My cat is:  " + cats[i-1] ); 
    }
  } 
} 
public class TestCat
{ 
  public static void main(String [] args)
  { 

    Cat[] cats = new Cat[10];
    for (int i=1; i<11; i++)
    {     
      cats[i-1]= new Cat("Cat" + i);

      System.out.println("My cat is:  " + cats[i-1] ); 
    }
  } 
} 
野の 2024-10-08 11:02:48

上面有很多答案,所以我只补充一点,你不能在 Java 中动态更改变量名称,所以下面的粗体部分是一个诺诺:

Cat ("cat"+i) = new Cat( “猫 1”);

Lots of answers above so I'll just add that you can't dynamically change variable names in Java, so the bolded part below is a nono:

Cat ("cat"+i) = new Cat("Cat 1");

客…行舟 2024-10-08 11:02:48

您正在对猫号进行硬编码:

Cat cat1 = new Cat("Cat 1"); 

您应该将其更改为:

Cat cat1 = new Cat("Cat " + i); 

对吗?

You're hardcoding the cat number:

Cat cat1 = new Cat("Cat 1"); 

You should change that to:

Cat cat1 = new Cat("Cat " + i); 

Right?

Oo萌小芽oO 2024-10-08 11:02:48

你需要一个数组。

  String[] cats = new String[10];

  for(int i = 0; i < cats.length; i++){
     cats[i] = "Cat" + i;
  }

这将创建并存储 10 只猫。

那么打印也是类似的:

  for(int i = 0; i < cats.length; i++){
     System.out.println("My cat is " + cats[i]);
  }

You need an array.

  String[] cats = new String[10];

  for(int i = 0; i < cats.length; i++){
     cats[i] = "Cat" + i;
  }

This creates and stores the 10 cats.

Then printing is similar:

  for(int i = 0; i < cats.length; i++){
     System.out.println("My cat is " + cats[i]);
  }
也只是曾经 2024-10-08 11:02:48

你可以这样做:

for (int i=1; i<10; i++){
        Cat cat = new Cat("Cat"+i);
        System.out.println("My cat is:  " + cat );
}

You can do:

for (int i=1; i<10; i++){
        Cat cat = new Cat("Cat"+i);
        System.out.println("My cat is:  " + cat );
}
知你几分 2024-10-08 11:02:48

Java 不支持动态命名变量,因此您无法使用以下命令创建“cat1”到“cat10”:

Cat ("cat"+i) = new Cat("Cat" + i);

(顺便说一句,您来自哪种语言?)

Java 方法是创建一个数组(正如其他人所建议的那样)。如果您不知道需要多少个实例,您可以动态调整数组的大小:

public class TestCat
{
    static class Cat
    {
        private String name;

        Cat(String name)
        {
            this.name = name;
        }

        @Override
        public String toString()
        {
            return "Cat{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }
    public static void main(String[] args)
    {
        Cat[] someCats = createCats(10);
        printCats(someCats);

        Cat[] lotsaCats = createCats(42);
        printCats(lotsaCats);
    }

    private static void printCats(Cat[] cats)
    {
        // Print them out
        for (Cat aCat : cats)
        {
            System.out.println("My Cat is: " + aCat);
        }
    }

    private static Cat[] createCats(int ncats)
    {
        Cat[] cats = new Cat[ncats];

        // Create my cats
        for (int i = 0; i < ncats; i++)
        {
            cats[i] = new Cat("Cat " + i + 1);
        }
        return cats;
    }
} 

您也可以使用列表而不是数组。

Java does not support dynamically named variables, so you can't create "cat1" through "cat10" with:

Cat ("cat"+i) = new Cat("Cat" + i);

(BTW, what language are you coming from?)

The Java way would be to create an array (as others have suggested). If you don't know how many instances you will need, you can dynamically size the array:

public class TestCat
{
    static class Cat
    {
        private String name;

        Cat(String name)
        {
            this.name = name;
        }

        @Override
        public String toString()
        {
            return "Cat{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }
    public static void main(String[] args)
    {
        Cat[] someCats = createCats(10);
        printCats(someCats);

        Cat[] lotsaCats = createCats(42);
        printCats(lotsaCats);
    }

    private static void printCats(Cat[] cats)
    {
        // Print them out
        for (Cat aCat : cats)
        {
            System.out.println("My Cat is: " + aCat);
        }
    }

    private static Cat[] createCats(int ncats)
    {
        Cat[] cats = new Cat[ncats];

        // Create my cats
        for (int i = 0; i < ncats; i++)
        {
            cats[i] = new Cat("Cat " + i + 1);
        }
        return cats;
    }
} 

You could also use a List instead of an array.

只为守护你 2024-10-08 11:02:48

您可以创建n只猫:

代码:

public class TestCat
{

  public static void main(String [] args) throws IOException
  {  
      int x=0; 
      DataInputStream in=new DataInputStream(System.in);
      x=Integer.parseInt(in.readLine());
      Cat[] cats = new Cat[x];
      for (int i=1; i<x; i++)
      {     
          cats[i-1]= new Cat("Cat" + i);       
          System.out.println("My cat is:  " + cats[i-1] ); 
      }
   }
}

You can create n number of Cats :

Code:

public class TestCat
{

  public static void main(String [] args) throws IOException
  {  
      int x=0; 
      DataInputStream in=new DataInputStream(System.in);
      x=Integer.parseInt(in.readLine());
      Cat[] cats = new Cat[x];
      for (int i=1; i<x; i++)
      {     
          cats[i-1]= new Cat("Cat" + i);       
          System.out.println("My cat is:  " + cats[i-1] ); 
      }
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文