質問
Thread.sleep(500);を使うとsleepを使う分だけ起動の時間が遅くなっているように感じます。
原因は何か教えて頂けると幸いです。
ソースコード
java
1import android.support.v7.app.AppCompatActivity; 2import android.os.Bundle; 3import android.widget.TextView; 4 5public class MainActivity extends AppCompatActivity { 6 String screen; 7 int random, i, j, x, y, z; 8 9 int field[][] = //パズルの中身 10 { // 0 1 2 3 4 5 6 7 8 9 10 11 12 11 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //0 12 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //1 13 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //2 14 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //3 15 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //4 16 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //5 17 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //6 18 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //7 19 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //8 20 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //9 21 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //10 22 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //11 23 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //12 24 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //13 25 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //14 26 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //15 27 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //16 28 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //17 29 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //18 30 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //19 31 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //20 32 }; 33 34 int blocktype[][][][] = { //ブロックタイプ 7種 回転 0度、90度、180度、360度 35 { 36 { //0-0 37 {0, 1, 0, 0}, 38 {0, 1, 0, 0}, 39 {0, 1, 0, 0}, 40 {0, 1, 0, 0} 41 }, 42---------------------------------------------------------------------省略 43 { //6-3 44 {0, 1, 0, 0}, 45 {1, 1, 1, 0}, 46 {0, 1, 0, 0}, 47 {0, 0, 0, 0} 48 } 49 } 50 }; 51 52 @Override 53 protected void onCreate(Bundle savedInstanceState) { 54 super.onCreate(savedInstanceState); 55 setContentView(R.layout.activity_main); 56 57 blockoutput(); 58 } 59 60 protected void display() { //画面に表示 61 TextView textView = findViewById(R.id.puzzle); 62 screen = ""; 63 64 for (i = 0; i <= 20; i++) { 65 for (j = 0; j <= 12; j++) { 66 if (field[i][j] == 1) { 67 screen = screen + '■'; 68 } else if (field[i][j] == 0) { 69 screen = screen + '□'; 70 } else { 71 screen = screen + '\n'; 72 } 73 } 74 } 75 textView.setText(screen); 76 } 77 78 protected void blockoutput() { //7種のブロックをrandomに1つ取り出して表示 79 random = (int) (Math.random() * 10 % 6); 80 81 display(); 82 for (z = 0; z + 3 <= 20; z++) { 83 for (x = 0; x <= 3; x++) { 84 for (y = 0; y <= 3; y++) { 85 if (blocktype[random][0][x][y] == 1) { 86 field[z + x][4 + y] = blocktype[random][0][x][y]; 87 } 88 } 89 } 90 try { 91 Thread.sleep(500); 92 display(); 93 } catch (InterruptedException e) { 94 } 95 } 96 } 97}
###修正後
java
1import android.support.v7.app.AppCompatActivity; 2import android.os.Bundle; 3import android.widget.TextView; 4 5 6public class MainActivity extends AppCompatActivity { 7 String screen; 8 int random, i, j, x, y, z; 9 10 int field[][] = //パズルの中身 11 { // 0 1 2 3 4 5 6 7 8 9 10 11 12 12 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //0 13 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //1 14 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //2 15 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //3 16 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //4 17 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //5 18 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //6 19 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //7 20 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //8 21 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //9 22 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //10 23 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //11 24 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //12 25 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //13 26 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //14 27 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //15 28 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //16 29 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //17 30 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //18 31 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //19 32 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //20 33 }; 34 35 int blocktype[][][][] = { //ブロックタイプ 7種 回転 0度、90度、180度、360度 36 { 37 { //0-0 38 {0, 1, 0, 0}, 39 {0, 1, 0, 0}, 40 {0, 1, 0, 0}, 41 {0, 1, 0, 0} 42 }, 43 -------------------------------------省略 44 { //6-3 45 {0, 1, 0, 0}, 46 {1, 1, 1, 0}, 47 {0, 1, 0, 0}, 48 {0, 0, 0, 0} 49 } 50 } 51 }; 52 53 @Override 54 protected void onCreate(Bundle savedInstanceState) { 55 super.onCreate(savedInstanceState); 56 setContentView(R.layout.activity_main); 57 58 blockoutput(); 59 } 60 61 protected void display() { //画面に表示 62 TextView textView = findViewById(R.id.puzzle); 63 screen = ""; 64 65 for (i = 0; i <= 20; i++) { 66 for (j = 0; j <= 12; j++) { 67 if (field[i][j] == 1) { 68 screen = screen + '■'; 69 } else if (field[i][j] == 0) { 70 screen = screen + '□'; 71 } else { 72 screen = screen + '\n'; 73 } 74 } 75 } 76 textView.setText(screen); 77 } 78 79 protected void blockoutput() { //7種のブロックをrandomに1つ取り出して表示 80 random = (int) (Math.random() * 10 % 6); 81 time tm = new time(); 82 83 display(); 84 for (z = 0; z + 3 <= 20; z++) { 85 for (x = 0; x <= 3; x++) { 86 for (y = 0; y <= 3; y++) { 87 if (blocktype[random][0][x][y] == 1) { 88 field[z + x][4 + y] = blocktype[random][0][x][y]; 89 } 90 } 91 } 92 display(); 93 tm.start(); 94 for (x = 0; x <= 3; x++) { 95 for (y = 0; y <= 3; y++) { 96 field[z + x][4 + y] = 0; 97 } 98 } 99 } 100 } 101} 102 103 104class time extends Thread { 105 public void run() { 106 try { 107 Thread.sleep(500); 108 } catch (InterruptedException e) { 109 } 110 } 111}
回答2件
あなたの回答
tips
プレビュー