import javax.swing.*;
public class MonkeyKing{
    public static void main(String[] args){
        String s;
        int n,k,m,n1;
        s=JOptionPane.showInputDialog("Please enter the amount of the monkey:");//输入对话框
        n=Integer.parseInt(s);//将字符串s转变为整型变量
        n1=n+1;
        s=JOptionPane.showInputDialog("Please enter the first monkey‘s number:");
        k=Integer.parseInt(s);//k:first monkey
        s=JOptionPane.showInputDialog("please enter the number to eliminate");
        m=Integer.parseInt(s);//m:eliminate
        int a[]=new int[n+1];
        a[0]=0;
        System.out.println("the monkey‘s number to eliminate");
        for(int i=1;i<a.length;i++)
            a[i]=1;
        for(int i=1;i<=m;i++){
            if(n==1)
                break;
            else if(i==m){
                n--;
                i=0;
                a[k]=0;
                System.out.print(k+" ");
            }
            do{
                k++;
                k=k%n1;
            }while(a[k]!=1);
        }
        System.out.println("final number of the monkey:"+k);
    }
}
原文:http://www.cnblogs.com/sunshinewxz/p/4528901.html