我创建了随机表达式,想要将他们的答案链接到屏幕上的键盘,这样如果randon问题是3 + 2 =?然后当我按下keypad_five然后它会在屏幕上显示数字5,如3 + 2 = 5。
我的.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/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/Title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
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"/>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keypad"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*" >
<TableRow>
<Button android:id="@+id/keypad_1"
android:text="1" >
</Button>
<Button android:id="@+id/keypad_2"
android:text="2" >
</Button>
<Button android:id="@+id/keypad_3"
android:text="3" >
</Button>
</TableRow>
<TableRow>
<Button android:id="@+id/keypad_4"
android:text="4" >
</Button>
<Button android:id="@+id/keypad_5"
android:text="5" >
</Button>
<Button android:id="@+id/keypad_6"
android:text="6" >
</Button>
</TableRow>
<TableRow>
<Button android:id="@+id/keypad_7"
android:text="7" >
</Button>
<Button android:id="@+id/keypad_8"
android:text="8" >
</Button>
<Button android:id="@+id/keypad_9"
android:text="9" >
</Button>
</TableRow>
<TableRow>
<Button android:id="@+id/delete"
android:text="DEL" >
</Button>
<Button android:id="@+id/keypad_0"
android:text="0" >
</Button>
<Button android:id="@+id/keypad_hash"
android:text="#" >
</Button>
<Button android:id="@+id/keypad_subtract"
android:text="-" >
</Button>
</TableRow>
</TableLayout>
<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"/>
</LinearLayout>
.Java代码
package org.example.question;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
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 question=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) {
switch(arg0.getId()){
case R.id.keypad_hash:
//Generates random numbers
fnum0 = (int) ((double) ((Math.random() * 10)));
snum0 = (int) ((double) ((Math.random() * 10)));
String str = "";
//genrates random number between 0 to 9
int operation = (int) ((double) ((Math.random() * 10)));
if(operation == 0)
str = fnum0+ "+" + snum0+ "=";
if("CORRECT".equals(display.getText().toString()))
else if(operation == 1)
str = fnum0 + "-" + snum0+ "=";
else if(operation == 2)
str = fnum0 + "*" + snum0+ "=";
else
str = fnum0 + "/" + snum0+ "=";
display.setText(str);
break;
}
}
public void requestFocus() {
// TODO Auto-generated method stub
}
}
但是当我插入比“正确”行时,问题不会出现在答案中
答案 0 :(得分:0)
首先,你不需要额外的演员......这应该做:
fnum0 = (int) (Math.random() * 10);
其次,使用Log
类,以便您可以轻松找到operation
变量在您点击按钮时的值:
只需使用Log.d("debug", "operation value: " + operation);
,然后让Eclipse导入Log类。转到“窗口&gt;打开视图&gt;其他...”并选择“LogCat”以查找您正在生成的日志。
可能这个变量有问题所以str
变量没有被正确填充。
此外,该行
if("CORRECT".equals(display.getText().toString()))
缺乏声明(更不用说分号)了。此外,请记住,如果在if或else子句之后有超过1个语句,则必须使用括号。无论如何,这条线路是什么?
另外,
//genrates random number between 0 to 9
int operation = (int) ((double) ((Math.random() * 10)));
它生成0到 10 之间的随机数,因为Math.random在[0; 1]区间内:)
让我知道各自的变量值是什么,并确保修复您的语法,也许我能够更好地帮助您。