印刷阵列错误 [英] Printing array error

查看:157
本文介绍了印刷阵列错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个简单的解决,但我只是没有看到它。我想弄清楚,我怎么得到我的 printOut的()方法从主正确打印 Project5PartA ?我需要获取,设置并返回的方法呢?另外,是我while循环甚至有必要在测试类?

该程序编译并继续运行到无穷远,所以我想while循环是错误的。但它也只能打印出 [Ljava.lang.String; @ 7c1c8c58 上连续的每一行。

这是延长主要的类是不相关的,该项目的一部分。如果道歉被张贴错了,感谢您的帮助。

整个程序的输出将类似于:


  

树皮,树皮。


  
  

喵,喵。


  
  

Roooaaar。


  
  

狗说汪汪,汪汪。


  
  

猫喵说,喵。


Tester类:

 公共类测试仪{的String [] = animalArray {狗,猫,TREX,牛,猪八戒,白蛇传,
    喜羊羊,猫头鹰,鸡,青蛙};的String [] = noiseArray {汪,汪,喵,喵,Roooaaar,MOOO
    哼,哼,发出嘶嘶声,咩咩咩,叱,叱,博克,博克
    Ribbit的,Ribbit的};的String [] = printArray新的String [10];公共字符串printOut的(){
    而(真){
        对(INT I = 0; I&小于10;我++){            字符串值=(animalArray [I] +云+ noiseArray [I] +。);
            printArray [I] =价值;            的System.out.println();
            的System.out.println(printArray);
            打破;        }
    }
}
}


解决方案

使用<一个href=\"http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Arrays.html#toString%28java.lang.Object%5B%5D%29\"相对=nofollow> Arrays.toString() 打印数组的内容。实际上不打印数组本身。

 的System.out.println(printArray); //输出[Ljava.lang.String; @ 7c1c8c58
的System.out.println(Arrays.toString(printArray0); //输出[狗说汪汪,汪汪...]

如果你使用 Arrays.toString(),打印循环外的数组。
你也可以只打印阵列的每个部分与的System.out.println(printArray [I])内循环。

 公共字符串printOut的(){
    而(真){
        对(INT I = 0; I&小于10;我++){            字符串值=(animalArray [I] +云+ noiseArray [I] +。);
            printArray [I] =价值;
            的System.out.println();
            的System.out.println(printArray [I]); //这工作
            打破;
        }
    }
    的System.out.println(Arrays.toString(printArray); //同样适用
}

This is probably a simple fix, but I am just not seeing it. I am trying to figure out, how do I get my printOut() method to print properly from the main Project5PartA? Do I need get, set, and return methods? Also, is my while loop even necessary in the Tester class?

The program compiles and keeps running to infinity, so I guess the while loop is wrong. But it also only prints out [Ljava.lang.String;@7c1c8c58 continuously on each line.

The classes that extend the main are irrelevant and part of the project. Apologies if this was posted wrong and thanks for any help.

The output of the whole program would be similar to:

Bark, bark.

Meow, meow.

Roooaaar.

Dog says woof, woof.

Cat says meow, meow.

Tester Class:

public class Tester {

String[] animalArray = {"Dog", "Cat", "tRex", "Cow", "Pig", "Snake",
    "Goat", "Owl", "Chicken", "Frog"};

String[] noiseArray = {"Woof, woof", "Meow, meow", "Roooaaar", "Mooo",
    "Oink, oink", "Hissss", "Baaa", "Hoot, hoot", "Bock, bock",
    "Ribbit, ribbit"};

String[] printArray = new String[10];

public String printOut() {
    while (true) {
        for (int i = 0; i < 10; i++) {

            String value = (animalArray[i] + " says " + noiseArray[i] + ".");
            printArray[i] = value;

            System.out.println();
            System.out.println(printArray);
            break;

        }
    }
}
}

解决方案

Use Arrays.toString() to print the contents of an array. Don't actually print the array itself.

System.out.println(printArray); // Prints [Ljava.lang.String;@7c1c8c58 
System.out.println(Arrays.toString(printArray0); // Prints [Dog says Woof, woof...]

If you do use Arrays.toString(), print the array outside the loops. You could also just print each part of the array with System.out.println(printArray[i]) inside the loop.

public String printOut() {
    while (true) {
        for (int i = 0; i < 10; i++) {

            String value = (animalArray[i] + " says " + noiseArray[i] + ".");
            printArray[i] = value;
            System.out.println();
            System.out.println(printArray[i]); // This works
            break;
        }
    }
    System.out.println(Arrays.toString(printArray); // Also works
}

这篇关于印刷阵列错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆