Vendrick-Xander

pg 315 questions HW

Jan 24th, 2019
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. 2. do while loop
  2. 3. it is a count controlled loop
  3. 4. true
  4.  
  5. 11. Hello
  6. Hello
  7. Hello
  8. Done
  9. 12. Hello
  10. Hello
  11. Hello
  12. Done
  13. 13. 2
  14. 14. 2033
  15. 15. i = 4
  16. sum = 15
  17. 16.i = 39
  18. sum = 6
  19. 17. sum = 30
  20. 18. sum = 15
  21. 19. 3
  22. 3
  23. 3
  24. 3
  25. 4
  26.  
  27. 27. for (int i = 0; i < 5; i++)
  28. {
  29. System.out.println("Hi there");
  30. }
  31. 29. int sum = 0;
  32. for (int i = 1; i <= 5; i++)
  33. {
  34. sum += i;
  35. }
  36.  
  37. 32. there is a semicolon after the start of the for loop
  38. 33. it is checking if i is greater than zero which will always be true given that it is scaling up i every iteration
  39. 35. there are no brackets after the start of the for loop so it is only going to do the sum += i; part
  40.  
  41. 38. the part where it is checking for the boolean and where it declares the statement to scale up i have been swapped
  42. 39. the math works out so that the condition will be met only twice, either change the starting value of i to 0 or have the check be 1 <= 3
  43. 40. commas were used instead of semicolons when writing the for loop, so change those to semicolons
  44. 41. i has been declared inside of the for loop, so it is a local variable, so declare i outside of the for loop
  45. 42. i is being defined twice and it does not like that. take off the int in front of the i in the for loop
  46.  
  47. 43. Scanner scan = new Scanner(System.in);
  48. System.out.println("give me a value greater than ten");
  49. int initialInput = scan.nextInt();
  50. int timesRooted;
  51. while (initialInput <= 10)
  52. {
  53. System.out.println ("something greater than ten, dummy!");
  54. initialInput = scan.nextInt();
  55. }
  56. while(initialInput > 1.01)
  57. {
  58. initialInput = math.sqrt(initialInput);
  59. timesRooted++;
  60. }
  61. System.out.println("the final square root was equal to " + initialinput);
  62. System.out.println("your inintial input was square rooted " + timesRooted +" times");
  63. 46. for(int i = 10; i <=20; i++)
  64. {
  65. 0 += i;
  66. }
  67. System.out.println("the total is " + i);
  68. 47. for(int i = 3; i <=7; i++)
  69. {
  70. 1 *= i;
  71. }
  72. System.out.println("the total is " + i);
  73. 48. int multiples = 0;
  74. for(int i = 33; i <=97; i+=7)
  75. {
  76. multiples++;
  77. }
  78. System.out.println("the total number of multiples is " + multiples);
  79. 49.Scanner scan = new Scanner(System.in);
  80. System.out.println(How many times should i print \"HelloWorld\"?");
  81. int num = scan.nextInt();
  82. while(!num.isDigit())
  83. {
  84. System.out.println("give me an integer");
  85. num = scan.nextInt();
  86. }
  87. for(int time=0; time<num; time++)
  88. {
  89. System.out.println("HelloWorld");
  90. }
  91. 50. Scanner scan = new Scanner(System.in);
  92. System.out.println("Give me your word");
  93. String word = scan.next();
  94. int length = word.length();
  95. String newWord = "";
  96. for(int chair = 0; chair < length; chair++)
  97. {
  98. newWord = word.charAt(chair) + " ";
  99. }
  100. System.out.println("Your word is now " + newWord);
  101. 51.Scanner scan = new Scanner(System.in);
  102. System.out.println("Give me your number to factorial");
  103. int num = scan.nextInt();
  104. int factorial = 1;
  105. for (int num; num >=1; num--)
  106. {
  107. factorial *=num;
  108. }
  109. System.out.println("The factorial of " + num + "is " + factorial);
  110. 52. Scanner scan = new Scanner(System.in);
  111. do
  112. {
  113. System.out.println("Give me a number");
  114. int
  115. }
  116. okay this was as far as i got before i realized that you run into the issue of having an int variable be named the same thing with each iteration, thus causing it to constantly swap out the previous entered value. I could not think of a way to circumnavigate this issue.
  117. Scanner scan = new Scanner(System.in);
  118. int minimum = 328923;
  119. for (int i = 1; i <= 10; i++)
  120. {
  121. System.out.println("Give me your number to compare");
  122. int newMin = scan.nextInt;
  123. if (newMin < minimum)
  124. {
  125. newMin = minimum;
  126. }
  127. }
  128. System.out.println("The smallest number is " + minimum);
Add Comment
Please, Sign In to add comment