SHARE
TWEET

Hw programs 21,22,23,24

Vendrick-Xander Jan 23rd, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. program 21:
  2. package com.Suarez;
  3.  
  4. /*
  5. Xander Fermier
  6. 1/22/19
  7. this programs generates random numbers until it rolls five and then terminates
  8.  */
  9.  
  10. import java.util.*;
  11.  
  12. public class RandomGenerateFive {
  13.     public static void main(String[]args)
  14.     {
  15.         Random rand = new Random();
  16.         int i;
  17.         do {
  18.             i = rand.nextInt(5) + 3;
  19.             if (i == 5)
  20.             {
  21.                 break;
  22.             }
  23.             System.out.println(i);
  24.         }
  25.         while (i !=5);
  26.     }
  27. }
  28.  
  29. Program 22:
  30. package com.Suarez;
  31.  
  32. /*
  33. Xander Fermier
  34. 1/22/19
  35. this program takes in a starting value and then values afterwards and if the secondary values are > the starting value it will print them. inputting 20
  36. terminates the program
  37.  */
  38.  
  39. import java.util.*;
  40.  
  41. public class UserIntegerOutput {
  42.     public static void main(String []args)
  43.     {
  44.         Scanner scan = new Scanner(System.in);
  45.         System.out.println("Give me the lowest number");
  46.         int firstNum = scan.nextInt();
  47.         if (firstNum == 20)
  48.         {
  49.             System.out.println("That number doesn't exist right now. Do another number");
  50.         }
  51.         int nextNum;
  52.         do {
  53.             System.out.println("Give me your next number to compare");
  54.             nextNum = scan.nextInt();
  55.             if (nextNum == 20)
  56.             {
  57.                 break;
  58.             }
  59.             if (nextNum >= firstNum)
  60.             {
  61.                 System.out.println(nextNum);
  62.             }
  63.         }
  64.         while(nextNum != 20);
  65.     }
  66. }
  67.  
  68. program 23:
  69. package com.Suarez;
  70.  
  71. /*
  72. Xander Fermier
  73. 1/22/19
  74. this program asks the user for numbers and then gets the sum of all of the numbers inputted and if the user enter -1 the program stops
  75.  */
  76.  
  77. import java.util.*;
  78.  
  79. public class UserInputAdd {
  80.     public static void main(String[]args)
  81.     {
  82.         Scanner scan = new Scanner(System.in);
  83.         System.out.println("Give me your starting number. Input \"-1\" to stop.");
  84.         int startnum = scan.nextInt();
  85.         int addnum;
  86.         int totalnum = startnum;
  87.  
  88.         do {
  89.             System.out.println("Give me the next number to add");
  90.             addnum = scan.nextInt();
  91.             startnum += addnum;
  92.             if (addnum == -1)
  93.             {
  94.                 break;
  95.             }
  96.             totalnum += addnum;
  97.             System.out.println("Current sum is " + totalnum);
  98.  
  99.         }
  100.         while(addnum != -1);
  101.         System.out.println("The end sum is " + totalnum);
  102.     }
  103. }
  104.  
  105. program 24:
  106. package com.Suarez;
  107.  
  108. /*
  109. Xander Fermier
  110. 1/22/19
  111. this programs uses a while loop to add the first four multiples of seven
  112.  */
  113.  
  114. import java.util.*;
  115.  
  116. public class FirstFourMultiplesOfSeven {
  117.     public static void main(String[]args)
  118.     {
  119.         int sum = 0;
  120.         int countMultiplesOf7=7;
  121.         int count = 1;
  122.  
  123.         while (count <= 4)
  124.         {
  125.             //sum += countMultiplesOf7;
  126.             //countMultiplesOf7 += 7;
  127.  
  128.             sum += (countMultiplesOf7*count);
  129.  
  130.             count ++;
  131.         }
  132.         System.out.println(sum);
  133.         /*
  134.         The smarter way to do this is to make a for loop. That way you could have the max attached to a number the user
  135.         puts int more easily and it would just make more sense all around to use a for loop.
  136.          */
  137.     }
  138. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top