import javax.swing.JOptionPane;
import java.io.*;
public class Player {private String playername;
private int playernumber;
private int attend;
private int score;
public Player (String pn,int num, int a, int s)
{
playername=pn;
playernumber= num;
attend=a;
score=s;
}
public String getplayername ()
{
return playername;
}
public int getattend()
{
return attend;
}
public int getscore()
{
return score;
}
public int getplayernumber()
{return playernumber;
}public static void Menu()throws IOException
{
Player []dataArray=new Player[4];
int c=6;
while(c!= 4)
{
String input = JOptionPane.showInputDialog( "1. Enter the player of detail\n" +
"2. Print the detail of player \n" +
"3.data is writen to file\n"+
"4. Quit\n" +
"Enter your choice"); c=Integer.parseInt(input);
switch (c)
{
case 1: Create(dataArray);
break;
case 2: print(dataArray);
break;
case 3: input(dataArray);
break;
case 4: JOptionPane.showMessageDialog(null, "Bye ");
break;
default: JOptionPane.showMessageDialog(null, "Invalid choice");
break;
}
}
}
public static void Create(Player[] dataArray)throws IOException {
for(int counter=1;counter<dataArray.length;counter++)
{
String pn=JOptionPane.showInputDialog("Please enter the playername of "+counter);
String ck=JOptionPane.showInputDialog("Enter the numebr of player of "+counter);
int num=Integer.parseInt(ck);
String d=JOptionPane.showInputDialog("Enter the attend of player of "+counter);
int a =Integer.parseInt(d);
String q=JOptionPane.showInputDialog("Enter the getting score of player of "+counter);
int s=Integer.parseInt(q);
dataArray[counter]=new Player(pn,num,a,s);
}
} public static void input(Player[] dataArray)throws IOException
{
final FileWriter outputFile = new FileWriter("kiss.txt");
final BufferedWriter output= new BufferedWriter(outputFile);
final PrintWriter printstream = new PrintWriter(output);
for (int i=0; i<dataArray.length; i++)
{
printstream.println(dataArray[i].playername);
printstream.println(dataArray[i].playernumber);
printstream.println(dataArray[i].attend);
printstream.println(dataArray[i].score);
}
printstream.close();
System.out.println("Details are written to file");
}
public static void main(String[] args)throws IOException
{
Menu();
}
}为什么我的输入的数据``存不进文本kiss.txt里面```帮我看看问题```谢谢````
import java.io.*;
public class Player {private String playername;
private int playernumber;
private int attend;
private int score;
public Player (String pn,int num, int a, int s)
{
playername=pn;
playernumber= num;
attend=a;
score=s;
}
public String getplayername ()
{
return playername;
}
public int getattend()
{
return attend;
}
public int getscore()
{
return score;
}
public int getplayernumber()
{return playernumber;
}public static void Menu()throws IOException
{
Player []dataArray=new Player[4];
int c=6;
while(c!= 4)
{
String input = JOptionPane.showInputDialog( "1. Enter the player of detail\n" +
"2. Print the detail of player \n" +
"3.data is writen to file\n"+
"4. Quit\n" +
"Enter your choice"); c=Integer.parseInt(input);
switch (c)
{
case 1: Create(dataArray);
break;
case 2: print(dataArray);
break;
case 3: input(dataArray);
break;
case 4: JOptionPane.showMessageDialog(null, "Bye ");
break;
default: JOptionPane.showMessageDialog(null, "Invalid choice");
break;
}
}
}
public static void Create(Player[] dataArray)throws IOException {
for(int counter=1;counter<dataArray.length;counter++)
{
String pn=JOptionPane.showInputDialog("Please enter the playername of "+counter);
String ck=JOptionPane.showInputDialog("Enter the numebr of player of "+counter);
int num=Integer.parseInt(ck);
String d=JOptionPane.showInputDialog("Enter the attend of player of "+counter);
int a =Integer.parseInt(d);
String q=JOptionPane.showInputDialog("Enter the getting score of player of "+counter);
int s=Integer.parseInt(q);
dataArray[counter]=new Player(pn,num,a,s);
}
} public static void input(Player[] dataArray)throws IOException
{
final FileWriter outputFile = new FileWriter("kiss.txt");
final BufferedWriter output= new BufferedWriter(outputFile);
final PrintWriter printstream = new PrintWriter(output);
for (int i=0; i<dataArray.length; i++)
{
printstream.println(dataArray[i].playername);
printstream.println(dataArray[i].playernumber);
printstream.println(dataArray[i].attend);
printstream.println(dataArray[i].score);
}
printstream.close();
System.out.println("Details are written to file");
}
public static void main(String[] args)throws IOException
{
Menu();
}
}为什么我的输入的数据``存不进文本kiss.txt里面```帮我看看问题```谢谢````
楼主【handsome2t】截止到2008-07-12 00:38:36的历史汇总数据(不包括此帖):
发帖的总数量:2 发帖的总分数:50 每贴平均分数:25
回帖的总数量:2 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:0 结贴的总分数:0
无满意结贴数:0 无满意结贴分:0
未结的帖子数:2 未结的总分数:50
结贴的百分比:0.00 % 结分的百分比:0.00 %
无满意结贴率:---------------------无满意结分率:---------------------
如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
for (int i=0; i <dataArray.length; i++)
{
printstream.println(dataArray[i].playername);
printstream.println(dataArray[i].playernumber);
printstream.println(dataArray[i].attend);
printstream.println(dataArray[i].score);
} 你的变量的名字起的 真是霸道 看了半天以为你用PrintStream 但原来是PrintWriter恶
input的时候i从0开始,dateArray[0].playername等属性肯定报错了。
把Create方法的 for(int counter=1;counter <dataArray.length;counter++)
改成 for(int counter=0;counter <dataArray.length;counter++)
FileOutputStream fos=new FileOutputStream("kiss.txt");
OutputStreamWriter osw=new OutputStreamWriter(fos);
BufferedWriter bw=new BufferedWriter(osw);
for (int i=0; i <dataArray.length; i++)
{
bw.println(dataArray[i].playername);
bw.println(dataArray[i].playernumber);
bw.println(dataArray[i].attend);
bw.println(dataArray[i].score);
}
System.out.println("");
这样试试