我正在尝试尽可能清晰地记录我的Java。我有少量的代码,不想过多解释。我目前正在使用区域,但不确定这是最好的方法。有哪些替代方案可以做区域事务,还是做得很好?
Java:
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//region Void is used to display amount of numbers without repeating a
//number, time the number display, and clear the screen after every loop.
@SuppressLint("CheckResult")
public void display_numbers(View view) {
final List<Integer> randomNumbers = new ArrayList<>();
Observable.intervalRange(0, 6, 0, 472, TimeUnit.MILLISECONDS)
.map(aLong -> {
int previous = randomNumbers.size() > 0 ? randomNumbers.get(
randomNumbers.size() - 1) : -1;
int randomNumber = generateRandomNumber();
while (randomNumber == previous) randomNumber =
generateRandomNumber();
randomNumbers.add(randomNumber);
return randomNumber;
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(integer -> {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(String.valueOf(integer));
}, error -> {}, () -> {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("");
});
}
//endregion
//region Int used to randomize the numbers generated.
private int generateRandomNumber() {
Random r = new Random();
int minNumber = 1;
int maxNumber = 9;
return r.nextInt((maxNumber - minNumber) + 1) + minNumber;
}
//endregion
}