课上测试-地铁售票系统
文件内容:
交河大道 南村 石家庄东站 火炬广场 留村 白佛 朝晖桥 谈固 北宋 体育馆 博物院 北国商城 平安大街 解放广场 新百广场 烈士陵园 和平医院 长城桥 时光街 西王
市二中 新百广场 东里 槐安桥 西三教 石家庄站
每3站一元,不够的按1元算
代码如下:
package shangke.disizhou; import java.io.File; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.util.Arrays; import java.util.List; import java.util.Scanner; import java.util.StringTokenizer; import java.io.FileWriter; public class Subway { private int num01=1; private int num03=3; private String ditie01[][]=new String[2][100]; public static int m[]=new int[2]; public static void main(String[] args) { Subway sta=new Subway(); sta.getxinxi(); Scanner shuru01=new Scanner(System.in); int n; System.out.println("***************************************************"); System.out.println(" 制作人姓名:苗可卓 学号:20183629"); for(;;) { System.out.println("***************************************************"); System.out.println(" 欢迎乘坐石家庄地铁"); System.out.println("***************************************************"); System.out.println(" 1、显示各线路信息"); System.out.println(" 2、查询站点信息"); System.out.println(" 3、购票"); System.out.println(" 4、退出"); System.out.print(" 请输入"); for(;;) { n=shuru01.nextInt(); if(n>=1&&n<=4) { break; } else { System.out.print(" 输入错误请重新输入"); } } if(n==4) { System.out.println(" 谢谢使用"); } switch(n) { case 1:sta.bianli();break; case 2:sta.chaxun();break; case 3:sta.goupiao();break; } } } public void bianli() { int x; m[0]=19; m[1]=6; Scanner shuru02 = new Scanner(System.in); System.out.println("**********************************************"); System.out.println(" 0、输出全部线路"); System.out.println(" 1、输出1号线"); System.out.println(" 3、输出3号线"); x=shuru02.nextInt(); switch(x) { case 0: { for(int a=0;a<2;a++) { if(a==0) { System.out.println("1号线"); } if(a==1) { System.out.println("3号线"); } for(int b=0;b<m[a];b++) { System.out.print(ditie01[a][b]+" "); } System.out.println(); } break; } case 1: { System.out.println("1号线"); for(int a=0;a<m[0];a++) { System.out.print(ditie01[0][a]+" "); } System.out.println(); break; } case 3: { System.out.println("3号线"); for(int a=0;a<m[1];a++) { System.out.print(ditie01[1][a]+" "); } System.out.println(); break; } } } public void getxinxi() { int i=0,j; try { // 防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw /* 读入TXT文件 */ String pathname = "H:\\wenben.txt"; // 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径 File filename = new File(pathname); // 要读取以上路径的input。txt文件 InputStreamReader reader = new InputStreamReader( new FileInputStream(filename)); // 建立一个输入流对象reader BufferedReader br = new BufferedReader(reader); // 建立一个对象,它把文件内容转成计算机能读懂的语言 String line = ""; line = br.readLine(); while (line != null) { StringTokenizer st2 = new StringTokenizer(line, " "); j=0; while (st2.hasMoreElements()) { ditie01[i][j]=String.valueOf(st2.nextElement()); ditie01[i][j]=ditie01[i][j].toLowerCase(); j++; } line = br.readLine(); // 一次读入一行数据 i++; } } catch (Exception e) { e.printStackTrace(); } } public void chaxun() { Scanner shuru02=new Scanner(System.in); String name1,name2; int sum=0; int flag0=0; int flag1=0; int flag2=0; int flag3=0; int flag4=0; int xiabiao1=0; int xiabiao2=0; int xiabiao3=0; int xiabiao4=0; System.out.println("*********************************************"); System.out.println(" 请输入您所查询的起始站和终点站"); name1=shuru02.next(); name2=shuru02.next(); for(int a=0;a<19;a++) { //System.out.println("1111111111111111111111111111111"); if(ditie01[0][a].contentEquals(name1)) { //System.out.println("*********"); flag1=1; xiabiao1=a; } if(name2.contentEquals(ditie01[0][a])) { flag2=1; xiabiao2=a; } } if(xiabiao1==14&&flag2==1) ; else { for(int a=0;a<6;a++) { if(name1.contentEquals(ditie01[1][a])) { //System.out.println("2222222222222222222222"); flag3=1; xiabiao3=a; } if(name2.contentEquals(ditie01[1][a])) { flag4=1; xiabiao4=a; } } } if(flag1==1&&flag2==1||flag3==1&&flag4==1) { flag0=1; } if(flag0==1) { if(flag1==1&&flag2==1) { if(xiabiao1<xiabiao2) { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(xiabiao2-xiabiao1+1+"站"); for(int a=xiabiao1;a<=xiabiao2;a++) { System.out.println(ditie01[0][a]); } } else { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(xiabiao1-xiabiao2+1+"站"); for(int a=xiabiao1;a>=xiabiao2;a--) { System.out.println(ditie01[0][a]); } } } else if(flag3==1&&flag4==1) { if(xiabiao3<xiabiao4) { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(xiabiao4-xiabiao3+1+"站"); for(int a=xiabiao3;a<=xiabiao4;a++) { System.out.println(ditie01[1][a]); } } else { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(xiabiao3-xiabiao4+1+"站"); for(int a=xiabiao3;a>=xiabiao4;a--) { System.out.println(ditie01[1][a]); } } } } else { if(flag1==1&&flag4==1) { if(xiabiao1>=14&&xiabiao4>=1) { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(xiabiao1-14+1+xiabiao4-1+"站"); for(int a=xiabiao1;a>14;a--) { System.out.println(ditie01[0][a]); } System.out.println(ditie01[0][14]+"换乘3号线"); for(int a=1+1;a<=xiabiao4;a++) { System.out.println(ditie01[1][a]); } } else if(xiabiao1>=14&&xiabiao4<=1) { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(xiabiao1-14+1+1-xiabiao4+"站"); for(int a=xiabiao1;a>14;a--) { System.out.println(ditie01[0][a]); } System.out.println(ditie01[0][14]+"换乘3号线"); for(int a=1-1;a>=xiabiao4;a--) { System.out.println(ditie01[1][a]); } } else if(xiabiao1<=14&&xiabiao4>=1) { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(14-xiabiao1+1+xiabiao4-1+"站"); for(int a=xiabiao1;a<14;a++) { System.out.println(ditie01[0][a]); } System.out.println(ditie01[0][14]+"换乘3号线"); for(int a=1+1;a<=xiabiao4;a++) { System.out.println(ditie01[1][a]); } } else if(xiabiao1<=14&&xiabiao4<=1) { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(14-xiabiao1+1+1-xiabiao4+"站"); for(int a=xiabiao1;a<14;a++) { System.out.println(ditie01[0][a]); } System.out.println(ditie01[0][14]+"换乘3号线"); for(int a=1-1;a>=xiabiao4;a--) { System.out.println(ditie01[1][a]); } } } else if(flag2==1&&flag3==1) { if(xiabiao2>=14&&xiabiao3>=1) { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(xiabiao2-14+1+xiabiao3-1+"站"); for(int a=xiabiao3;a>1;a--) { System.out.println(ditie01[1][a]); } System.out.println(ditie01[1][1]+"换乘1号线"); for(int a=14+1;a<=xiabiao2;a++) { System.out.println(ditie01[0][a]); } } else if(xiabiao2>=14&&xiabiao3<1) { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(xiabiao2-14+1+1-xiabiao3+"站"); for(int a=xiabiao3;a<1;a++) { System.out.println(ditie01[1][a]); } System.out.println(ditie01[1][1]+"换乘1号线"); for(int a=14+1;a<=xiabiao2;a++) { System.out.println(ditie01[0][a]); } } else if(xiabiao2<14&&xiabiao3>=1) { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(14-xiabiao2+1+xiabiao3-1+"站"); for(int a=xiabiao3;a>1;a--) { System.out.println(ditie01[1][a]); } System.out.println(ditie01[1][1]+"换乘1号线"); for(int a=14-1;a>=xiabiao2;a--) { System.out.println(ditie01[0][a]); } } else if(xiabiao2<14&&xiabiao3<1) { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(14-xiabiao2+1+1-xiabiao3+"站"); for(int a=xiabiao3;a<1;a++) { System.out.println(ditie01[1][a]); } System.out.println(ditie01[1][1]+"换乘1号线"); for(int a=14-1;a>=xiabiao2;a--) { System.out.println(ditie01[0][a]); } } } } } public void goupiao() { Scanner shuru03=new Scanner(System.in); int sum=0; int aaa=0; int flag0=0; int flag1=0; int flag2=0; int flag3=0; int flag4=0; int xiabiao1=0; int xiabiao2=0; int xiabiao3=0; int xiabiao4=0; System.out.println("***************************************************"); System.out.println(" 欢迎使用石家庄地铁"); System.out.println("***************************************************"); System.out.println(" 请输入所购票的起始站和终点站"); String name1,name2; name1=shuru03.next(); name2=shuru03.next(); System.out.println(" 请输入您所购数量"); aaa=shuru03.nextInt(); for(int a=0;a<19;a++) { //System.out.println("1111111111111111111111111111111"); if(ditie01[0][a].contentEquals(name1)) { //System.out.println("*********"); flag1=1; xiabiao1=a; } if(name2.contentEquals(ditie01[0][a])) { flag2=1; xiabiao2=a; } } if(xiabiao1==14&&flag2==1) ; else { for(int a=0;a<6;a++) { if(name1.contentEquals(ditie01[1][a])) { //System.out.println("2222222222222222222222"); flag3=1; xiabiao3=a; } if(name2.contentEquals(ditie01[1][a])) { flag4=1; xiabiao4=a; } } } if(flag1==1&&flag2==1||flag3==1&&flag4==1) { flag0=1; } if(flag0==1) { if(flag1==1&&flag2==1) { if(xiabiao1<xiabiao2) { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(xiabiao2-xiabiao1+1+"站"); for(int a=xiabiao1;a<=xiabiao2;a++) { System.out.println(ditie01[0][a]); } sum=Math.abs(xiabiao1-xiabiao2)+1; sum=((sum+2)/3)*aaa; System.out.println("您需花费"+sum+"元"); } else { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(xiabiao1-xiabiao2+1+"站"); for(int a=xiabiao1;a>=xiabiao2;a--) { System.out.println(ditie01[0][a]); } sum=Math.abs(xiabiao1-xiabiao2)+1; sum=((sum+2)/3)*aaa; System.out.println("您需花费"+sum+"元"); } } else if(flag3==1&&flag4==1) { if(xiabiao3<xiabiao4) { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(xiabiao4-xiabiao3+1+"站"); for(int a=xiabiao3;a<=xiabiao4;a++) { System.out.println(ditie01[1][a]); } sum=Math.abs(xiabiao3-xiabiao4)+1; sum=((sum+2)/3)*aaa; System.out.println("您需花费"+sum+"元"); } else { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(xiabiao3-xiabiao4+1+"站"); for(int a=xiabiao3;a>=xiabiao4;a--) { System.out.println(ditie01[1][a]); } sum=Math.abs(xiabiao3-xiabiao4)+1; sum=((sum+2)/3)*aaa; System.out.println("您需花费"+sum+"元"); } } } else { if(flag1==1&&flag4==1) { if(xiabiao1>=14&&xiabiao4>=1) { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(xiabiao1-14+1+xiabiao4-1+"站"); for(int a=xiabiao1;a>14;a--) { System.out.println(ditie01[0][a]); } System.out.println(ditie01[0][14]+"换乘3号线"); for(int a=1+1;a<=xiabiao4;a++) { System.out.println(ditie01[1][a]); } } else if(xiabiao1>=14&&xiabiao4<=1) { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(xiabiao1-14+1+1-xiabiao4+"站"); for(int a=xiabiao1;a>14;a--) { System.out.println(ditie01[0][a]); } System.out.println(ditie01[0][14]+"换乘3号线"); for(int a=1-1;a>=xiabiao4;a--) { System.out.println(ditie01[1][a]); } } else if(xiabiao1<=14&&xiabiao4>=1) { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(14-xiabiao1+1+xiabiao4-1+"站"); for(int a=xiabiao1;a<14;a++) { System.out.println(ditie01[0][a]); } System.out.println(ditie01[0][14]+"换乘3号线"); for(int a=1+1;a<=xiabiao4;a++) { System.out.println(ditie01[1][a]); } } else if(xiabiao1<=14&&xiabiao4<=1) { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(14-xiabiao1+1+1-xiabiao4+"站"); for(int a=xiabiao1;a<14;a++) { System.out.println(ditie01[0][a]); } System.out.println(ditie01[0][14]+"换乘3号线"); for(int a=1-1;a>=xiabiao4;a--) { System.out.println(ditie01[1][a]); } } sum=Math.abs(xiabiao1-14)+Math.abs(xiabiao4-1)+1; sum=((sum+2)/3)*aaa; System.out.println("您需花费"+sum+"元"); } else if(flag2==1&&flag3==1) { if(xiabiao2>=14&&xiabiao3>=1) { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(xiabiao2-14+1+xiabiao3-1+"站"); for(int a=xiabiao3;a>1;a--) { System.out.println(ditie01[1][a]); } System.out.println(ditie01[1][1]+"换乘1号线"); for(int a=14+1;a<=xiabiao2;a++) { System.out.println(ditie01[0][a]); } } else if(xiabiao2>=14&&xiabiao3<1) { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(xiabiao2-14+1+1-xiabiao3+"站"); for(int a=xiabiao3;a<1;a++) { System.out.println(ditie01[1][a]); } System.out.println(ditie01[1][1]+"换乘1号线"); for(int a=14+1;a<=xiabiao2;a++) { System.out.println(ditie01[0][a]); } } else if(xiabiao2<14&&xiabiao3>=1) { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(14-xiabiao2+1+xiabiao3-1+"站"); for(int a=xiabiao3;a>1;a--) { System.out.println(ditie01[1][a]); } System.out.println(ditie01[1][1]+"换乘1号线"); for(int a=14-1;a>=xiabiao2;a--) { System.out.println(ditie01[0][a]); } } else if(xiabiao2<14&&xiabiao3<1) { System.out.println("起始站:"+name1); System.out.println("终点站:"+name2); System.out.println(14-xiabiao2+1+1-xiabiao3+"站"); for(int a=xiabiao3;a<1;a++) { System.out.println(ditie01[1][a]); } System.out.println(ditie01[1][1]+"换乘1号线"); for(int a=14-1;a>=xiabiao2;a--) { System.out.println(ditie01[0][a]); } } sum=Math.abs(xiabiao2-14)+Math.abs(xiabiao3-1)+1; sum=((sum+2)/3)*aaa; System.out.println("您需花费"+sum+"元"); } } /*for(int a=0;a<19;a++) { //System.out.println("1111111111111111111111111111111"); if(ditie01[0][a].contentEquals(name1)) { //System.out.println("*********"); flag1=1; xiabiao1=a; } if(name2.contentEquals(ditie01[0][a])) { flag2=1; xiabiao2=a; } } if(xiabiao1==14&&flag2==1) ; else { for(int a=0;a<6;a++) { if(name1.contentEquals(ditie01[1][a])) { //System.out.println("2222222222222222222222"); flag3=1; xiabiao3=a; } if(name2.contentEquals(ditie01[1][a])) { flag4=1; xiabiao4=a; } } } if(flag1==1&&flag2==1||flag3==1&&flag4==1) { flag0=1; } if(flag0==1) { if(flag1==1&&flag2==1) { sum=Math.abs(xiabiao1-xiabiao2)+1; sum=((sum+2)/3)*aaa; System.out.println("您需花费"+sum+"元"); } else { sum=Math.abs(xiabiao3-xiabiao4)+1; sum=((sum+2)/3)*aaa; System.out.println("您需花费"+sum+"元"); } } else { if(flag1==1&&flag4==1) { sum=Math.abs(xiabiao1-14)+Math.abs(xiabiao4-1)+1; sum=((sum+2)/3)*aaa; System.out.println("您需花费"+sum+"元"); } else if(flag2==1&&flag3==1) { sum=Math.abs(xiabiao2-14)+Math.abs(xiabiao3-1)+1; sum=((sum+2)/3)*aaa; System.out.println("您需花费"+sum+"元"); } }*/ } }
这个最主要的数文件的导入和地铁站中中转站的算法计算
该程序可以查询出地铁线路所花价钱和所走路线