简体   繁体   English

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

[英]Java: how to create 10 cats? when Cat (“cat” + i) = “cat name” not working as in other languages

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.... 如何创建10个10个Cat实例..... cat1,... cat2 .....因为在其他语言中,我可以使用“cat”|| i = ...,来创建不同的变量,我想知道如何我可以在Java中做类似的事情....

In other words, I want to name the instances I'm going to create by taking the loop information into account. 换句话说,我想通过考虑循环信息来命名我要创建的实例。

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);
    }
  } 
} 

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. 注意:这段代码会产生不正确的结果,让我难以理解,直到我仔细观察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

Here was the culprit: 这是罪魁祸首:

public class Cat
{
     static String catName;

Remove the static and you are golden. 去除static ,你是金色的。

for (int i=1; i<10; i++){     
   Cat cat = new Cat("Cat" + i);  
   System.out.println("My cat is:  " + cat ); 
}
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] ); 
    }
  } 
} 

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: 上面有很多答案,所以我只想补充一点,你不能在Java中动态更改变量名,所以下面的粗体部分是一个nono:

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

You're hardcoding the cat number: 你正在硬编码猫号码:

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

You should change that to: 你应该改为:

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

Right? 对?

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. 这创造并储存了10只猫。

Then printing is similar: 然后打印类似:

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

You can do: 你可以做:

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

Java does not support dynamically named variables, so you can't create "cat1" through "cat10" with: Java不支持动态命名变量,因此您无法通过“cat10”创建“cat1”:

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). Java的方式是创建一个数组(正如其他人所建议的那样)。 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. 您也可以使用List而不是数组。

You can create n number of Cats : 你可以创建n个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] ); 
      }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM