4.9 solving problems

xiaoxiao2021-03-06  49

Software lifecycle: demand analysis and definition, design, implementation, test, maintenance. 4.9.1 TWENTY-One Pickup: Demand Analysis and Definition 4.9.2 TWENTY-One Pickup: Designed to write pseudo code to design the method you need to write. 4.9.3 TWENTY-One Pickup: Implement TWENTYONEPICKUP.JAVA

// TwentyOnePickup.java import tio *;.. Public class TwentyOnePickup {/ ** * Play the game of Twenty-One Pickup The user and the computer * take turns removing from 1 to 3 stones from a pile There are * 21 stones. In The Pile to Start With. The last one to remove a * stone wins. * / public static void main (string [] args) {printinstructions (); // create the initial pile with 21 stones int numberofstones = 21; // keep track of who moved last boolean playerMovedLast = false; while (numberOfStones> 0) {numberOfStones = playerMove (numberOfStones); playerMovedLast = true; if (numberOfStones> 0) {numberOfStones = computerMove (numberOfStones); playerMovedLast = false;}} / / Print The Outcome IF (PlayermoveDlast) System.out.Println ("Congratulation, You Won."); Else System.Out.println ("Better Luck Next Time. ");} / ** * Printinstructions Prints the initial instructions * / static void printinstructions () {system.out.println (" The Object of this game is " " To remove the last stone./n " " THERE 21 Stones in the Pile to Start with./N " " You May Remove From 1 to 3 stones on each move./n " " good luck! ");} / ** * Playermove Completes One move by the Player. * * @Param Numberofstones * The number of stones reamining in the pile. * @return the number of stones remaining after the user '

. S move * / static int playerMove (int numberOfStones) {int move = getUserMove (numberOfStones); numberOfStones = numberOfStones - move; System.out.println ( "There are" numberOfStones ". Stones remaining"); return numberOfStones; } / ** * computerMove completes one move by the computer. * * @param numberOfStones * The number of stones reamining in the pile. * @return The numberOfStones remaining after the computer's move. * / static int computerMove (int numberOfStones) {int Move; if (Numberofstones <= 3) {move = Numberofstones; / * Remove the rest * /} else {move = numberofstones% 4; if (move == 0) Move = 1;} Numberofstones = Numberofstones - Move; System. Out.println ("THE Computer Removes" Move "Stones Leaving" Numberofstones "); Return Numberofstones; } / ** * getUserMove reads in the user's move, only accepting legal * inputs. * * @Param numberOfStones * The number of stones reamining in the pile. * @Return The number of stones selected for removal by the user. * / Static INT GETUSERMOVE (Int Numberofstones) {System.out.println ("Your Move - How Many Stones" "Do You Wish To Remove?"); int move = console.in.readint (); while (move> Numberofstones || Move <1 || Move> 3) {if (NumberOfstones> =

3) System.out.println ("Sorry," "You can Only Remove 1 To 3 stones."); Else system.out.println ("Sorry, You can only" "Remove 1 to" Numberofstones " Stones. "); System.out.println (" How Many Stones " " Do You Wish To Remove? "); Move = Console.in.Readint ();} Return Move;}} For the analysis, you can first Set breakpoints at the beginning of the program, then apply the Eclipse's debug as, then use F5 and F7 to carefully analyze the running direction of the program in the corresponding view. 4.9.4 TWENTY-One Pickup: STATEMENT CONVERET: To establish a collection of test conditions (ie, test sets), all test conditions in the collection execute each statement in the program is at least once. But the complete statement coverage cannot guarantee the correctness of the program. Branch coverage: To establish a collection of test conditions, after all test conditions in the collection, ensure that each branch in the program is executed at least once.

转载请注明原文地址:https://www.9cbs.com/read-82531.html

New Post(0)