我正在修改一个程序来帮助我跟踪工作里程。我只前往特定的地点。我做了一个开关,它具有我可能拥有的所有可能的起点和终点位置,以及它们的距离。现在,当我运行我的程序时,会弹出一个窗口询问我是否去了每个位置。我必须每次都将结束位置键入下一个起始位置。我希望能够键入一个长字符串(例如:location1> location2 + location3> location4)并计算总里程数。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Main extends JFrame {
public static void main(String[] agrs) {
double total = 0;
double d = 0;
int c = 0;
int t = 0;
//Scanner input = new Scanner(System.in);
// Scanner input1 = new Scanner(System.in);
// String place;
JOptionPane.showMessageDialog(null, "MILEAGE CALCULATOR \n\nLocations: ADMIN, AE, BE, CE, CHS, CJHS, IE, NSE, PKE, RHS, SMS, SPE \nFormat: Location1>Location2");
// JOptionPane.showMessagedialog(null,"Locations: ADMIN, AE, BE, CE, CHS, CJHS, IE, NSE, PKE, RHS, SMS, SPE");
//System.out.println("MILAGE CALCULATOR \nLocations: ADMIN, AE, BE, CE, CHS, CJHS, IE, NSE, PKE, RHS, SMS, SPE \nFormat: LOCATION1>LOCATION2");
t = Integer.parseInt(JOptionPane.showInputDialog(null, "How many places did you go to from the admin building?"));
//t = input.nextInt();
//t++;
// System.out.println(t);
// System.out.println(c);
while (t > c) {
String place = JOptionPane.showInputDialog(null, "where did you go?");
//String place = input.nextLine();
// place.equalsIgnoreCase(place);
d=0;
switch (place.toUpperCase()) {
case "ADMIN>AE":
case "AE>ADMIN":
d = 7;
break;
case "ADMIN>BE":
case "BE>ADMIN":
d = 2.8;
break;
case "ADMIN>CE":
case "CE>ADMIN":
d = 1.5;
break;
case "ADMIN>CHS":
case "CHS>ADMIN":
d = .6;
break;
case "ADMIN>CJHS":
case "CJHS>ADMIN":
d = 1.5;
break;
case "ADMIN>IE":
case "IE>ADMIN":
d = 2.3;
break;
case "ADMIN>NSE":
case "NSE>ADMIN":
d = 4.5;
break;
case "ADMIN>PKE":
case "PKE>ADMIN":
d = 2.6;
break;
case "ADMIN>RHS":
case "RHS>ADMIN":
d = 1.5;
break;
case "ADMIN>SMS":
case "SMS>ADMIN":
d = 3.9;
break;
case "ADMIN>SPE":
case "SPE>ADMIN":
d = 3.2;
break;
case "AE>BE":
case "BE>AE":
d = 9.5;
break;
case "AE>CE":
case "CE>AE":
d = 6.3;
break;
case "AE>CHS":
case "CHS>AE":
d = 7.6;
break;
case "AE>CJHS":
case "CJHS>AE":
d = 6.3;
break;
case "AE>IE":
case "IE>AE":
d = 6.3;
break;
case "AE>NSE":
case "NSE>AE":
d = 8;
break;
case "AE>PKE":
case "PKE>AE":
d = 7.6;
break;
case "AE>RHS":
case "RHS>AE":
d = 6.3;
break;
case "AE>SMS":
case "SMS>AE":
d = 8.2;
break;
case "AE>SPE":
case "SPE>AE":
d = 7.5;
break;
case "BE>CE":
case "CE>BE":
d = 3.7;
break;
case "BE>CHS":
case "CHS>BE":
d = 2.7;
break;
case "BE>CJHS":
case "CJHS>BE":
d = 3.7;
break;
case"BE>IE":
case "IE>BE":
d = 5;
break;
case "BE>NSE":
case "NSE>BE":
d = 4.2;
break;
case "BE>PKE":
case "PKE>BE":
d = 2.8;
break;
case "BE>RHS":
case "RHS>BE":
d = 3.7;
break;
case "BE>SMS":
case "SMS>BE":
d = 4.2;
break;
case "BE>SPE":
case "SPE>BE":
d = 7.4;
break;
case "CE>CHS":
case "CHS>CE":
d = 1.9;
break;
case "CE>CJHS":
case "CJHS>CE":
d = 0;
break;
case "CE>IE":
case "IE>CE":
d = 2.5;
break;
case "CE>NSE":
case "NSE>CE":
d = 3.5;
break;
case "CE>PKE":
case "PKE>CE":
d = 2.3;
break;
case "CE>RHS":
case "RHS>CE":
d = 0;
break;
case "CE>SMS":
case "SMS>CE":
d = 3.7;
break;
case "CE>SPE":
case "SPE>CE":
d = 3.3;
break;
case "CHS>CJHS":
case "CJHS>CHS":
d = 1.8;
break;
case "CHS>IE":
case "IE>CHS":
d = 2.9;
break;
case "CHS>NSE":
case "NSE>CHS":
d = 3.2;
break;
case "CHS>PKE":
case "PKE>CHS":
d = 2;
break;
case "CHS>RHS":
case "RHS>CHS":
d = 1.9;
break;
case "CHS>SMS":
case "SMS>CHS":
d = 3.3;
break;
case "CHS>SPE":
case "SPE>CHS":
d = 3.8;
break;
case "CJHS>IE":
case "IE>CJHS":
d = 2.5;
break;
case "CJHS>NSE":
case "NSE>CJHS":
d = 4.3;
break;
case "CJHS>PKE":
case "PKE>CJHS":
d = 2.3;
break;
case "CJHS>RHS":
case "RHS>CJHS":
d = 0;
break;
case "CJHS>SMS":
case "SMS>CJHS":
d = 3.7;
break;
case "CJHS>SPE":
case "SPE>CJHS":
d = 3.3;
break;
case "IE>NSE":
case "NSE>IE":
d = 5.8;
break;
case "IE>PKE":
case "PKE>IE":
d = 4.6;
break;
case "IE>RHS":
case "RHS>IE":
d = 2.5;
break;
case "IE>SMS":
case "SMS>IE":
d = 6;
break;
case "IE>SPE":
case "SPE>IE":
d = 3.1;
break;
case "NSE>PKE":
case "PKE>NSE":
d = 1.3;
break;
case "NSE>RHS":
case "RHS>NSE":
d = 3.5;
break;
case "NSE>SMS":
case "SMS>NSE":
d = .2;
break;
case "NSE>SPE":
case "SPE>NSE":
d = 7.2;
break;
case "PKE>RHS":
case "RHS>PKE":
d = 2.2;
break;
case "PKE>SMS":
case "SMS>PKE":
d = 1.5;
break;
case "PKE>SPE":
case "SPE>PKE":
d = 5.5;
break;
case "RHS>SMS":
case "SMS>RHS":
d = 3.7;
break;
case "RHS>SPE":
case "SPE>RHS":
d = 3.3;
break;
case "SMS>SPE":
case "SPE>SMS":
d = 7.4;
break;
}
// System.out.println(total);
total = d + total;
// System.out.println(total);
// JOptionPane.showMessageDialog("miles Driven ", d+total);
c++;
}
JOptionPane.showMessageDialog(null, "Miles Driven: " + total);
//System.out.println(total);
}
}
答案 0 :(得分:0)
您可以执行以下操作:
int total = 0;
String[] places = place.split(">"); //Split your input string place into a list
for(int i = 0; i < places.length - 1; i++){
String leg = places[i] + ">" + places[i+1]; //Create string representing this leg of the journey
//Figure out the length of this leg of the journey using your switch statement
total += d; //d is the variable you were using in the switch
}
因此,如果您输入“AE&gt; Admin&gt; BE&gt; CJHS”,循环的第一次迭代会将案例设置为AE&gt; Admin,并使用您的开关将距离设置为7并将总数设置为7,第二次应该获得Admin&gt ; BE,距离= 2.8,总数= 9.8,第三次迭代BE&gt; CJHS,距离= 3.7,总数= 13.5,如果我正确地进行心理数学计算。然后我会增加到3,因为数组的长度只有4,所以它会跳出循环。
如果您希望以不同方式格式化字符串,请参阅字符串类的java文档及其split方法。 http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)