1、 0) countPositive+; else if (num countNegative+; total += num; count+; / Read the next number if (count = 0) System.out.println(no numbers are entered except 0 else The number of positives is + countPositive);The number of negatives is + countNegative);The total is + total);The average is + total *
2、1.0 / count);5.1附加 public class Exercise05_01Extra final int NUMBER_OF_TRIALS = 10000000; int numberOfHits = 0; for (int i = 0; i NUMBER_OF_TRIALS; i+) double x = Math.random() * 2.0 - 1; double y = Math.random() * 2.0 - 1; if (x * x + y * y = 1) numberOfHits+; double pi = 4.0 * numberOfHits / NUMBE
3、R_OF_TRIALS;PI is + pi);5.2public class Exercise05_02 int correctCount = 0; / Count the number of correct answers int count = 0; / Count the number of questions java.util.Scanner input = new java.util.Scanner(System.in); long startTime = System.currentTimeMillis(); while (count 10) / 1. Generate two
4、 random single-digit integers int number1 = 1 + (int)(Math.random() * 15); int number2 = 1 + (int)(Math.random() * 15); / 2. Prompt the student to answer 搘hat is number1 ?number2?What is + number1 + + + number2 + int answer = input.nextInt(); / 3. Grade the answer and display the result String reply
5、String; if (number1 + number2 = answer) replyString = You are correct!; correctCount+;Your answer is wrong.n should be + (number1 + number2); System.out.println(replyString); / Increase the countCorrect count is + correctCount); long endTime = System.currentTimeMillis();Time spent is + (endTime - st
6、artTime) / 1000 + seconds5.2附加public class Exercise05_02Extra final int NUMBER_OF_TRIALS = 1000000; i+) if (x 1 | x 1 | y 0) double slope = (1.0 - 0) / (0 - 1.0); double x1 = x + -y * slope; if (x1 return false; return true; else */5.3 public class Exercise05_03 System.out.printf(%-14s%-10sn, Kilogr
7、amsPounds- int kilograms = 1; while (kilograms 200) %-14d%-10.1fn, kilograms, kilograms * 2.2); kilograms += 2;/* Alternatively use for loop for (int kilograms = 1; kilograms = 199; kilograms += 2) System.out.println(kilograms + tt + kilograms * 2.2);*/5.3附加public class Exercise05_03Extra Enter the
8、number of seconds: int numberOfSeconds = input.nextInt(); long currentTime = System.currentTimeMillis() / 1000; long endTime = currentTime + numberOfSeconds * 1000; while (currentTime 1000) System.out.println(endTime - currentTime) / 1000 + seconds remaining else if (endTime - currentTime = 1000)1 second remaining currentTime += 1;Stopped5.4public class Exercise05_04 MilesttKilometers- / Use while loop int miles = 1; while (miles = 10) System.out.println(miles + + miles * 1.609); miles+; for (int miles = 1; miles = 10; miles+) 5.4附加
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1