Exercise to help with dice program:
1) Simple totalling: Keep changing this program by adding each step:
► Write a program to display the numbers between 2 and 40 counting by 2's. You will see:
2
4
up to 40
► Change that to print the total after each number. Print "The total including the last number is " and the total.You will see.
2
the total including the last number is 4
4
the total including the last number is 6
6
the total including the last number is 10
up to 40
► Change that to print the total of all the numbers between 2 and 40 couting by 2's. Label the total : final total. You will see
2
the total including the last number is 4
4
the total including the last number is 6
6
the total including the last number is 10
up to 40
the final total is xxxx
► Change that to not print the running total:
2
4
6
up to 40
the final total is xxxx
► Change that to do the same thing counting backwards.
40
38
upt to 40
the final total is xxxx
2) Dice program: Keep changing this program by adding each step:
► Write this program that displays the number 5 100 times.
int numberIwant;
for (int count = 1; count <= 100; count++)
{
numberIwant = 5;
System.out.println("The number is " + numberIwant);
}
► Accumulate a total, and at the end, print the final total. Print "The final total of added 5s is " and the total. You will see:
The number is 5
The number is 5
printed 100 times
The total is xxxx
► Change your loop to only add the number 5 2 times. Leave everything else the same. You will now see:
The number is 5
The number is 5
The total of added 5s is 10
► Run the 5 2 times loop 20 times. You want it to repeat the following
The number is 5
The number is 5
The total of added 5s is 10
The number is 5
The number is 5
The total of added 5s is 10
(repeat for 20 times)
► Change the number is 5 to the dice throw logic, and accumulate the dice throw instead of the numberIwant.
► Change the number of dice being rolled to 5 instead of 2.