Q: this is a question I asked when I went to elementary school. He said that when I built a house in the school, a bunch of migrant workers were playing this game together and there were three piles of stones, there are 3, 5, and 7 respectively. The game rule is to take two people in turn. Each person can only take one pile at a time, and each time can only take one to three, whoever takes the last stone gives the other party a dollar.
Package COM. HPU. bai; import Java. util. role; public class cobble {public static void main (string [] ARGs) {cobble c = new cobble (); C. print (); C. getcobble () ;}int [] arr = {3, 5, 7}; int n = 15; int COUNT = 0; counter input = new partition (system. in); Public int total () {int m = 0; For (INT I = 0; I <arr. length; I ++) {M + = arr [I];} return m;} public void print () {for (INT I = 0; I <arr. length; I ++) {system. out. println ("no" + (I + 1) + "remaining" + arr [I] + "##") ;}} public void player (int B) {If (B % 2 = 1) {system. out. println ("the first person took the stone");} else {system. out. println ("this second person has taken stones") ;}} public void getcobble () {count ++; If (n> 1) {player (count); system. out. println ("Enter in which stone you want to take the stone"); int I = input. nextint ()-1; system. out. println ("Enter the number of stones you want to fetch in this pile"); int M = input. nextint (); If (M> 3) {system. out. println ("your input is incorrect. Please enter it again"); count --; getcobble ();} else {arr [I] = arr [I]-m; if (ARR [I] <0) {system. out. println ("the stone is almost gone, or you should take less points, or you should change a pile"); arr [I] = arr [I] + m; count --;} print (); n = total (); getcobble () ;}} else {If (count % 2 = 1) {system. out. println ("don't take it, you have already lost 1st players");} else {system. out. println ("don't take it, you have already lost 2nd players ");}}}}
The key to program implementation is to have a variable similar to a pointer to control who should take it. Of course, after learning the thread well, this program can be further improved. Java beginner QQ: 623687677. You can communicate with each other or run the code.
Analysis of Small game programs