Edit #1
评论中有人建议使用Math.floor()
和intValue()
。如何实现到我的代码中?仍然可以在安卓上运行该程序吗?
我想实现什么目标?
我正在尝试创建与Android兼容的Java代码(目的在代码中的注释中解释(。我是Java的新手,仍然不知道(可能是基本的(像文本框这样的东西。<小时 />
我有什么?
现在我有代码:
- 根据数组定义的整数范围(需要返工;在下面的
What do I need?
中解释(和z
(答案相等变量(生成x
和y
,这些范围将用于将输入与实际答案进行比较。 - 将输入(变量
answer
(与实际答案(变量z
(进行比较的代码
<小时 />
我需要什么?
每个需要的代码段都在代码的注释中进行了解释,但这里也有一个列表:
- 文本框(整数输入(,用户必须在其中将答案输入 x*y(等于 z(。输入应为变量"答案">
- 基于数组的随机化
x
和y
的代码
<小时 />
法典
public class numGen {
public static void main(String[] args) {
int[] firstNum = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int x = firstNum[/*Here program should put a random number out of firstNum array*/];
int[] secondNum = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int y = secondNum[/*Here program should put a random number out of secondNum array*/];
int z = x*y;
/*
Here I'm looking to insert textbox (integer input), in which user has to put answer to x*y (which is equal to z). Input should be a variable 'answer'
*/
/*
And that's the code comparing textbox input with z variable:
*/
if (answer == z);{
System.out.println("Correct."); // and after good answer program should repeat with new x and y values...
} else {
System.out.println("Incorrect. Try again."); // ...and here not.
}
System.out.println(x + " * " + y);
}
}
<小时 />如果你想在安卓中做到这一点,那么你不能使用main方法,你需要去一个单一的活动安卓应用程序。 特别是对于您的问题,即.
这里的程序应该从firstNum数组
中放一个随机数,使用Random((对象来获取随机整数
其他相同
在这里,我希望插入文本框(整数输入(,其中用户必须将答案输入 x*y(等于 z(。输入应为变量"答案">
为此,您需要一个 EditText 并使用int answer = Integer.parseInt((editText.getText().toString()))
提取
类型化的整数
这是一个包含一项活动的完整解决方案。
主要活动.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private EditText textBox;
private int z , answer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textBox=findViewById(R.id.editText);
setActual();
}
// Top Part of your code from main method
private void setActual(){
Random random = new Random();
int[] firstNum = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int x = firstNum[random.nextInt(firstNum.length)];/*Here program should put a random number out of firstNum array*/
int[] secondNum = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int y = secondNum[random.nextInt(secondNum.length)];/*Here program should put a random number out of secondNum array*/
z = x*y;
}
// Cheking part of your code
public void onCheck(View view){
this.answer = Integer.parseInt(textBox.getText().toString());
if(answer == z){
// Do action on right ans
Log.d("TAG","Correct.");
}
else{
// do action on wrong ans
Log.d("TAG","Incorrect. Try again.");
}
}
}
这是此活动的 xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="Enter you guess"
android:inputType="numberDecimal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.333"
tools:layout_editor_absoluteX="0dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Check"
android:layout_margin="20dp"
android:onClick="onCheck"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>