我正在創建一個程序來計算他的旅行成本。每個細分市場都有成本,並且要求用戶輸入每個細分市場的成本,然後輸入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)爲什麼有* ** 6 ***掃描儀? 2)請爲代碼塊使用一致的邏輯縮進。 –
6掃描儀? 1是綽綽有餘。另外如果你想重複'1'上的整個程序作爲輸入。只是將你想要重複的代碼的相關部分放在一個while循環中,如果'userChoice'是1 –
,我想我明白了。那掃描儀呢?我應該怎麼做呢? – Kronos