Advertisement
Arush22

Untitled

Jan 23rd, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. Answer to problem 21
  2. package com.company;
  3. import java.util.*;
  4. import java.lang.*;
  5.  
  6. public class WhileLoopPractice1 {
  7. public static void main(String[] args)
  8. {
  9. Random rand = new Random();
  10. int secretNumber = rand.nextInt(7)+3;
  11.  
  12. while(secretNumber != 5)
  13. {
  14. secretNumber = rand.nextInt(7)+3;
  15. System.out.println(secretNumber);
  16. }
  17.  
  18. }
  19. }
  20.  
  21. Answer to problem 22
  22. package com.company;
  23. import java.util.*;
  24. import java.lang.*;
  25.  
  26. public class TwoRandomInts {
  27. public static void main(String[] args)
  28. {
  29. Scanner input = new Scanner(System.in);
  30. System.out.println("Type a number");
  31. int typedNumber = input.nextInt();
  32. int retypedNumber = input.nextInt();
  33. while(retypedNumber != 20) {
  34. System.out.println("Type a new number");
  35. retypedNumber = input.nextInt();
  36. if(retypedNumber > typedNumber && retypedNumber !=20){
  37. System.out.println(retypedNumber);
  38. }
  39.  
  40. }
  41. }
  42. }
  43.  
  44. Answer to problem 23
  45. package com.company;
  46. import java.util.*;
  47. import java.lang.*;
  48.  
  49. public class WhileSums {
  50. public static void main(String[] args)
  51. {
  52. Scanner input = new Scanner(System.in);
  53. System.out.println("Type a number");
  54. int number1 = input.nextInt();
  55. System.out.println("Type a second number");
  56. int number2 = input.nextInt();
  57. while(number1 != -1 || number2 != -1) {
  58. System.out.println(number1+number2);
  59. System.out.println("Type a new number");
  60. number1 = input.nextInt();
  61. System.out.println("Type a second new number");
  62. number2 = input.nextInt();
  63.  
  64. }
  65. }
  66. }
  67.  
  68. Answer to problem 25
  69. package com.company;
  70. import java.util.*;
  71. import java.lang.*;
  72.  
  73. public class SentenceConcatenater {
  74. public static void main(String[] args)
  75. {
  76. Scanner input = new Scanner(System.in);
  77. String sentence = "";
  78. String word = "";
  79. System.out.println("Type a sentence");
  80. sentence = input.nextLine();
  81. System.out.println("Type a second sentence");
  82. word = input.next();
  83. while(! word.equals("end")) {
  84. System.out.println(sentence+" "+word);
  85. System.out.println("Type a new sentence");
  86. sentence = input.nextLine();
  87. System.out.println("Type a second new sentence");
  88. word = input.next();
  89.  
  90. }
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement