ぞ红落叶ぞ  11:41:11
有个问题 请教一下
比如:
现有数组【1,2,3,4,5】
第一次循环输出
【1,2,3,4,5】
1分钟后
第二次循环
【2,3,4,5,1】
再过一分钟
第三次循环输出
【3,4,5,1,2】
在过一分钟
第4次循环输出
【4,5,1,2,3】
以此类推
第6次循环输出
【1,2,3,4,5】这个问题怎么解决!!!

解决方案 »

  1.   

    import java.util.*; 
    import java.text.*;public class Myfor extends Thread
    {
    String[] sdate={"1","2","3","4","5","6","7","8","9","0"};
    int sgateleng=0;
    int count=0;
    int forcount=0;
    int m=0;
    int yxcs=1;
    Myfor(int mm){m=mm;}
    public void run ()
    {

    sgateleng=sdate.length;
    try
    {
    while (true)
    {
    forcount=count%sgateleng;
    for (int k=0;k<sgateleng ;k++ )
    {
    if (forcount>=sgateleng)
    {
    System.out.print("["+sdate[forcount-sgateleng]+"]");
    }
    else 
    {
    System.out.print("["+sdate[forcount]+"]");
    }
    forcount++;
    }
    System.out.println("第"+yxcs+"次运行结果:");
    count++;
    yxcs++;
    sleep(m);
    }
    }
    catch (InterruptedException e) {return;}
    }
    public static void main(String args[])
    {
    Myfor xian = new Myfor(60000);
    xian.start();   //启动线程
    }
    }
      

  2.   

    package test;public class MyThread extends Thread {    private int[] str = { 1, 2, 3, 4, 5 };    private int count;    public void run() {        try {
                System.out.println();
                System.out.println("Test Start >>>");
                System.out.print("第1次循环: ");
                System.out.println("【" + str[0] + "," + str[1] + "," + str[2] + "," + str[3] + "," + str[4] + "】");
                for (int i = 0; i < str.length; i++) {
                    Thread.sleep(60000);                count = str[0];
                    str[0] = str[1];
                    str[1] = str[2];
                    str[2] = str[3];
                    str[3] = str[4];
                    str[4] = count;                System.out.print("第" + (i + 2) + "次循环: ");
                    System.out.println("【" + str[0] + "," + str[1] + "," + str[2] + "," + str[3] + "," + str[4] + "】");
                }
                System.out.println();
                System.out.println("Test End <<<");
            } catch (Exception e) {
                System.out.println("Test Exception !!!");
                e.printStackTrace();
            }
        }    public static void main(String args[]) {        MyThread thread = new MyThread();
            thread.start();
        }
    }
      

  3.   

    楼上的可以把run里面的实现抽取成为一个函数, 内部用一个static变量记录调用次数就行, 然后循环取模输出, 这样也许更加符合楼主的要求
      

  4.   

    public class num 
    {
    public static void main(String [] args)
    {
    String [] str = {"1","2","3","4","5"};
    System.out.println("[" + str[0] + "," + str[1] + "," + str[2] + "," + str[3] + "," + str[4] + "]");
    for(int i=0;i<5;i++)
    {
    String temp = str[0];
    for(int j=0;j<4;j++)
    {
    str[j] = str[j+1];
    }
    str[4] = temp;
    System.out.println("[" + str[0] + "," + str[1] + "," + str[2] + "," + str[3] + "," + str[4] + "]");
    }
    }
    }
      

  5.   

    【mzhang_sh】你说的考虑过了,更麻烦。不如这样来的直观
      

  6.   

        public static void main (String arg[]) {  
            testArray();
        }
        public static void testArray (){
            int [] i_a = {1,2,3,4,5};
            for (int i = 0; i < 6; i++) {
                for (int j = 0; j < 5; j++) {
                    System.out.print(i_a[(i+j)%5]);
                }
                System.out.println();
                try {
                    Thread.sleep(60000);
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }这样应该比较易懂,呵呵
      

  7.   

    int[] a={1,2,3,4,5];
    for(int j=0;;j++)
    {
    if(j>=a.length)j%=a.length;
    for(int i=j;i<j;)
    {
    if(i>=a.length)i%=a.length;
    System.out.print(a[i]+"\t");
    j++;
    }
    //这里加一个延时的代码,抱歉各位,我初学,不知道怎么延时,谁可以讲
    }