真三国无双7猛将传裸妆:java高手些,帮帮忙

来源:百度文库 编辑:神马品牌网 时间:2024/04/29 02:59:27
Question 3: This problem uses the Counter class from Question 2. The following program segment is meant to simulate tossing a coin 100 times. It should use two Counter objects, headCount and tailCount, to count the number of heads and the number of tails. Fill in the blanks so that it will do so.
Counter headCount, tailCount;
tailCount = new Counter();
headCount = new Counter();
for ( int flip = 0; flip < 100; flip++ ) {
if (Math.random() < 0.5)
_____(1)_______________ ; // Count a "head".
else
_____(2)_______________ ; // Count a "tail".
}
System.out.println("There were " + ___(3)___ + " heads.");
System.out.println("There were " + __(4)___ + " tails.");
这几个空怎么填表哟
Question 2: For this problem, you should write a very simple but complete class. The class represents a counter that counts 0, 1, 2, 3, 4,.... The name of the class should be Counter. It has one private instance variable representing the value of the counter. It has two instance methods: increment() adds one to the counter value, and getValue() returns the current counter value. Write a complete definition for the class, Counter.

Question 2:
public class Counter {
private int value = 0;
public void increment() {
value++;
}
public int getValue() {
return value;
}
}

Question 3:
Counter headCount, tailCount;
tailCount = new Counter();
headCount = new Counter();
for ( int flip = 0; flip < 100; flip++ ) {
if (Math.random() < 0.5)
(1) headCount.increment(); // Count a "head".
else
(2) tailCount .increment(); // Count a "tail".
}
System.out.println("There were " + (3)headCount .getValue()+ " heads.");
System.out.println("There were " + (4) tailCount.getValue()+" tails.");