Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are reading the <code>choice</code> tow times within the while loop. Move one instance out as below:</p> <pre><code> 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(); } </code></pre> <p>Also, there is not need of extra flag. you an use <code>choice</code> itself in the condition as mentioned above.</p> <p><strong>Your while loop will not start, if user enter <code>0</code> in the beginning; and the loop will terminate automatically when user enters <code>0</code> afterwards.</strong></p> <p><strong>EDIT:</strong> Updated program.</p> <pre><code>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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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