【问题标题】:Repeat statement for infinite times using loop?使用循环无限次重复语句?
【发布时间】:2016-12-02 11:53:11
【问题描述】:

我想打印如下:

for(int i=0;i<should be infinite;i++)
{
    Data is Loading.
    sleep(100);
    Data is Loading..
    sleep(100);
    Data is Loading... 
    sleep(100);
}

下面是它不工作的代码

package com.queen.a01_simple_request;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
TextView textView;
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.textView);
    button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            for (; ; ) {
                try {
                    textView.setText("Downloading The File");
                    Thread.sleep(1000);
                    textView.setText("Downloading The File.");
                    Thread.sleep(1000);
                    textView.setText("Downloading The File..");
                    Thread.sleep(1000);
                    textView.setText("Downloading The File...");
                    Thread.sleep(1000);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });
}

}

现在,这应该无限循环。这些点应该永远一个接一个地变化。我不想设置像 i

【问题讨论】:

  • 如果你想要一个无限循环,那么只需创建一个while(true)
  • 你能举个例子吗?
  • 简单地省略保护条件:for (int i = 0; /* nothing */; i++).

标签: java for-loop


【解决方案1】:
    for(;;){
    {
    try{
     Data is Loading.
     Thread.sleep(100);
     Data is Loading..
     Thread.sleep(100);
     Data is Loading... 
     Thread.sleep(100);
    }catch(Exception ignored){}
    }

编辑: 如果您在主线程(UI 线程)上执行此操作,它将冻结应用程序,您必须像这样在后台启动循环:

new Thread(new Runnable(){
   @Override
   public void run(){
       for(;;){
            try{
             Data is Loading.
             Thread.sleep(100);
             Data is Loading..
             Thread.sleep(100);
             Data is Loading... 
             Thread.sleep(100);
            }catch(Exception ignored){}
        }
    }
}

【讨论】:

  • youzking,它不工作..当我按下按钮时,它就像冻结......
  • @AppleAppala 嗯嗯。你请求了一个无限循环
  • @TimCastelijns yeh..我在上面发布了输出...它不起作用...请看一下..
  • @AppleAppala 如果您在 UI 线程中使用无限循环,它将冻结应用程序,因为循环不会让用户界面有时间刷新,您必须在后台启动它,我已经编辑了答案重试
【解决方案2】:

basic for statement 的前 3 个“部分”是可选的:

for ( [ForInit] ; [Expression] ; [ForUpdate] ) Statement

所以,虽然看起来很奇怪,但这是一个有效的 for 循环(永远不会做任何事情):

for (;;);

在您的情况下,只需省略 Expression:

for (int i = 0; ; i++) { ... }

【讨论】:

    【解决方案3】:

    试试这个:

    while(true){
       code...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 1970-01-01
      • 2018-07-27
      • 1970-01-01
      • 1970-01-01
      • 2017-10-13
      相关资源
      最近更新 更多