gpt4 book ai didi

java - 安卓数学游戏 : How to get only non-negative subtraction questions and whole numbers only for division?

转载 作者:行者123 更新时间:2023-11-29 04:37:23 24 4
gpt4 key购买 nike

目前对于减法题,你可以得到第一个数字小于第一个数字(即6-10)的问题,除法的问题可以是小数答案。

我怎样才能让减法题总是非负的,而整数答案只对除法?

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

int rightBox;
Button b1;
Button b2;
Button b3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b1 = (Button)findViewById(R.id.button1);
b2 = (Button)findViewById(R.id.button2);
b3 = (Button)findViewById(R.id.button3);

createOperation();
}


public enum Operation {
SUM, DIVISION, MULTIPLICATION, SUBTRACTION
}

private Operation generateRandOperation() {
Random rand = new Random();
int choice = rand.nextInt(4);
Operation operation = null;

switch (choice)
{
case 0:
operation = Operation.SUM;
break;
case 1:
operation = Operation.DIVISION;
break;
case 2:
operation = Operation.MULTIPLICATION;
break;
case 3:
operation = Operation.SUBTRACTION;
break;
}
String op = String.valueOf(operation);
Log.d("Operation: ", op);
return operation;
}

public void createOperation() {

Operation operation = generateRandOperation();
Random rand = new Random();
int num1 = rand.nextInt(1000);
int num2 = rand.nextInt(1000);
int wrongAns1 = rand.nextInt(1000);
int wrongAns2 = rand.nextInt(1000);
rightBox = rand.nextInt(2);
String choice = String.valueOf(rightBox);
Log.d("Right box: ", choice);

String equationText = String.valueOf(num1) + " " +
getOperationString(operation) + " " + String.valueOf(num2) + "?";

TextView displayEq = (TextView) findViewById(R.id.tvDisplay);
displayEq.setText(equationText);

float rightAns = calculateAnswer(operation, num1, num2);

switch(rightBox) {
case 0:
b1.setText(String.valueOf(rightAns));
b2.setText(String.valueOf(wrongAns1));
b3.setText(String.valueOf(wrongAns2));
break;
case 1:
b1.setText(String.valueOf(wrongAns1));
b2.setText(String.valueOf(rightAns));
b3.setText(String.valueOf(wrongAns2));
break;
case 2:
b1.setText(String.valueOf(wrongAns2));
b2.setText(String.valueOf(wrongAns1));
b3.setText(String.valueOf(rightAns));
break;
default:
break;
}

}

private float calculateAnswer(Operation operation, int num1, int num2) {

float ans = 0;

if (operation == Operation.SUM)
{
ans = num1 + num2;
}
else if (operation == Operation.SUBTRACTION)
{
ans = num1 - num2;
}
else if (operation == Operation.DIVISION)
{
ans = num1 / num2;
}
else if (operation == Operation.MULTIPLICATION)
{
ans = num1 * num2;
}
String choice = String.valueOf(ans);
Log.d("ANS", choice);
TextView answerTv = (TextView) findViewById(R.id.tvPoints);
answerTv.setText(String.valueOf(ans));
return ans;
}

private String getOperationString(Operation operation) {

String operationText = "";

if (operation == Operation.SUM)
{
operationText = "+";
} else if (operation == Operation.MULTIPLICATION)
{
operationText = "*";
} else if (operation == Operation.SUBTRACTION)
{
operationText = "-";
} else if (operation == Operation.DIVISION)
{
operationText = "/";
}
return operationText;
}

public void onClick(View view) {

boolean correct = false;
boolean play = true;

switch(view.getId()) {
case R.id.button1:
if (rightBox == 0)
{
correct = true;
}
break;
case R.id.button2:
if (rightBox == 1)
{
correct = true;
}
break;
case R.id.button3:
if (rightBox == 2)
{
correct = true;
}
break;
}
if (correct)
{
Toast.makeText(getApplicationContext(), "Correct!", Toast.LENGTH_SHORT).show();
}
if (!correct)
{
Toast.makeText(getApplicationContext(), "WRONG!", Toast.LENGTH_SHORT).show();

}
if(play)
{
createOperation();
}
}
}

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mathgame.MainActivity">

<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:id="@+id/button1"
android:onClick="onClick"/>

<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:layout_toEndOf="@+id/button1"
android:layout_marginStart="21dp"
android:id="@+id/button2"
android:onClick="onClick"/>

<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button2"
android:layout_toEndOf="@+id/button2"
android:layout_marginStart="30dp"
android:id="@+id/button3"
android:onClick="onClick"/>

<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/button2"
android:layout_marginStart="13dp"
android:layout_marginTop="78dp"
android:id="@+id/tvDisplay"/>

<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_alignStart="@+id/tvDisplay"
android:layout_marginTop="66dp"
android:id="@+id/tvPoints"/>
</RelativeLayout>

最佳答案

使用 if else 语句示例:

Random rand = new Random();
int num1 = rand.nextInt(1000);
int num2 = rand.nextInt(1000);
if(num1<num2){
Operation operation = generateRandOperation();
while(operation == Operation.SUBTRACTION||operation == Operation.DIVISION){
Operation operation = generateRandOperation();
}
}
else{
Operation operation = generateRandOperation();
}
if(operation == Operation.DIVISION){
if(isPrime(num1)){
num1++;
}
while(((num1%num2)!=0)&&num1<num2){
int num2 = rand.nextInt(1000);
}
}

将此方法添加到程序中

public static boolean isPrime(int number){
for(int i=2; i<number; i++){
if(number%i == 0){
return false; //number is divisible so its not prime
}
}
return true; //number is prime now
}

关于java - 安卓数学游戏 : How to get only non-negative subtraction questions and whole numbers only for division?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40749524/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com