查看完整版本: 請問android studio的猜數字AB的升級版要怎麼寫
頁: [1]

fto123 發表於 2024-4-5 11:37 PM

請問android studio的猜數字AB的升級版要怎麼寫

各位學長 大家好
小弟這學期修了android studio
老師出作業要寫猜數字
雖然參考網路上的分享與同學的幫忙寫完AB邏輯


但是老師又出了一個升級版,要加算C


作業預設4個數字
新邏輯是要先計算生成的隨機數是否有重複值
若有重複值,就要拿手動輸入的值來比對


若手動輸入值與重複值相等,則C+1,若無,則C=0
且每一個手動輸入值都要算


但是想不出邏輯要怎麼解,請各位學長幫忙


謝謝
MainActivity.java如下package com.example.ch5_2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class MainActivity extends AppCompatActivity
{
        private int systemA, systemB, systemC, systemD;
        private String strA, strB, strC, strD, resault;
        Button submit;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getNum();
        submit = (Button) this.findViewById(R.id.button);
        submit.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                EditText text = (EditText) findViewById(R.id.txtTemp);
                TextView tv = (TextView) findViewById(R.id.lblOutput);
                TextView output2 = (TextView) findViewById(R.id.lblOutput2);

                String myA, myB, myC, myD;
                int a = 0;
                int b = 0;
//                int C = 0;
                myA = text.getText().toString().substring(0, 1);
                myB = text.getText().toString().substring(1, 2);
                myC = text.getText().toString().substring(2, 3);
                myD = text.getText().toString().substring(3, 4);

//                if (myA.equals(myB) || myB.equals(myC) || myC.equals(myA)) {
//                    resault="輸入錯誤,請輸入三個不重複的數字";
//                    tv.setText(resault);
//                    text.setText("");
//                } else {
                if (myA.equals(strA) && myB.equals(strB) && myC.equals(strC) && myD.equals(strD))
                {
                    tv.setText("Input的數字:" + myA + myB + myC + myD + "  恭喜你答對囉~答案 : " + strA + strB + strC+ strD);
                } else {
                    if (myA.equals(strA)) {
                        a += 1;
                    } else if (myA.equals(strB)) {
                        b += 1;
                    } else if (myA.equals(strC)) {
                        b += 1;
                    } else if (myA.equals(strD)) {
                        b += 1;
                    }
                    if (myB.equals(strB)) {
                        a += 1;
                    } else if (myB.equals(strA)) {
                        b += 1;
                    } else if (myB.equals(strC)) {
                        b += 1;
                    } else if (myB.equals(strD)) {
                        b += 1;
                    }
                    if (myC.equals(strC)) {
                        a += 1;
                    } else if (myC.equals(strA)) {
                        b += 1;
                    } else if (myC.equals(strB)) {
                        b += 1;
                    } else if (myC.equals(strD)) {
                        b += 1;
                    }
                    if (myD.equals(strD)) {
                        a += 1;
                    } else if (myD.equals(strA)) {
                        b += 1;
                    } else if (myD.equals(strB)) {
                        b += 1;
                    } else if (myD.equals(strC)) {
                        b += 1;
                    }
                    resault = "Input的數字:" + myA + myB + myC + myD + "=" + a + "A"+ b + "B" ;
//                    + c "C";
                    tv.setText(resault);
                    output2.setText((tv.getText()  + "\n") + output2.getText());
                    text.setText("");
// 清空輸入框


                }
            }
        });

    }

public void getNum() {
        systemA = (int) (Math.random() * 10);
        systemB = (int) (Math.random() * 10);
        systemC = (int) (Math.random() * 10);
        systemD = (int) (Math.random() * 10);
//        if (systemB == systemA) {
//        systemB = (int) (Math.random() * 10);
//        }
//        if (systemC == systemB || systemC == systemA) {
//        systemC = (int) (Math.random() * 10);
//        }
        strA = String.valueOf(systemA);
        strB = String.valueOf(systemB);
        strC = String.valueOf(systemC);
        strD = String.valueOf(systemD);
        }
        }



...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div><div></div>
頁: [1]