?? listdisplay.java.bak
字號:
import java.io.*;
class Employee
{
static int salary[]=new int[10];
public void accept_details() throws IOException
{
try
{
System.out.println("\n\n\n");
System.out.println("+++++++++++ BEGIN +++++++++++");
System.out.println("Check the salary please :\n");
FileInputStream fis=new FileInputStream("data.txt");
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(isr);
String s=br.readLine();
String[] str1=s.split("\\ ");
for(int i=0;i<10;i++)
{
salary[i]=Integer.parseInt(str1[i]);
System.out.print(salary[i]+" ");
if(i==4)System.out.println("");
}
System.out.println("\n");
}
catch(IOException e)
{
System.out.println("Read Error!");
}
}
public void display_max() throws IOException
{
try
{
int i=1;
int temp;
temp=salary[0];
while(i<10)
{
if(salary[i]>temp)
temp=salary[i];
i++;
}
FileWriter outF = new FileWriter("result.txt",true); //true的作用
System.out.println("The max salary is : "+temp);
outF.write("The max salary is : "+temp+"\r\n");
outF.write("-------------------------------------------------------------------\r\n");
outF.close();
}
catch(IOException e)
{
System.out.println("display_max Error!");
}
}
public void display_min() throws IOException
{
try
{
int i=1;
int temp;
temp=salary[0];
while ( i < 10 )
{
if (salary[i]<temp)temp=salary[i];i++;
}
FileWriter outF = new FileWriter("result.txt",true);
System.out.println("The min salary is : "+temp);
outF.write("The min salary is : "+temp+"\r\n");
outF.write("-------------------------------------------------------------------\r\n");
outF.close();
}
catch(IOException e)
{
System.out.println("display_min() Error!");
}
}
public void display_avg() throws IOException
{
try
{
int i;
int sum;
int avg;
sum=0;
avg=0;
for(i=0;i<=9;i++)sum+=salary[i];
avg=sum/10;
FileWriter outF=new FileWriter("result.txt",true);
System.out.println("Average : "+ avg);
outF.write("Average : "+avg+"\r\n");
outF.write("-------------------------------------------------------------------\r\n");
outF.close();
}
catch(IOException e)
{
System.out.println("display_avg() Error!");
}
}
public void count() throws IOException
{
try
{
int i,j;
i=0;j=0;
while(i<=9)
{
if(salary[i]>1000)++j;++i;
}
FileWriter outF=new FileWriter("result.txt",true);
System.out.println("salary > 1000: "+j+"\n");
outF.write("salary > 1000: "+j+"\r\n");
outF.write("-------------------------------------------------------------------\r\n");
outF.close();
}
catch(IOException e)
{
System.out.print("count() Error!");
}
}
public void sequence() throws IOException
{
try
{
int i,j;
int temp;
i=0;
while (i<9)
{
if(salary[i]>salary[i+1])
{
temp=salary[i];
salary[i]=salary[i+1];
salary[i+1]=temp;i=0;
continue;
}
i++;
}
j=0;
FileWriter outF=new FileWriter("result.txt",true);
System.out.println("min->max: ");
outF.write("min->max: ");
do
{
System.out.print(salary[j]);
outF.write(salary[j]+" ");
//修改outF.write(salary[j]);由于沒有空開,所以把數字與'<'癡埋一齊,造成亂碼!
//其他噶全部改為"\r\n"了,txt顯示無問題了!
if(j!=9) System.out.print(" < ");
if(j!=9) outF.write("<");
++j;
}while (j<=9);
System.out.println("\n\n");
outF.write("\r\n");
System.out.println( "max->min: ");
outF.write("max->min: ");
j=9;
do
{
System.out.print(salary[j]);
outF.write(salary[j]+" ");
if(j!=0) System.out.print(" > ");
if(j!=0) outF.write(">");
--j;
}while(j>=0);
System.out.println("\n\n\n");
outF.write("\r\n");
outF.write("-------------------------------------------------------------------\r\n");
outF.close();
}
catch(IOException e)
{
System.out.println("sequence Error!");
}
}
}//結束類的定義
class NewEmployee extends Employee
{
int s[]=new int[5];
public void section() throws IOException
{
try
{
int i,k;
for(k=0;k<=4;k++)s[k]=0;
for(i=0;i<= 9;i++)
{
if(salary[i]<=1000)s[0]++;
else if(salary[i]<=2000)s[1]++;
else if(salary[i]<=3000)s[2]++;
else if(salary[i]<=4000)s[3]++;
else s[4]++;
}
System.out.println("salary <= 1000 : "+s[0]);
System.out.println("1000 < salary <= 2000 : "+s[1]);
System.out.println("2000 < salary <= 3000 : "+s[2]);
System.out.println("3000 < salary <= 4000 : "+s[3]);
System.out.println("4000 < salary : "+s[4]);
FileWriter outF=new FileWriter("result.txt",true);
outF.write("show section : \r\n");
outF.write("salary <= 1000 : "+ s[0]+"\r\n");
outF.write("1000 < salary <= 2000 : "+s[1]+"\r\n");
outF.write("2000 < salary <= 3000 : "+s[2]+"\r\n");
outF.write("3000 < salary <= 4000 : "+s[3]+"\r\n");
outF.write("4000 < salary : "+s[4]+"\r\n");
outF.write("-------------------------------------------------------------------\r\n");
outF.close();
}
catch(IOException e)
{
System.out.println("section() Error!");
}
}
}
public class ListDisplay
{
public static void main(String args[]) throws IOException
{
try
{
NewEmployee e1=new NewEmployee();
e1.accept_details();
loop1:
while(true)
{
System.out.println("\n\n\n");
System.out.println("============Menu=============");
System.out.println("| 1.display max salary |");
System.out.println("| 2.display min salary |");
System.out.println("| 3.display average salary |");
System.out.println("| 4.display salary > 1000 |");
System.out.println("| 5.display sequence |");
System.out.println("| 6.display section |");
System.out.println("| 0.exit |");
System.out.println("-----------------------------\n");
System.out.print("Please choose a number(0-5):");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String n=br.readLine();
int m=Integer.parseInt(n);
loop2:
switch(m) //強制轉換類型
{
case 1:e1.display_max();break loop2; //調用每個函數之前最好清屏
case 2:e1.display_min();break loop2;
case 3:e1.display_avg();break loop2;
case 4:e1.count();break loop2;
case 5:e1.sequence();break loop2;
case 6:e1.section();break loop2;
default:break loop2;
}
if(m==0) break loop1;
}
}
catch(IOException e)
{
System.out.println("Error!");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -