0

我正在纠正一个程序来帮助我跟踪我的工作里程。我只去特定的地方。我做了一个开关,它具有我可能拥有的所有可能的开始和结束位置,以及它们的距离。现在当我运行我的程序时,会弹出一个窗口询问我是否去了每个位置。我每次都必须输入结束位置作为下一个开始位置。我希望能够输入一个长字符串(例如: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); } }
4

1 回答 1

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>Admin>BE>CJHS”,则循环的第一次迭代会将大小写设置为 AE>Admin,并使用您的开关将距离设置为 7,总距离为 7,第二次通过应该得到 Admin>BE,距离 = 2.8 和总计 = 9.8,第三次迭代 BE>CJHS,距离 = 3.7 和总计 = 13.5,如果我的心算正确的话。然后 i 将递增到 3,由于数组的长度只有 4,它会跳出循环。

如果您希望以不同的方式格式化您的字符串,这里是字符串类的 java 文档和它的 split 方法。http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#split(java.lang.String )

于 2012-08-14T13:46:29.600 回答