Java 使用JOptionPane打印报告
好的,现在我得到了要编译和运行的代码,但是现在输出是不正确的。我需要能够选择一个选项,然后为2,3有3个额外的选项后,选择该选项。我应该如何调整我的编码来做到这一点 任务:Java 使用JOptionPane打印报告,java,arrays,report,joptionpane,dialog,Java,Arrays,Report,Joptionpane,Dialog,好的,现在我得到了要编译和运行的代码,但是现在输出是不正确的。我需要能够选择一个选项,然后为2,3有3个额外的选项后,选择该选项。我应该如何调整我的编码来做到这一点 任务: 所有信息列表 特定医生的所有手术列表(提示医生) 特定类型的所有手术列表(提示输入手术类型) 支付给每位医生的手术费总额 平均费用 数据文件(patient.txt): 迄今为止的代码供参考: package Patient_Reports_Package; /** * Created by bzink on 8/28/
package Patient_Reports_Package;
/**
* Created by bzink on 8/28/2015.
*/
import javax.swing.*;
import java.io.*;
import java.util.StringTokenizer;
/**
* The Patient_Reports_File class reads the data file into an array, and then has a menu for 5 reports.
*/
class Patient_Reports {
private final int[] id = new int[100];
private final String[] patient = new String[100];
private final String[] doctor = new String[100];
private final String[] surgery = new String[100];
private final double[] cost = new double[100];
private int count = -1;
private int i;
public static void main (String[] args) {
int selection;
String report_number;
Patient_Reports patient = new Patient_Reports();
patient.start_system();
report_number = patient.menu();
selection = Integer.parseInt(report_number);
while (selection !=6) {
if (selection == 1) {
patient.allInformationReport();
} else if (selection == 2) {
patient.surgeryDoctorReport();
} else if (selection == 3) {
patient.surgeryTypeReport();
} else if (selection == 4) {
patient.doctorFeesReport();
} else if (selection == 5) {
patient.averageFeesReport();
}
report_number = patient.menu();
selection = Integer.parseInt(report_number);
}//while loop
patient.writeReports();
System.exit(0);
}//main
//Read Data File into Array
private void start_system() {
String newLine;
try {
//define a file variable for Buffered read
BufferedReader Patient_Reports = new BufferedReader(new java.io.FileReader("C:\\Users\\Brandon\\" +
"Downloads\\Patient_Reports_File\\patient.txt"));
//read lines in file until there are no more lines in the file to read
while ((newLine = Patient_Reports.readLine()) != null) {
//there is a "," between each data item in each line
StringTokenizer delimiter = new StringTokenizer(newLine, ",");
count = count + 1;
id[count] = Integer.parseInt(delimiter.nextToken());
patient[count] = delimiter.nextToken();
doctor[count] = delimiter.nextToken();
surgery[count] = delimiter.nextToken();
cost[count] = Double.parseDouble(delimiter.nextToken());
}//while loop
Patient_Reports.close();
}//end try
catch (IOException error) {
//there was an error on the file writing
System.out.println("Error on file read " + error);
}//end catch
}//end start_system
//Report Menu
private String menu () {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Id ").append(" \n");
stringBuilder.append("Patient ").append(" \n");
stringBuilder.append("Doctor ").append(" \n");
stringBuilder.append("Surgery ").append(" \n");
stringBuilder.append("Cost ").append(" \n");
for (int i = 0; i < 6; i++) {
stringBuilder.append(i).append(" Name"+i).append('\n');
}
String startTag ="<font size='2' color='red'>";
String endTag = "</font>";
stringBuilder.append(startTag).append("Some content").append(endTag);
JOptionPane.showMessageDialog(null, stringBuilder.toString());
return stringBuilder.toString();
}//end menu\
/*
//Report Menu
private String menu() {
String report;
String Output = "Reports" + "\n" + "1. All_Information_Report" + "\n" +
"2. Surgeries_Doctor_Report" + "\n" +
"3. Surgeries_Type_Report" + "\n" +
"4. Doctor_Fees_Report" + "\n" +
"5. Average_Fees_Report" + "\n" +
"6. Exit" + "\n" +
" " + "\n" +
"Select a Report >";
report = JOptionPane.showInputDialog(null,
Output, "", JOptionPane.QUESTION_MESSAGE);
return report;
}//end menu\
*/
//Report containing all of the information
private void allInformationReport() {
System.out.println("All Information Report");
for (i = 0; i <= count; ++i) {
System.out.println(id[i] + " " + patient[i] + " " + doctor[i] + " " + surgery[i] + " " + cost[i] + " ");
}//for loop
}//end report
/* void selectDoctor()
{
//select doctor
String doctorOutput;
//int intNum=0,intNum1=0,i,x=-1;
count=count+1;
doctorOutput = "Enter the Doctor's Name";
doctor[count] =JOptionPane.showInputDialog(null,doctorOutput,
"",JOptionPane.QUESTION_MESSAGE);
}//end select doctor
//Start Doctor Menu
public static void doctorMenu (String[] args) {
int selection;
String doctorName;
Patient_Reports doctor = new Patient_Reports();
doctor.start_system();
doctorName = doctorMenu();
selection = Integer.parseInt(doctorName);
while (selection !=4) {
if (selection == 1) {
doctor.norrisSurgeries();
} else if (selection == 2) {
doctor.bondSurgeries();
} else if (selection == 3) {
doctor.leeSurgeries();
}
doctorName = doctorMenu();
selection = Integer.parseInt(doctorName);
}//while loop
doctor.writeReports();
System.exit(0);
}//End Doctor Menu
//Report on all surgeries by Dr. Norris
private void norrisSurgeries() {
System.out.println("Norris Surgeries Report");
for (i = 0; i <= count; ++i) {
System.out.println(doctor[i] + " " + surgery[i] + " ");
}//for loop
}//end report
//Report on all surgeries by Dr. Bond
private void bondSurgeries() {
System.out.println("Bond Surgeries Report");
for (i = 0; i <= count; ++i) {
System.out.println(doctor[i] + " " + surgery[i] + " ");
}//for loop
}//end report
//Report on all surgeries by Dr. Lee
private void leeSurgeries() {
System.out.println("Lee Surgeries Report");
for (i = 0; i <= count; ++i) {
System.out.println(doctor[i] + " " + surgery[i] + " ");
}//for loop
}//end report
*/
//Report on all surgeries of a specific doctor (prompt for the doctor)
private void surgeryDoctorReport() {
System.out.println("Surgeries Doctor Report");
for (i = 0; i <= count; ++i) {
System.out.println(id[i] + " " + patient[i] + " " + doctor[i] + " " + surgery[i] + " " + cost[i] + " ");
}//for loop
}//end report
/*
void selectSurgery()
{
//select surgery
String surgeryOutput;
//int intNum=0,intNum1=0,i,x=-1;
count=count+1;
surgeryOutput = "Enter the Surgery Type";
doctor[count] =JOptionPane.showInputDialog(null,surgeryOutput,
"",JOptionPane.QUESTION_MESSAGE);
}//end select surgery
*/
//Report on all surgeries of a specific type(Prompt for the surgery type)
private void surgeryTypeReport() {
System.out.println("Surgeries Type Report");
for (i = 0; i <= count; ++i) {
System.out.println(id[i] + " " + patient[i] + " " + doctor[i] + " " + surgery[i] + " " + cost[i] + " ");
}//for loop
}//end report
//Report on the total amount of fees paid to each doctor
private void doctorFeesReport() {
System.out.println("Doctor Fees Report");
for (i = 0; i <= count; ++i) {
System.out.println(id[i] + " " + patient[i] + " " + doctor[i] + " " + surgery[i] + " " + cost[i] + " ");
}//for loop
}//end report
//Report on the Average Fee
private void averageFeesReport() {
System.out.println("Average Fees Report");
for (i = 0; i <= count; ++i) {
System.out.println(id[i] + " " + patient[i] + " " + doctor[i] + " " + surgery[i] + " " + cost[i] + " ");
}//for loop
}//end report
//Store Data File in Array
private void writeReports()
{
try {
BufferedWriter Patient_Reports = new BufferedWriter(new java.io.FileWriter("patient_out.txt"));
for (i = 0; i <= count; ++i) {
//put "," between each data item in the file
Patient_Reports.write(id[i] + "," + patient[i] + "," + doctor[i] + "," +
surgery[i] + "," + cost[i] + ",");
//write a new line in the file
Patient_Reports.newLine();
}//for loop
Patient_Reports.close();
}//end try
catch (IOException error) {
//there was an error on the write to file
System.out.println("Error on file write " + error);
}//end error
}//end write_reports
}
'
package Patient\u Reports\u package;
/**
*bzink于2015年8月28日创建。
*/
导入javax.swing.*;
导入java.io.*;
导入java.util.StringTokenizer;
/**
*Patient_Reports_File类将数据文件读取到一个数组中,然后有一个包含5个报告的菜单。
*/
分类病人报告{
私有最终整数[]id=新整数[100];
私有最终字符串[]患者=新字符串[100];
私有最终字符串[]医生=新字符串[100];
私有最终字符串[]外科手术=新字符串[100];
私人最终双倍[]成本=新双倍[100];
私有整数计数=-1;
私人互联网i;
公共静态void main(字符串[]args){
int选择;
字符串报告编号;
患者报告患者=新患者报告();
病人。启动系统();
报告编号=patient.menu();
selection=Integer.parseInt(报告编号);
while(选择!=6){
如果(选择==1){
患者。所有信息报告();
}else if(选择==2){
患者。外科医生报告();
}else if(选择==3){
患者。手术类型报告();
}else if(选择==4){
病人。医生报告();
}else if(选择==5){
患者平均感觉报告();
}
报告编号=patient.menu();
selection=Integer.parseInt(报告编号);
}//while循环
patient.writeReports();
系统出口(0);
}//主要
//将数据文件读入数组
专用无效启动系统(){
字符串换行符;
试一试{
//定义缓冲读取的文件变量
BufferedReader Patient_Reports=新的BufferedReader(新的java.io.FileReader(“C:\\Users\\Brandon\”)+
“下载\\Patient\u报告\\Patient.txt”);
//读取文件中的行,直到文件中不再有行可读取为止
while((newLine=Patient_Reports.readLine())!=null){
//每行中的每个数据项之间都有一个“,”
StringTokenizer分隔符=新的StringTokenizer(换行符“,”);
计数=计数+1;
id[count]=Integer.parseInt(delimiter.nextToken());
患者[计数]=分隔符.nextToken();
doctor[count]=分隔符.nextToken();
手术[计数]=分隔符.nextToken();
cost[count]=Double.parseDouble(delimiter.nextToken());
}//while循环
患者报告。关闭();
}//结束尝试
捕获(IOException错误){
//文件写入时出错
System.out.println(“文件读取错误”+错误);
}//端接
}//结束-启动系统
//报告菜单
私有字符串菜单(){
StringBuilder StringBuilder=新的StringBuilder();
stringBuilder.append(“Id”).append(“\n”);
stringBuilder.append(“患者”).append(\n”);
stringBuilder.append(“医生”).append(\n”);
stringBuilder.append(“手术”).append(“\n”);
stringBuilder.append(“成本”).append(“\n”);
对于(int i=0;i<6;i++){
stringBuilder.append(i).append(“名称”+i.append('\n');
}
字符串startTag=“”;
字符串endTag=“”;
追加(startTag).append(“某些内容”).append(endTag);
showMessageDialog(null,stringBuilder.toString());
返回stringBuilder.toString();
}//结束菜单\
/*
//报告菜单
私有字符串菜单(){
字符串报告;
字符串输出=“报告”+“\n”+“1.所有信息报告”+“\n”+
“2.手术\医生\报告”+“\n”+
“3.手术类型报告”+“\n”+
“4.医生费用报告”+“\n”+
“5.平均费用报告”+“\n”+
6.退出“+”\n+
“+”\n”+
“选择报告>”;
report=JOptionPane.showInputDialog(null,
输出“”,作业窗格。问题消息);
返回报告;
}//结束菜单\
*/
//包含所有信息的报告
私人作废所有信息报告(){
System.out.println(“所有信息报告”);
对于(i=0;i使用String
或StringBuilder
或StringBuffer
来执行此操作。但是不要使用StringBuilder
或StringBuffer
。因为对于String
,您需要一些额外的对象来进行String
操作
Ex:
StringBuilder sb = new StringBuilder();
sb.append("Id ").append(" Name\n");
for (int i = 0; i < 10; i++) {
sb.append(i).append(" Name"+i).append('\n');
}
JOptionPane.showMessageDialog(null, sb.toString());
String startTag ="<font size='2' color='red'>";
String endTag = "</font>";
sb.append(startTag+"Some content"+endTag);
使用String
或StringBuilder
或StringBuffer
来执行此操作。但是不要使用StringBuilder
或StringBuffer
。因为对于String
,您需要一些额外的对象来进行String
操作
Ex:
StringBuilder sb = new StringBuilder();
sb.append("Id ").append(" Name\n");
for (int i = 0; i < 10; i++) {
sb.append(i).append(" Name"+i).append('\n');
}
JOptionPane.showMessageDialog(null, sb.toString());
String startTag ="<font size='2' color='red'>";
String endTag = "</font>";
sb.append(startTag+"Some content"+endTag);
为什么你不能用JTextArea
和JScrollPane
来代替JOptionpane
来使用其他组件,比如JTextArea
和JScrollPane
来代替JOptionpane
。那么我应该把它添加到我当前的字符串方法中,还是用它来代替呢?编辑:我有点生气ty是oop的新手。在我大学毕业之前,我一直在努力提高我的技能。我的建议是,使用StringBuilder
而不是String
。私有字符串菜单(){放在这里?是的,我是说用StingBuilder
来代替它。现在开始实施。我会加上你的答案