Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class TestCar {
  3.  
  4. public static void main(String[] args){
  5.  
  6. Scanner input = new Scanner(System.in);
  7. System.out.print("Input name: ");
  8. String Name = input.nextLine();
  9.  
  10. System.out.print("Input registration: ");
  11. String Registration = input.nextLine();
  12.  
  13. System.out.print("Input colour: ");
  14. String Colour = input.nextLine();
  15.  
  16. System.out.print("Input trips: ");
  17. int Tripnum = input.nextInt();
  18.  
  19. int odomread = 0;
  20. int odomarray[] = new int [Tripnum +1];
  21. // Declare variables that we will use to store the shortest length and the last value of the run
  22. // We set it as -1 to indicate that we've never used it before
  23.  
  24. int shortlength = 0;
  25. int lastVal = -1;
  26. int longlength = 0;
  27. int one = 0;
  28.  
  29. if(Tripnum == -2) {
  30. System.out.println("Odometer reading 0: ");
  31. one = input.nextInt();
  32. System.out.println("");
  33. System.out.println(Name +" | " + Registration + " | " + Colour);
  34. System.out.println("Longest distance travelled: 0");
  35. System.out.println("Shortest distance travelled: 0");
  36. System.out.println("Average distance travelled: 0");
  37.  
  38. }
  39.  
  40. // Loop through each trip
  41. for (odomread = 0; odomread < Tripnum + 1; odomread++) {
  42.  
  43. System.out.print("Odometer reading " + odomread + ": ");
  44. odomarray[odomread] = input.nextInt();
  45.  
  46. // If it's -1 it means this is the first time its running in the loop
  47.  
  48. if (lastVal == 0) {
  49. lastVal = odomarray[odomread];
  50. longlength = lastVal;
  51. shortlength = lastVal;
  52. } else {
  53.  
  54. // Check whether its shortest, if it is, set the new short length value
  55. if ((odomarray[odomread] - lastVal) < shortlength) {
  56. shortlength = odomarray[odomread] - lastVal;
  57. }
  58. if ((odomarray[odomread] - lastVal) > longlength) {
  59. longlength = odomarray[odomread] - lastVal;
  60. }
  61. // Set the last value so we can use it for the next loop iteration
  62. lastVal = odomarray[odomread];
  63. }
  64. }
  65. System.out.println("");
  66. System.out.println(Name +" | " + Registration + " | " + Colour);
  67. System.out.println("Longest distance travelled: " + longlength);
  68. System.out.println("Shortest distance travelled: " + shortlength);
  69.  
  70. //do average here
  71. if (Tripnum == 0) {
  72. int average = 0;
  73. System.out.println("Average distance travelled: 0");
  74. }else{
  75.  
  76. int totallength = odomarray[odomread-1];
  77. if (totallength % Tripnum > 0) {
  78. double average = ((double)odomarray[odomread-1]/Tripnum);
  79. System.out.println("Average distance travelled:" + average);
  80. }
  81. else {
  82. int average = (odomarray[odomread-1]/Tripnum);
  83. System.out.println("Average distance travelled: " + average);
  84. }
  85.  
  86. }
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement