Reputation: 13
I cant seem to get my main method working as it should 100%. It is good but I need fix a looping problem.
My main goal was to get the program to repeat method pathCalc() once user enters any number >=1 and to end the program when user enters 0. However, when user enters >= 1 to repeat the program, program does repeat, but when it gets to the point to ask user if to repeat or exit again, and user enters 0 to exit, the program repeats method pathCalc() instead of exiting.
How do I get this to work after repeating method, so that the user enters >= 1 to repeat method or 0 to exit?
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();
}
}
}
Upvotes: 0
Views: 11306
Reputation: 34367
You are reading the choice
tow times within the while loop. Move one instance out as below:
getIDs();
pathCalc();
System.out.println("Enter 0 to exit or any other number"+
" to evaluate another path:");
int 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();
}
Also, there is not need of extra flag. you an use choice
itself in the condition as mentioned above.
Your while loop will not start, if user enter 0
in the beginning; and the loop will terminate automatically when user enters 0
afterwards.
EDIT: Updated program.
public class AssignmentArrays {
static int[] data = new int[6];
static Scanner seg;
public static void getIDs() {
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() {
int pathCost;
System.out.println("Enter ID of segment 0 of path:");
int node1value = seg.nextInt();
System.out.println("Enter ID of segment 1 of path:");
int node2value = seg.nextInt();
System.out.println("Enter ID of segment 2 of path:");
int node3value = seg.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) {
seg = new Scanner(System.in);
getIDs();
pathCalc();
System.out.println("Enter 0 to exit or any other number"
+ " to evaluate another path:");
int choice;
choice = seg.nextInt();
while (choice != 0) {
getIDs();
pathCalc();
System.out.println("Enter 0 to exit or any other number"
+ " to evaluate another path:");
choice = seg.nextInt();
}
seg.close();
}
}
Upvotes: 1
Reputation: 508
if (choice == 0){
System.exit(0);
}
to exit directly or make sure to check choice before entering the loop
Upvotes: 0