我正在开发一款代码破解游戏,即Android中的Bulls and Cows。问题是在我的主类中我应用了一个逻辑来检查数字。这个逻辑属于循环,但是当我运行应用程序时,它会在进入循环后冻结。我厌倦了在互联网上搜索答案。如果你能以某种方式提供帮助,我将非常感谢你们。我很遗憾我的代码看起来很蹩脚,因为我是android编程的初学者。 以下是我的主类的代码。 : -
package com.bullsncows.bnc;
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;
}
}
以下是同一页面的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>
答案 0 :(得分:5)
没有阅读所有代码,我似乎不太可能
else if(bulls == 4)
将永远评估为真,因为每次迭代都会重置公牛
int bulls = 0;
你只有四次尝试:
for(int i= 0;i < 4;i++)
自
else if(bulls == 4){
guessed = true;
break;
是你唯一的终止条件,你永远循环。
答案 1 :(得分:0)
如果您可以将代码范围缩小到导致问题有用的区域。但我可能会猜测有错误的循环是
while(n > 0){
n/= 10;
}
n可能永远不会达到0.您是否可以单步执行代码并确切地确定哪个部分失败了?