They ask me to build a pyramid with the sign '%' with threads, as requests I am asked to enter the number of signs to show and it should appear in this way consecutively:
a. You must request how many lines the pyramid wants.
b. When you press the button you must create the pyramid with the following character "%"
c. You must print each character ("%") with an interval of 1 second.
d. Example
%
%%
%%%
%%%%
%%%%%
%%%%%%
%%%%%%%
%%%%%%%%
%%%%%%%%%
e. In the previous example, a pyramid of 9 lines was painted, each line increasing by 1 in 1 character.
If someone can give me an orientation that I should correct, I would appreciate it.
public class Hilo extends Thread {
MainActivity objetointerfaz;
EditText txt_cant, txt_mostrar;
Button btn_mostrar;
int cant , i, j;
public Hilo (MainActivity objeto) {
objetointerfaz = objeto;
}
@Override
public void run() {
txt_cant = objetointerfaz.findViewById(R.id.txt_cant);
txt_mostrar = objetointerfaz.findViewById(R.id.txt_mostrar);
cant = Integer.parseInt(txt_cant.getText().toString());
objetointerfaz.runOnUiThread(new Runnable() {
@Override
public void run() {
for (i = 0; i < cant; i++) {
for (j = 0; j < i + 1; j++){
txt_mostrar.setText("%");
}
}
}
});
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class MainActivity extends AppCompatActivity {
Hilo h;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Iniciar(View v){
h = new Hilo(this);
h.start();
}
}