2012-11-16 30 views
0

我正在創建一個程序來計算他的旅行成本。每個細分市場都有成本,並且要求用戶輸入每個細分市場的成本,然後輸入3個細分市場ID(0-6)。 3 ID的成本被添加到最終價格。用戶輸入後重復執行的代碼

如果用戶輸入> = 1(請參見注釋),我需要從頭開始重複該程序,我該怎麼做?另外,我可以改進我的計劃嗎?如何?

import java.util.Scanner; 

public class AssignmentArrays{ 
    public static void main(String[] args){ 


     Scanner seg0 = new Scanner(System.in); 
     Scanner seg1 = new Scanner(System.in); 
     Scanner seg2 = new Scanner(System.in); 
     Scanner seg3 = new Scanner(System.in); 
     Scanner seg4 = new Scanner(System.in); 
     Scanner seg5 = new Scanner(System.in); 

     int[] data = new int [6]; 
     data[0] = 0; 
     data[1] = 0; 
     data[2] = 0; 
     data[3] = 0; 
     data[4] = 0; 
     data[5] = 0; 

     /* Segment values */ 

     while(data[0] == 0){ 

      System.out.println("Enter cost for segment 0:"); 
      data[0] = seg0.nextInt(); 

      System.out.println("Enter cost for segment 1:"); 
      data[1] = seg1.nextInt(); 

      System.out.println("Enter cost for segment 2::"); 
      data[2] = seg2.nextInt(); 

      System.out.println("Enter cost for segment 3:"); 
      data[3] = seg3.nextInt(); 

      System.out.println("Enter cost for segment 4:"); 
      data[4] = seg4.nextInt(); 

      System.out.println("Enter cost for segment 5:"); 
      data[5] = seg5.nextInt(); 

      /* Path inputs */ 

      Scanner node1 = new Scanner(System.in); 
      Scanner node2 = new Scanner(System.in); 
      Scanner node3 = new Scanner(System.in); 

      int node1value; 
      int node2value; 
      int node3value; 
      int pathCost; 

      System.out.println("Enter ID of segment 0 of path:"); 
       node1value = node1.nextInt(); 

      System.out.println("Enter ID of segment 1 of path:"); 
       node2value = node2.nextInt(); 

      System.out.println("Enter ID of segment 2 of path:"); 
       node3value = node3.nextInt(); 

      /* Path cost calculation */ 

      pathCost = data[node1value] + data[node2value] + data[node3value]; 
       System.out.println("The cost of the trip is: $" + pathCost); 

      /* Repeate or end program */ 

      Scanner end = new Scanner(System.in); 

      int userChoice; 

      System.out.println("Enter 0 to exit or any other number to evaluate another path:"); 
       userChoice = end.nextInt(); 

      if (userChoice == 0){ 
       System.out.println("The program has ended"); 
       break; 
       } 
       else if(userChoice >= 1){ 
       /* REPEATE ALL OF THE ABOVE HERE */ 
       } 
      } 

    } 

} 
+1

1)爲什麼有* ** 6 ***掃描儀? 2)請爲代碼塊使用一致的邏輯縮進。 –

+0

6掃描儀? 1是綽綽有餘。另外如果你想重複'1'上的整個程序作爲輸入。只是將你想要重複的代碼的相關部分放在一個while循環中,如果'userChoice'是1 –

+0

,我想我明白了。那掃描儀呢?我應該怎麼做呢? – Kronos

回答

1

我可能會讓該類更加面向對象。但是,你可以簡化你有什麼打破了你使用代碼的方法:

import java.util.Scanner; 

public class AssignmentArrays{ 

static int[] data = new int [6]; 

public static void getSegmentIDs() { 
    ... 
} 

pubilc static int getUserMenuChoice() { 
    ... 
} 

public static void main(String[] args){ 

int exit = false; 

while(!exit) { 
    getSegmentIDs(); 
    choice = getUserMenuChoice(); 
    if (choice == 0) exit = true; 
} 

} 
} 
+0

我已經將程序分爲3個方法,getIDs(); ,pathCalc();和主要方法。現在我遇到了一個問題,因爲DATA變量在getIDs()中;我需要它在pathCalc中。 「找不到符號」 – Kronos

+0

您可以使其成爲AssignmentArrays類的靜態成員。我已經更新了我的答案以反映這一點。 –

+0

好的,現在可以工作,但是我的程序從pathCalc()開始;而不是getIDs();.我怎樣才能解決這個問題? – Kronos

0

我會把整個事情變成一個做while語句來這樣做:

String answer =""; 
    do { 
    Scanner seg0 = new Scanner(System.in); 
    Scanner seg1 = new Scanner(System.in); 
    Scanner seg2 = new Scanner(System.in); 
    YOUR CODE CONTINOUS 

    /* Repeate or end program */ 
    System.out.println("would you like to redo? (Y/N)"); 
    answer = end.next(); 
    } while(answer.toLowerCase().contains("y"));