
package com.xiangqiao.guess.activity; |
import java.util.Random; |
import android.R.integer; |
import android.app.Activity; |
import android.content.Intent; |
import android.os.Bundle; |
import android.widget.TextView; |
import android.widget.Toast; |
public class ResultActivity extends Activity { |
|
private TextView tView; |
@Override |
protected void onCreate(Bundle savedInstanceState) { |
// TODO Auto-generated method stub |
super.onCreate(savedInstanceState); |
setContentView(R.layout.result_layout); |
|
tView=(TextView) findViewById(R.id.result); |
|
//得到人出的拳 |
Intent intent=getIntent(); |
Bundle bundle=intent.getExtras(); |
int ren=bundle.getInt("quan"); |
|
//电脑出的拳 |
Random random=new Random(); |
int cpu=random.nextInt(3-1)+1; |
|
//Toast.makeText(ResultActivity.this, "人: "+ ren, Toast.LENGTH_LONG).show(); |
// Toast.makeText(ResultActivity.this, "电脑: "+ cpu, Toast.LENGTH_LONG).show(); |
//比较结果 得出胜负 |
String result=compare(ren ,cpu); |
tView.setText(result); |
} |
|
private String compare(int ren ,int cpu){ |
StringBuffer sbf=new StringBuffer("结果:\n"+"人 "+convert(ren)+" VS "+convert(cpu)+" 电脑\n"); |
if (ren==cpu) { |
sbf.append("平局,再接再励!"); |
}else if(ren==1 && cpu==3){ |
sbf.append("电脑胜,孤独求败!"); |
}else if(ren==3 && cpu==1){ |
sbf.append("人胜,孤独求败!"); |
}else if(ren<cpu){ |
sbf.append("人胜,孤独求败!"); |
}else if(ren>cpu){ |
sbf.append("电脑胜,孤独求败!"); |
} |
return sbf.toString(); |
} |
|
private String convert(int id){ |
|
switch (id) { |
case 1: |
return "石头"; |
case 2: |
return "剪刀"; |
case 3: |
return "布"; |
default: |
break; |
} |
return null; |
} |
} |




初级程序员
by: Essence 发表于:2015-06-04 11:18:42 顶(0) | 踩(0) 回复
回复评论