【发布时间】:2017-01-20 22:27:14
【问题描述】:
我正在构建一个算命先生安卓应用程序,它使用随机类从数组中选择不同的财富,它基于多态性。我知道我编写得很好,但我的应用程序甚至无法打开,有什么帮助吗?这是我的代码:
public class fortuneList {
Random good = new Random();
Random bad = new Random();
public String getGood() {
String goodFortunes[] = new String[20];
int sel = 0;
for (int i = 0; i < 9; i++) {
sel = good.nextInt();
}
goodFortunes[0] = "Adel is awesome";
goodFortunes[1] = "Adel is cool";
goodFortunes[2] = "Adel is nice";
goodFortunes[3] = "Adel is gentle";
goodFortunes[4] = "Adel is smart";
goodFortunes[5] = "Adel is rich";
goodFortunes[6] = "Adel is good";
goodFortunes[7] = "This is 8";
goodFortunes[8] = "Change the quotes later";
return goodFortunes[sel];
}
public String getBad() {
String badFortunes[] = new String[5];
int sele = 0;
for (int is = 0; is < 5; is++) {
sele = bad.nextInt();
}
badFortunes[0] = "WTF";
badFortunes[1] = "Bad 1";
badFortunes[2] = "Nigga";
badFortunes[3] = "bish";
badFortunes[4] = "haha";
return badFortunes[sele];
}
}
这是我的主要活动代码:
public class MainActivity extends AppCompatActivity {
TextView fortuneText;
Random select = new Random();
ImageView ball;
Button button;
Drawable blue = getResources().getDrawable(R.drawable.blue);
Drawable red = getResources().getDrawable(R.drawable.red);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ball = (ImageView) findViewById(R.id.ball);
button = (Button) findViewById(R.id.button);
fortuneText = (TextView) findViewById(R.id.fortuneText);
}
public void getFortune(View view){
fortuneList obj = new fortuneList();
String fortuneType[]={obj.getGood(),obj.getBad()};
int fortuneSel = 0;
for(int i =0; i<2;i++){
fortuneSel = select.nextInt();
}
fortuneText.setText(fortuneType[fortuneSel]);
if (fortuneSel == 0) {
ball.setImageDrawable(blue);
}
if(fortuneSel == 1){
ball.setImageDrawable(red);
}
}
}
【问题讨论】:
-
你能发布堆栈跟踪吗?
标签: java android random polymorphism