重要なお知らせ

「教えて! goo」は2025年9月17日(水)をもちまして、サービスを終了いたします。詳細はこちら>

電子書籍の厳選無料作品が豊富!

System.out.println("----------------");
System.out.println("NAME:"+player[0].name);
System.out.println("HP:"+player[0].hp);
System.out.println("MP:"+player[0].mp);
System.out.println("----------------");
System.out.println("NAME:"+player[1].name);
System.out.println("HP:"+player[1].hp);
System.out.println("MP:"+player[1].mp);
System.out.println("----------------");
System.out.println("NAME:"+player[2].name);
System.out.println("HP:"+player[2].hp);
System.out.println("MP:"+player[2].mp);
System.out.println("----------------");
これを、配列を使って効率の良いプログラムを作りたいのですがどうすればいいのでしょうか?

A 回答 (4件)

コレでどうでしょう。



System.out.println("----------------");
for(int i=0;i<3;i++){
System.out.println("NAME:"+player[i].name);
System.out.println("HP:"+player[i].hp);
System.out.println("MP:"+player[i].mp);
System.out.println("----------------");
}
    • good
    • 0

どのようにって、#1で回答したそのままですよ。



import java.io.*;
public class Ex081
{
public static void main (String[]args)throws IOException
{
String name;
int hp;
int mp;
BufferedReader br = new BufferedReader
(new InputStreamReader(System.in));
CharData player[] = new CharData[3];
for(int i=0; i<3; i++)
{
player[i] = new CharData("",0,0);
}
for(int i=0; i<3; i++)
{
System.out.println("NAMEの入力");
name = br.readLine();
System.out.println("HPの入力");
hp = Integer.parseInt(br.readLine());
System.out.println("MPの入力");
mp = Integer.parseInt(br.readLine());
player[i] = new CharData(name,hp,mp);
}
System.out.println("----------------");
for(int i=0; i<player.length; i++)
{
print(player[i]
}
}
}

public static void print(CharData player) {
System.out.println("NAME:"+player.name);
System.out.println("HP:"+player.hp);
System.out.println("MP:"+player.mp);
System.out.println("----------------");
}
    • good
    • 0

>全文を書くと



全文じゃないですよね?
変数playerのフィールドname, hp, mpは
どこで宣言してるんですか?
    • good
    • 0

これだけじゃ情報が少なすぎですが、


メソッドを作って

for(int i=0; i<player.length; i++) {
print(player[i]);
}

public void print(Player player) {
System.out.println("NAME:"+player.name);
System.out.println("HP:"+player.hp);
System.out.println("MP:"+player.mp);
System.out.println("----------------");
}

でどうでしょう。

この回答への補足

全文を書くと
import java.io.*;
public class Ex081
{
public static void main (String[]args)throws IOException
{
String name;
int hp;
int mp;
BufferedReader br = new BufferedReader
 (new InputStreamReader(System.in));
CharData player[] = new CharData[3];
 for(int i=0; i<3; i++)
{
player[i] = new CharData("",0,0);
}
for(int i=0; i<3; i++)
{
System.out.println("NAMEの入力");
name = br.readLine();
System.out.println("HPの入力");
hp = Integer.parseInt(br.readLine());
System.out.println("MPの入力");
mp = Integer.parseInt(br.readLine());
player[i] = new CharData(name,hp,mp);
}
System.out.println("----------------");
System.out.println("NAME:"+player[0].name);
System.out.println("HP:"+player[0].hp);
System.out.println("MP:"+player[0].mp);
System.out.println("----------------");
System.out.println("NAME:"+player[1].name);
System.out.println("HP:"+player[1].hp);
System.out.println("MP:"+player[1].mp);
System.out.println("----------------");
System.out.println("NAME:"+player[2].name);
System.out.println("HP:"+player[2].hp);
System.out.println("MP:"+player[2].mp);
System.out.println("----------------");
}
}
です。出力はどのように配列でやればいいのでしょうか?

補足日時:2006/06/07 20:20
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!