Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class TestCar {
- public static void main(String[] args){
- Scanner input = new Scanner(System.in);
- System.out.print("Input name: ");
- String Name = input.nextLine();
- System.out.print("Input registration: ");
- String Registration = input.nextLine();
- System.out.print("Input colour: ");
- String Colour = input.nextLine();
- System.out.print("Input trips: ");
- int Tripnum = input.nextInt();
- int odomread = 0;
- int odomarray[] = new int [Tripnum +1];
- // Declare variables that we will use to store the shortest length and the last value of the run
- // We set it as -1 to indicate that we've never used it before
- int shortlength = 0;
- int lastVal = -1;
- int longlength = 0;
- int one = 0;
- if(Tripnum == -2) {
- System.out.println("Odometer reading 0: ");
- one = input.nextInt();
- System.out.println("");
- System.out.println(Name +" | " + Registration + " | " + Colour);
- System.out.println("Longest distance travelled: 0");
- System.out.println("Shortest distance travelled: 0");
- System.out.println("Average distance travelled: 0");
- }
- // Loop through each trip
- for (odomread = 0; odomread < Tripnum + 1; odomread++) {
- System.out.print("Odometer reading " + odomread + ": ");
- odomarray[odomread] = input.nextInt();
- // If it's -1 it means this is the first time its running in the loop
- if (lastVal == 0) {
- lastVal = odomarray[odomread];
- longlength = lastVal;
- shortlength = lastVal;
- } else {
- // Check whether its shortest, if it is, set the new short length value
- if ((odomarray[odomread] - lastVal) < shortlength) {
- shortlength = odomarray[odomread] - lastVal;
- }
- if ((odomarray[odomread] - lastVal) > longlength) {
- longlength = odomarray[odomread] - lastVal;
- }
- // Set the last value so we can use it for the next loop iteration
- lastVal = odomarray[odomread];
- }
- }
- System.out.println("");
- System.out.println(Name +" | " + Registration + " | " + Colour);
- System.out.println("Longest distance travelled: " + longlength);
- System.out.println("Shortest distance travelled: " + shortlength);
- //do average here
- if (Tripnum == 0) {
- int average = 0;
- System.out.println("Average distance travelled: 0");
- }else{
- int totallength = odomarray[odomread-1];
- if (totallength % Tripnum > 0) {
- double average = ((double)odomarray[odomread-1]/Tripnum);
- System.out.println("Average distance travelled:" + average);
- }
- else {
- int average = (odomarray[odomread-1]/Tripnum);
- System.out.println("Average distance travelled: " + average);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement