第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

單擊時(shí)正確生成整數(shù)

單擊時(shí)正確生成整數(shù)

慕碼人2483693 2023-02-23 14:45:00
關(guān)于調(diào)用函數(shù)時(shí)生成的整數(shù)的代碼,我遇到了問(wèn)題 - 我的代碼是一個(gè)數(shù)學(xué)游戲,用戶(hù)可以用 1 到 12 的數(shù)字回答隨機(jī)算術(shù)問(wèn)題,我可以做到,但代碼似乎正在生成在允許用戶(hù)回答上一個(gè)問(wèn)題之前生成新的隨機(jī)值,這意味著用戶(hù)回答了屏幕上未顯示的下一個(gè)問(wèn)題。我認(rèn)為在我對(duì)特定行進(jìn)行評(píng)論的代碼中對(duì)其進(jìn)行了更好的描述。public class MainActivity extends AppCompatActivity {Random r = new Random();int points1 = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    int value1 = r.nextInt(12 - 1) + 1;             //1st randomized set of values    int value2 = r.nextInt(12 - 1) + 1;             // ^    TextView Number1 = findViewById(R.id.number1);    Number1.setText(""+value1);                            //assigns randomized values to TextViews    TextView Number2 = findViewById(R.id.number2);    Number2.setText(""+value2);                            // ^^}public void onSubmitCheck (View view) {    reset();                                               //When button Submit is clicked}       //^ ^ Issue starts here when we click "submit" - the code generates new random values before checking the old ones against the mathematical expression in line 50..        //..meaning the user is already answering to the SECOND question before even attempting the first one.public void reset(){    int value1 = r.nextInt(12 - 1) + 1;            //Assigns 2nd randomized values    int value2 = r.nextInt(12 - 1) + 1;            // ^        TextView Number1 = findViewById(R.id.number1);        Number1.setText("" + value1);        TextView Number2 = findViewById(R.id.number2);        Number2.setText("" + value2);    TextView Answer = findViewById(R.id.Answer);    TextView displayPoints = findViewById(R.id.points);    EditText Attempt = findViewById(R.id.Attempt);所以最后,我想要實(shí)現(xiàn)的是在用戶(hù)可以回答的創(chuàng)建 (onCreate) 上生成值,然后在用戶(hù)輸入和提交之后,應(yīng)用程序應(yīng)該生成新值并允許用戶(hù)再次回答。我并不期待為我完成完整的工作,而是可以引導(dǎo)我找到答案的想法,因?yàn)槲艺娴暮芟雽W(xué)習(xí)和理解我的問(wèn)題。我嘗試了不同的方法,但我想這只是我的經(jīng)驗(yàn)和知識(shí)阻礙了我找到解決方案——我是一個(gè)初學(xué)者和自學(xué)成才的人。
查看完整描述

2 回答

?
喵喔喔

TA貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個(gè)贊

使 value1 和 value2 成為全局值,并僅在將其與舊值進(jìn)行比較后才重置這些值。也做了一些其他的重新安排。


這可能是您的代碼:


public class MainActivity extends AppCompatActivity {



Random r = new Random();

int points1 = 0;

int value1, value2;

TextView Number1;

TextView Number2;

TextView Answer;

TextView displayPoints;

EditText Attempt;


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    Number1 = findViewById(R.id.number1);

    Number2 = findViewById(R.id.number2);

    Answer = findViewById(R.id.Answer);

    displayPoints = findViewById(R.id.points);

    Attempt = findViewById(R.id.Attempt);


    value1 = r.nextInt(12 - 1) + 1;             //1st randomized set of values

    value2 = r.nextInt(12 - 1) + 1;             // ^

    Number1.setText("" + value1);                            //assigns randomized values to textEdits

    Number2.setText("" + value2);                            // ^^


}


public void onSubmitCheck(View view) {

    reset();                                               //When button Submit is clicked


}       //^ ^ Issue starts here when we click "submit" - the code generates new random values before checking the old ones against the mathematical expression in line 50..

//..meaning the user is already answering to the SECOND question before even attempting the first one.


public void reset() {


    int userAnswer = Integer.parseInt(Attempt.getText().toString());    //compares inputted value to an answer from line 50

    if (userAnswer == value1 + value2) {                                    //LINE 50

        Answer.setText("Correct!");

        point();

        displayPoints.setText("" + points1);


    } else {

        Answer.setText("Wrong :( Answer was: " + (value1 + value2));

    }


    value1 = r.nextInt(12 - 1) + 1;            //Assigns 2nd randomized values

    value2 = r.nextInt(12 - 1) + 1;            // ^

    Number1.setText("" + value1);

    Number2.setText("" + value2);


查看完整回答
反對(duì) 回復(fù) 2023-02-23
?
蝴蝶刀刀

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個(gè)贊

static int points1 = 0; 


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    TextView Number1 = findViewById(R.id.number1);

    TextView Number2 = findViewById(R.id.number2);

    TextView Answer = findViewById(R.id.Answer);

    EditText Attempt = findViewById(R.id.Attempt);

    TextView displayPoints = findViewById(R.id.points);


    generateNumbers();


}


public void onSubmitCheck (View view) {

    checkResult();

    Attempt.clear();                                              


private void generateNumbers () {

    Random r = new Random();

    value1 = r.nextInt(12 - 1) + 1;

    value2 = r.nextInt(12 - 1) + 1;


    Number1.setText(""+value1);                   

    Number2.setText(""+value2);


}


void checkResult() {

    int userAnswer = Integer.parseInt(Attempt.getText().toString());   

    if (userAnswer == value1 + value2) {                 

        Answer.setText("Correct!");


        points1++;


        displayPoints.setText(""+ points1);


    } else {

        Answer.setText("Wrong :( Answer was: " + (value1 + value2));

    }

}

試試這個(gè),從一開(kāi)始這段代碼就可以工作。如果您使用另一個(gè)名為Next 的按鈕來(lái)生成下一個(gè) Value ,這將是一個(gè)更好的選擇。它可以幫助您理解代碼。


查看完整回答
反對(duì) 回復(fù) 2023-02-23
  • 2 回答
  • 0 關(guān)注
  • 108 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)