Note that there are some explanatory texts on larger screens.

plurals
  1. PORepeat Method In While Loop
    text
    copied!<p>I cant seem to get my main method working as it should 100%. It is good but I need fix a looping problem. </p> <p>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. </p> <p>How do I get this to work after repeating method, so that the user enters >= 1 to repeat method or 0 to exit?</p> <pre><code> 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(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload