Android 我的应用程序在进入循环后冻结
我正在开发一款破解密码的游戏,即android中的公牛和奶牛。问题是,在我的主课上,我应用了逻辑来检查数字。此逻辑属于循环,但当我运行应用程序时,它在进入循环后冻结。我厌倦了在网上搜索答案。如果你们能以某种方式提供帮助,我将非常感谢你们。如果你觉得我的代码很蹩脚,我很遗憾,因为我是android编程的初学者。 以下是我的主课代码: 包com.bullsncows.bncAndroid 我的应用程序在进入循环后冻结,android,Android,我正在开发一款破解密码的游戏,即android中的公牛和奶牛。问题是,在我的主课上,我应用了逻辑来检查数字。此逻辑属于循环,但当我运行应用程序时,它在进入循环后冻结。我厌倦了在网上搜索答案。如果你们能以某种方式提供帮助,我将非常感谢你们。如果你觉得我的代码很蹩脚,我很遗憾,因为我是android编程的初学者。 以下是我的主课代码: 包com.bullsncows.bnc import java.util.Random; import android.app.Activity; import an
import java.util.Random;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Startingpoint extends Activity {
EditText etn1, etn2, etn3, etn4;
Button bsub;
TextView errormsg,res;
Random r = new Random();
int num = 0;
boolean guessed = false;
int count =0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
initializevar();
// making the computer select a random four digit number
while(hasDupes(num= (r.nextInt(9000) + 1000)));
// on clicking the submit button
bsub.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String n1 = etn1.getText().toString();
String n2 = etn2.getText().toString();
String n3 = etn3.getText().toString();
String n4 = etn4.getText().toString();
String cnum = num+"";
if (n1.length()==0||n2.length()==0||n3.length()==0||n4.length()==0) {
errormsg.setText("Fields cannot be left blank");
errormsg.setTextColor(Color.RED);
errormsg.setGravity(Gravity.CENTER);
} else if (n1.equals(n2) || n1.equals(n3) || n1.equals(n4)
|| n2.equals(n3) || n2.equals(n4) || n3.equals(n4)) {
errormsg.setText("Please enter distinct number");
errormsg.setTextColor(Color.RED);
errormsg.setGravity(Gravity.CENTER);
}else{
String guess = n1+n2+n3+n4;
errormsg.setText("Correct "+ cnum + " "+ guess);
errormsg.setTextColor(Color.GREEN);
errormsg.setGravity(Gravity.CENTER);
do{
int bulls = 0;
int cows = 0;
count++;
for(int i= 0;i < 4;i++){
if(guess.charAt(i) == cnum.charAt(i)){
bulls++;
}else if(cnum.contains(guess.charAt(i)+"")){
cows++;
}
else if(bulls == 4){
guessed = true;
break;
}else{
res.setText(cows+" Cows and "+bulls+" Bulls.");
res.setTextColor(Color.BLUE);
res.setGravity(Gravity.CENTER);
}
}
}while(!guessed);
errormsg.setText("You won after "+count+" guesses!");
errormsg.setTextColor(Color.MAGENTA);
errormsg.setGravity(Gravity.CENTER);
}
}
});
}
private void initializevar() {
// TODO Auto-generated method stub
etn1 = (EditText) findViewById(R.id.etnum1);
etn2 = (EditText) findViewById(R.id.etnum2);
etn3 = (EditText) findViewById(R.id.etnum3);
etn4 = (EditText) findViewById(R.id.etnum4);
bsub = (Button) findViewById(R.id.bsubmit);
errormsg = (TextView) findViewById(R.id.tverror);
res = (TextView) findViewById(R.id.tvres);
}
public static boolean hasDupes(int n){
boolean[] digs = new boolean[10];
while(n > 0){
if(digs[n%10]) return true;
digs[n%10] = true;
n/= 10;
}
return false;
}
}
import java.util.Random;
导入android.app.Activity;
导入android.graphics.Color;
导入android.os.Bundle;
导入android.view.Gravity;
导入android.view.view;
导入android.view.Window;
导入android.view.WindowManager;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
公共类启动点扩展活动{
编辑文本etn1、etn2、etn3、etn4;
按钮bsub;
TextView errormsg,res;
随机r=新随机();
int num=0;
布尔猜测=假;
整数计数=0;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,
WindowManager.LayoutParams.FLAG(全屏);
setContentView(R.layout.main);
initializevar();
//让计算机随机选择一个四位数的数字
而(hasDupes(num=(r.nextInt(9000)+1000));
//单击“提交”按钮时
b sub.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
//TODO自动生成的方法存根
字符串n1=etn1.getText().toString();
字符串n2=etn2.getText().toString();
字符串n3=etn3.getText().toString();
字符串n4=etn4.getText().toString();
字符串cnum=num+“”;
如果(n1.length()==0 | | n2.length()==0 | | n3.length()==0 | | n4.length()==0){
errormsg.setText(“字段不能为空”);
errormsg.setTextColor(Color.RED);
errormsg.setGravity(重心);
}如果(n1.等于(n2)| | n1.等于(n3)| | n1.等于(n4)
||n2.等于(n3)| | n2.等于(n4)| | n3.等于(n4)){
errormsg.setText(“请输入不同的数字”);
errormsg.setTextColor(Color.RED);
errormsg.setGravity(重心);
}否则{
字符串猜测=n1+n2+n3+n4;
errormsg.setText(“更正”+cnum+“”+guess);
errormsg.setTextColor(Color.GREEN);
errormsg.setGravity(重心);
做{
int=0;
int=0;
计数++;
对于(int i=0;i<4;i++){
如果(猜测字符(i)=cnum字符(i)){
公牛++;
}else if(cnum.contains(guess.charAt(i)+“”){
奶牛++;
}
否则如果(多头==4){
猜测=正确;
打破
}否则{
res.setText(cows+“cows and”+公牛+“公牛”);
res.setTextColor(Color.BLUE);
res.setGravity(重心);
}
}
}而(!猜测);
errormsg.setText(“你在“+count+”猜之后赢了!”);
errormsg.setTextColor(颜色为洋红色);
errormsg.setGravity(重心);
}
}
});
}
私有void initializevar(){
//TODO自动生成的方法存根
etn1=(EditText)findViewById(R.id.etnum1);
etn2=(EditText)findViewById(R.id.etnum2);
etn3=(EditText)findViewById(R.id.etnum3);
etn4=(EditText)findViewById(R.id.etnum4);
bsub=(按钮)findViewById(R.id.bsubmit);
errormsg=(TextView)findViewById(R.id.tverror);
res=(TextView)findViewById(R.id.tvres);
}
公共静态布尔hasDupes(int n){
布尔值[]digs=新布尔值[10];
而(n>0){
if(digs[n%10])返回true;
digs[n%10]=真;
n/=10;
}
返回false;
}
}
以下是同一页面的XML编码:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Please select the numbers below" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<EditText
android:id="@+id/etnum1"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLength="1" />
<EditText
android:id="@+id/etnum2"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLength="1" />
<EditText
android:id="@+id/etnum3"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLength="1" />
<EditText
android:id="@+id/etnum4"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLength="1" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:id="@+id/tverror"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<TextView android:id="@+id/tvres"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/bsubmit"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_gravity="fill_vertical"
android:text="Submit" />
</LinearLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</TableLayout>
</LinearLayout>
如果您可以将代码范围缩小到只会导致问题的区域,则会有所帮助。但我可能猜有错误的循环是
while(n > 0){
n/= 10;
}
n可能永远不会达到0。你能一步一步地检查你的代码并准确地找出哪个部分出了故障吗?如果你能把代码的范围缩小到只会导致问题的区域,这会有所帮助。但我可能猜有错误的循环是
while(n > 0){
n/= 10;
}
n可能永远不会达到0。你能一步一步地检查你的代码并准确地找出哪个部分失败了吗?如果不阅读你所有的代码,我似乎不太可能
else if(bulls == 4)
将永远求值为true,因为每次迭代都会重置
int bulls = 0;
您只有四次尝试:
for(int i= 0;i < 4;i++)
是您唯一的终止条件,您将永远循环。如果不阅读所有代码,我似乎不太可能
else if(bulls == 4)
将永远求值为true,因为每次迭代都会重置
int bulls = 0;
您只有四次尝试:
for(int i= 0;i < 4;i++)
是您唯一的终止条件,您将永远循环。看起来很重,将逻辑放在单独的线程中。while循环中的一些打印语句将帮助您解决此类问题看起来很重,将逻辑放在单独的线程中。while循环中的一些打印语句将帮助您解决此类问题