如何从通过一个字符串数组打印输出的名单? [英] How do I printout a list of names from an array through a string?

查看:113
本文介绍了如何从通过一个字符串数组打印输出的名单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在遇到麻烦试图找出如何从一个数组打印输出的名单并肩其他信息片断补充。例如,这是我的性格:

I'm having trouble trying to figure out how to printout a list of names from an array to add alongside other pieces of information. For example this is what I have for a character:

public Character(String fn, String ln, String w, int exp)
{
    firstName = fn;
    lastName = ln;
    weapon = w;
    expGained = exp;
}//end Character

我的toString:

my toString:

public String toString()
{
    String charInformation = "";
    charInformation = "Character Information :" + "\n Name: " + getFirstName() + " " + 
    getLastName() +"\n Weapon Equipped: " + getWeaponName() + "\n EXP Gained: " + getExpGained();
    return charInformation; 
}//end toString

新创建的角色:

Character kaito = new Character("Kaito", "Owari", "Crossbow", 43)

输出看起来是这样的:

The output looks like this:

Name: Kaito Owari
 Weapon: Crossbow
 EXP Gained: 43

我想要做的就是要列出他的战友们每行一个的名字,像这样:

What I want to do is to list the names of his comrades one per line like so:

 Name: Kaito Owari
 Weapon: Crossbow
 EXP Gained: 43
 Comrades:
 (Namehere)~Just first and last names
 (Namehere)
 (Namehere)

我做了我的数组我只是不知道如何让我的toString一一列举如上。我的数组:

I've made my array I just don't know how to get my toString to list them like above. My array:

String firstNames[]={"Max","Owain","Ashe"}
String lastNames[]={"Zero","Matoi","Minami"}

帮助真的AP preciated!

Help is really appreciated!

推荐答案

首先,你不应该定义自己的角色类,因为这是一个核心的Java类为好。编译器会接受它,但它是混乱到其他程序员,而且容易出错。

First of all, you shouldn't define your own Character class, because this is a core java class as well. The compiler will accept it, but it is confusing to other programmers and error-prone.

其次,我不明白为什么你有一个字符串数组的不是字符和。如果你有一个字符数组,并希望利用自己的toString方法,您可以轻松地做到这一点,因为这:

Secondly, I don't understand why you have an array of Strings and not of Characters. If you have an array of Characters and want to utilize your toString method, you can do it as easily as this:

Character[] characters = (...) // initialize
for (Character character : characters)
    System.out.println(character);

这篇关于如何从通过一个字符串数组打印输出的名单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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