gpt4 book ai didi

java - 我在 susbstring 管理中遇到错误

转载 作者:行者123 更新时间:2023-11-30 04:56:29 25 4
gpt4 key购买 nike

我将创建一个带有 4 位数字的随机数,并要求用户输入 4 位数字。用户将尝试猜测。我正在尝试制作一个游戏,其中用户输入的任何数字与程序生成的任意数量的随机数相匹配,这将通知用户并要求用户删除并重新猜测该值用户输入。为此,我尝试将用substring方法输入的数字进行分解,检查数字是否匹配,但程序报错。有谁知道我做错了什么?这是我写的代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

public static final int MAX_NUMBER = 10;
public static final Random randomNumberOne = new Random();
public static final Random randomNumberTwo = new Random();
public static final Random randomNumberThree = new Random();
public static final Random randomNumberFour = new Random();
private TextView msgTvOne, msgTvTwo, msgTvThree, msgTvFour;
private EditText editText1;
private EditText editText2;
private EditText editText3;
private EditText editText4;
private Button button_onayla;
private int NumberToFindOne,NumberToFindTwo,NumberToFindThree,NumberToFindFour, numberTries;
private String s;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

msgTvOne = (TextView) findViewById(R.id.gameTxtViewOne);
editText1 = (EditText) findViewById(R.id.editText1);
button_onayla = (Button) findViewById(R.id.button_onayla);
button_onayla.setOnClickListener(this);

newGame();
}

private void newGame() {
NumberToFindOne = randomNumberOne.nextInt(10);
NumberToFindTwo = randomNumberTwo.nextInt(10);
NumberToFindThree = randomNumberThree.nextInt(10);
NumberToFindFour = randomNumberFour.nextInt(10);
msgTvOne.setText(R.string.begining_msg);
editText1.setText("");
numberTries = 0;
}


@Override
public void onClick(View view) {
if (view == button_onayla) {
button_onayla();
}
}

private void button_onayla() {
int nOne = Integer.parseInt(editText1.getText().toString());
numberTries++;
String subs = Integer.toString(nOne);
String sOne = subs.substring(1, 2);
String sTwo = subs.substring(2, 3);
String sThree = subs.substring(3, 4);
String sFour = subs.substring(4);

int iOne = Integer.parseInt(sOne);
int iTwo = Integer.parseInt(sTwo);
int iThree = Integer.parseInt(sThree);
int iFour = Integer.parseInt(sFour);

if (iOne == NumberToFindOne && iTwo == NumberToFindTwo && iThree == NumberToFindThree && iFour == NumberToFindFour) {
Toast.makeText(this, "Tebrikler, tahminin doğru! \n Deneme Sayısı: " + numberTries , Toast.LENGTH_SHORT).show();
}else if (iOne != NumberToFindOne && iTwo != NumberToFindTwo && iThree != NumberToFindThree && iFour != NumberToFindFour) {
msgTvOne.setText("Herhangi bir sayı eşleşmedi.");
editText1.getText().clear();
}else if (iOne == NumberToFindOne && iTwo == NumberToFindTwo && iThree == NumberToFindThree || iOne == NumberToFindOne && iTwo == NumberToFindTwo && iFour == NumberToFindFour
|| iTwo == NumberToFindTwo && iThree == NumberToFindThree && iFour == NumberToFindFour) {
msgTvOne.setText("Herhangi üç sayı eşleşti.");
editText1.getText().clear();
}else if (iOne != NumberToFindOne && iTwo != NumberToFindTwo && iThree != NumberToFindThree || iOne != NumberToFindOne && iTwo != NumberToFindTwo && iFour != NumberToFindFour
|| iTwo != NumberToFindTwo && iThree != NumberToFindThree && iFour != NumberToFindFour) {
msgTvOne.setText("Herhangi bir sayı eşleşmedi.");
editText1.getText().clear();
}else if (iOne == NumberToFindOne && iTwo == NumberToFindTwo || iOne == NumberToFindOne && iThree == NumberToFindThree
|| iOne == NumberToFindOne && iFour == NumberToFindFour || iTwo == NumberToFindTwo && iThree == NumberToFindThree
|| iTwo == NumberToFindTwo && iFour == NumberToFindFour || iThree == NumberToFindThree && iFour == NumberToFindFour){
msgTvOne.setText("Herhangi iki sayı eşleşti.");
editText1.getText().clear();
}else if (iOne != NumberToFindOne && iTwo != NumberToFindTwo || iOne != NumberToFindOne && iThree != NumberToFindThree
|| iOne != NumberToFindOne && iFour != NumberToFindFour || iTwo != NumberToFindTwo && iThree != NumberToFindThree
|| iTwo != NumberToFindTwo && iFour != NumberToFindFour || iThree != NumberToFindThree && iFour != NumberToFindFour){
msgTvOne.setText("Herhangi bir sayı eşleşmedi.");
editText1.getText().clear();
}else if (iOne == NumberToFindOne && iTwo != NumberToFindTwo && iThree != NumberToFindThree && iFour != NumberToFindFour) {
msgTvOne.setText("Herhangi bir sayı eşleşti.");
editText1.getText().clear();
}else if (iOne != NumberToFindOne && iTwo == NumberToFindTwo && iThree != NumberToFindThree && iFour != NumberToFindFour) {
msgTvOne.setText("Herhangi bir sayı eşleşti.");
editText1.getText().clear();
}else if (iOne != NumberToFindOne && iTwo != NumberToFindTwo && iThree == NumberToFindThree && iFour != NumberToFindFour) {
msgTvOne.setText("Herhangi bir sayı eşleşti.");
editText1.getText().clear();
}else if (iOne != NumberToFindOne && iTwo != NumberToFindTwo && iThree != NumberToFindThree && iFour == NumberToFindFour) {
msgTvOne.setText("Herhangi bir sayı eşleşti.");
editText1.getText().clear();
}else if (iOne != NumberToFindOne && iTwo != NumberToFindTwo && iThree != NumberToFindThree && iFour != NumberToFindFour) {
msgTvOne.setText("Herhangi bir sayı eşleşmedi.");
editText1.getText().clear();
}

}
}

最佳答案

这是您真正想要的(获得四位数)吗?

        String sOne = subs.substring(0, 1);
String sTwo = subs.substring(1, 2);
String sThree = subs.substring(2, 3);
String sFour = subs.substring(3);

关于java - 我在 susbstring 管理中遇到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59051732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com