捡石子小游戏程序解析

package   com.hpu.bai;
 
import   java.util.Scanner;
 
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 ;
         Scanner input = new   Scanner(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( "第" +(i+ 1 )+ "堆还剩下" +arr[i]+ "个石子##" );
             }
         }
         public   void   player( int   b){
             if (b% 2   == 1 ){
                 System.out.println( "该第一个人拿石子了" );
             } else {
                 System.out.println( "该第二个人拿石子了" );
             }
                     
         }
         public   void   getCobble(){
             count++;
             if (n> 1 ){
                 player(count);
                 System.out.println( "请输入你要在哪一堆石子中取石子" );
                 int   i = input.nextInt()- 1 ;
                 System.out.println( "请输入你要在这堆石子中取几个" );
                 int   m = input.nextInt();
                 if (m> 3 ){
                         System.out.println( "你的输入有误,请重新输入" );
                         count--;
                         getCobble();
                 } else {
                 arr[i]=arr[i]-m;
                 if (arr[i]< 0 ){
                     System.out.println( "石子快没了,要么你少拿点,要么你换一堆吧" );
                     arr[i]= arr[i]+m;
                     count--;
                 }
                 print();
                 n = total();
                 getCobble();
             }
             } else {
                 if (count% 2   == 1 ){
                     System.out.println( "别拿了,第1位玩家你已经输了" );
                 } else {
                     System.out.println( "别拿了,第2位玩家你已经输了" );
                 }
             }
         }
}

你可能感兴趣的:(游戏)