我似乎无法让我的主要方法正常工作,因为它应该 100%。这很好,但我需要解决循环问题。
我的主要目标是让程序在用户输入任何数字 >=1 时重复方法 pathCalc() 并在用户输入 0 时结束程序。但是,当用户输入 >=1 重复程序时,程序确实重复,但是当它询问用户是否重复或再次退出,并且用户输入 0 退出时,程序重复方法 pathCalc() 而不是退出。
如何在重复方法后使其工作,以便用户输入 >= 1 重复方法或 0 退出?
import java.util.Scanner;
public class AssignmentArrays{
static int[] data = new int [6];
public static void getIDs(){
Scanner seg = 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 */
System.out.println("Enter cost for segment 0:");
data[0] = seg.nextInt();
System.out.println("Enter cost for segment 1:");
data[1] = seg.nextInt();
System.out.println("Enter cost for segment 2::");
data[2] = seg.nextInt();
System.out.println("Enter cost for segment 3:");
data[3] = seg.nextInt();
System.out.println("Enter cost for segment 4:");
data[4] = seg.nextInt();
System.out.println("Enter cost for segment 5:");
data[5] = seg.nextInt();
}
public static void pathCalc(){
/* Path inputs */
Scanner node1 = new Scanner(System.in);
int pathCost;
System.out.println("Enter ID of segment 0 of path:");
int node1value = node1.nextInt();
System.out.println("Enter ID of segment 1 of path:");
int node2value = node1.nextInt();
System.out.println("Enter ID of segment 2 of path:");
int node3value = node1.nextInt();
/* Path cost calculation */
pathCost = data[node1value] + data[node2value] + data[node3value];
System.out.println("The cost of the trip is: $" + pathCost);
}
public static void main(String[] args){
getIDs();
pathCalc();
System.out.println("Enter 0 to exit or any other number"+
" to evaluate another path:");
int choice;
choice = end.nextInt();
while(choice != 0){
getIDs();
pathCalc();
System.out.println("Enter 0 to exit or any other number"+
" to evaluate another path:");
choice = end.nextInt();
}
}
}