Someone in comments suggested using Math.floor()
and intValue()
. How to implement it to my code? Is it still possible to run the program on Android?
I'm trying to create Android-compatible Java code (purpose is explained in comments in code). I'm new to Java, and still don't know about (probably basic) stuff like textboxes.
Right now I have code that:
x
and y
based on array-defined integer range (needs rework; explained in What do I need?
below), and z
(answer-equal variable) that will be used to compare input with actual answer.answer
) with actual answer (variable z
)Every needed piece of code is explained in comments in code, but here's a list too:
x
and y
, based on arrayspublic 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);
}
}
If you want to do it in android then you can not use main method, you need go for a single activity android app. Particularly for your problems ie
Here program should put a random number out of firstNum array
Use Random() object to get random int
same for other
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'
For this you need a EditText and extract typed integer using
int answer = Integer.parseInt((editText.getText().toString()))
Here is a complete solution with one activity.
MainActivity.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.");
}
}
}
Here is xml for this activity
<?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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.