Note that there are some explanatory texts on larger screens.

plurals
  1. PORepeat executed code after user input
    primarykey
    data
    text
    <p>I'm creating a program that will calculate he cost of traveling. Each segment has a cost, and user is asked to enter the cost for each segment, then enter 3 segment ID's (0-6). The cost of the 3 ID's is added to give a final price.</p> <p>I need to repeat the program from the beginning if user enters >=1 (see comment at the end), how can I do this? Also, could I improve my program, and how?</p> <pre><code>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 &gt;= 1){ /* REPEATE ALL OF THE ABOVE HERE */ } } } } </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. 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