Android “显示”;“正确”;及;“不正确”;取决于答案

Android “显示”;“正确”;及;“不正确”;取决于答案,android,Android,如果答案正确,我想在按下一次键盘杂凑时显示“正确”,如果答案错误,则显示“不正确”,当再次按下键盘杂凑时,将转到下一个随机问题 我的XML的一部分是 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:lay

如果答案正确,我想在按下一次键盘杂凑时显示“正确”,如果答案错误,则显示“不正确”,当再次按下键盘杂凑时,将转到下一个随机问题

我的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:id="@+id/Title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:textSize="25dp" />

    <TextView
        android:id="@+id/Guess"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:text="Guess: "
        android:textSize="25dp" />

    <TextView
        android:id="@+id/Answer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:textSize="25dp" />

    <TextView
        android:id="@+id/Incorrect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

    <TextView
        android:id="@+id/CORRECT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="40dp"/>

Java代码是

package org.example.question;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class QuestionActivity extends Activity implements View.OnClickListener {
    /** Called when the activity is first created. */

    //variable for different questions
    int fnum0, snum0,fnum1, snum1,fnum2, snum2,fnum3, snum3,
    fnum4, snum4,fnum5, snum5,fnum6, snum6,fnum7, snum7,
    fnum8, snum8,fnum9, snum9, answer;

    //Variable and type declaration for buttons and text
    Button keyOne;
    Button keyTwo;
    Button keyThree;
    Button keyFour;
    Button keyFive;
    Button keySix;
    Button keySeven;
    Button keyEight;
    Button keyNine;
    Button keyDel;
    Button keyZero;
    Button keyHash;
    Button keySubtract;
    TextView display;
    TextView display1;
    TextView answer0;
    int q=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Display text on screen

        display1 = (TextView)findViewById(R.id.Guess);
        display = (TextView)findViewById(R.id.Title);
        answer0 = (TextView)findViewById(R.id.Answer);

        //Code for correct and incorrect

        //Assigning names to each keypad
        keyOne= (Button) findViewById(R.id.keypad_1);
        keyTwo= (Button) findViewById(R.id.keypad_2);
        keyThree= (Button) findViewById(R.id.keypad_3);
        keyFour= (Button) findViewById(R.id.keypad_4);
        keyFive = (Button) findViewById(R.id.keypad_5);
        keySix= (Button) findViewById(R.id.keypad_6);
        keySeven = (Button) findViewById(R.id.keypad_7);
        keyEight = (Button) findViewById(R.id.keypad_8);
        keyNine = (Button) findViewById(R.id.keypad_9);
        keyZero = (Button) findViewById(R.id.keypad_0);
        keySubtract = (Button) findViewById(R.id.keypad_subtract);
        keyHash = (Button) findViewById(R.id.keypad_hash);
        keyDel = (Button) findViewById(R.id.delete);

        //setting button to produce an event when each button is pressed
        keyOne.setOnClickListener(this);     keyTwo.setOnClickListener(this);     keyThree.setOnClickListener(this);
        keyFour.setOnClickListener(this);     keyFive.setOnClickListener(this);     keySix.setOnClickListener(this);
        keySeven.setOnClickListener(this);      keyEight.setOnClickListener(this);  keyNine.setOnClickListener(this);
        keySubtract.setOnClickListener(this);    keyHash.setOnClickListener(this);  keyDel.setOnClickListener(this);
    }

    public void onClick(View arg0) {
        //Created a new string object
        String str = new String();

        switch (arg0.getId()) {

            //what happens when hash button is pressed

            case R.id.keypad_hash:
            {
                //Generates random numbers

                fnum0 = (int) ((double) ((Math.random() * 10)));
                snum0 = (int) ((double) ((Math.random() * 10)));

                //Generates a random number between 0 to 9 with random operators for first and sceond number.
                int operation = (int) ((double) ((Math.random() * 10)));

                /* Generate random expression with random operators and display "?" if the answer is one digit
                 * or"??" if the answer is greater than 10.
                 */
                if(operation == 0)
                    str = fnum0+  "+"  +  snum0+  "=" + ((fnum0+snum0<10)? "?" : "??");
                if("Correct".equals(display.getText().toString()))
                {
                    display.setText("Correct");
                }
                else
                {
                    display.setText("Incorrect");
                }

                if(operation == 1)
                    str = fnum0 +  "-"  +  snum0+  "=" + ((fnum0-snum0<10)? "?" : "??");
                if("Correct".equals(display.getText().toString()))
                {
                    display.setText("Correct");
                }
                else
                {
                    display.setText("Incorrect");
                }
                if(operation == 2)
                   str = fnum0 +  "*"  +  snum0+  "=" +  ((fnum0*snum0<10)? "?" : "??");
                if("Correct".equals(display.getText().toString()))
                {
                    display.setText("Correct");
                }
                else
                {
                    display.setText("Incorrect");
                }
                if("Correct".equals(display.getText().toString()))
                {
                    display.setText("Correct");
                }
                else
                {
                    display.setText("Incorrect");
                }
                str = fnum0 +  "/"  +  snum0+  "=" +  ((fnum0/snum0<10)? "?" : "??");
                if("Correct".equals(display.getText().toString()))
                {
                    display.setText("Correct");
                }
                else
                {
                    display.setText("Incorrect");
                }
                display.setText(str);
                break;
            }

            case R.id.keypad_1:
                String str1 = display.getText().toString();
                display.setText(str1.replace("?", "1"));
                break;

            case R.id.keypad_2:
                String str2 = display.getText().toString();
                display.setText(str2.replace("?", "2"));
                break;

            case R.id.keypad_3:
                String str3 = display.getText().toString();
                display.setText(str3.replace("?", "3"));
                break;

            case R.id.keypad_4:
                String str4 = display.getText().toString();
                display.setText(str4.replace("?", "4"));
                break;

            case R.id.keypad_5:
                String str5 = display.getText().toString();
                display.setText(str5.replace("?", "5"));
                break;

            case R.id.keypad_6:
                String str6 = display.getText().toString();
                display.setText(str6.replace("?", "6"));
                break;

            case R.id.keypad_7:
                String str7 = display.getText().toString();
                display.setText(str7.replace("?", "7"));
                break;

            case R.id.keypad_8:
                String str8 = display.getText().toString();
                display.setText(str8.replace("?", "8"));
                break;

            case R.id.keypad_9:
                String str9 = display.getText().toString();
                display.setText(str9.replace("?", "9"));
                break;

            case R.id.delete:
                break;

            case R.id.keypad_0:
                String str0 = display.getText().toString();
                display.setText(str0.replace("?", "0"));
                break;

            case R.id.keypad_subtract:
                display.setText("-");
                break;
        }
    }

    public void requestFocus() {
        // TODO Auto-generated method stub
    }
}
package org.example.question;
导入java.util.Random;
导入android.app.Activity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
公共类QuestionActivity扩展活动实现View.OnClickListener{
/**在首次创建活动时调用*/
//不同问题的变量
int fnum0、snum0、fnum1、snum1、fnum2、snum2、fnum3、snum3、,
fnum4、fnum5、fnum6、fnum4、fnum5、fnum6、fnum7、snum7、,
fnum8,snum8,fnum9,snum9,回答;
//按钮和文本的变量和类型声明
按键式;
按键二;
按键三;
按键四;
按键五;
按钮键;
按键均匀;
按键八;
按键九;
按钮键;
按键归零;
按钮键散列;
按键子轨迹;
文本视图显示;
文本视图显示1;
文本视图回答0;
int q=0;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//在屏幕上显示文本
display1=(TextView)findViewById(R.id.Guess);
display=(TextView)findViewById(R.id.Title);
answer0=(TextView)findViewById(R.id.Answer);
//正确和不正确的代码
//为每个键盘分配名称
keyOne=(按钮)findViewById(R.id.keypad_1);
keyTwo=(按钮)findViewById(R.id.keypad_2);
按键三=(按钮)findViewById(R.id.keypad_3);
keyFour=(按钮)findViewById(R.id.keypad_4);
keyFive=(按钮)findViewById(R.id.keypad_5);
keySix=(按钮)findViewById(R.id.keypad_6);
按键偶数=(按钮)findViewById(R.id.keypad_7);
keyEight=(按钮)findViewById(R.id.keypad_8);
keyNine=(按钮)findViewById(R.id.keypad_9);
keyZero=(按钮)findViewById(R.id.keypad_0);
keySubtract=(按钮)findViewById(R.id.keypad\U减法);
keyHash=(按钮)findViewById(R.id.keypad\u散列);
keyDel=(按钮)findViewById(R.id.delete);
//设置按钮以在按下每个按钮时生成事件
keyOne.setOnClickListener(此);keyTwo.setOnClickListener(此);keyThree.setOnClickListener(此);
keyFour.setOnClickListener(此);keyFive.setOnClickListener(此);keySix.setOnClickListener(此);
keySeven.setOnClickListener(此);keyEight.setOnClickListener(此);keyNine.setOnClickListener(此);
keySubtract.setOnClickListener(此);keyHash.setOnClickListener(此);keyDel.setOnClickListener(此);
}
公共void onClick(视图arg0){
//创建了一个新的字符串对象
String str=新字符串();
开关(arg0.getId()){
//当按下哈希按钮时会发生什么
案例R.id.keypad\u散列:
{
//生成随机数
fnum0=(int)((double)((Math.random()*10));
snum0=(int)((double)((Math.random()*10));
//使用第一个和第二个数字的随机运算符生成0到9之间的随机数。
int运算=(int)((double)((Math.random()*10));
/*使用随机运算符生成随机表达式,如果答案是一位数字,则显示“?”
*如果答案大于10,则为“?”。
*/
如果(操作==0)

str=fnum0+“+”+snum0+“=”+((fnum0+snum0根据操作,将代码行替换为:

str = fnum0+  "+"  +  snum0+  "=" + ((fnum0+snum0<10) ? "?" : "??")

计算两个随机数和运算后,需要计算答案。然后创建一个新的字符串变量。如果答案为10或更高,请将其设置为“?”,否则将其设置为“?”。然后将此变量附加到字符串末尾,而不是“?”你已经硬编码了。

我有点困惑,似乎你把它设置为??不管怎样,所以这就是显示的内容。yh..但我需要显示“?”如果随机表达式的答案是一位数字,如果两位数字,我尝试了前两行代码,它只显示“?”或“?”在屏幕上,表情没有显示是的,你必须自己添加它..哈哈..刚刚编辑了我的答案,因为你看起来真的很懒;-)looool…从那以后一直在重新搜索这个问题,但没有线索…几乎放弃了,但你激励了我;-)…谢谢伙计!你能告诉我如何显示“正确”吗当只按一次键盘时,我的方式不起作用。我将向你展示代码的那部分请,因为你完全修改了你的问题,答案没有任何意义,人们会认为我的答案是疯狂的!请回到旧问题并创建一个新问题!简短回答:你知道你在写什么吗我们的if语句?case R.id.keypad\u hash:后面的代码是开始一个新的等式,所以这是一个逻辑行为!
str = fnum0+  "*"  *  snum0+  "=" + ((fnum0*snum0<10) ? "?" : "??")
int a = ( b ? c : d );
// is shorthand for:
if ( b )
{
    a = c;
}
else
{
    a = d;
}