public class JavaProjectTest {
public static void main(String[] args) {
Function1 fn = new Function1();
//System.out.println(fn.condition());
for (int i = 1; i <= 10; i++) {
System.out.println(i + ". " + fn.random_Question());
for (int j = 'a'; j <= 'd'; j++) {
if (j == 'a') {
System.out.println(" a. " + fn.cal());
} else if (j == 'b') {
System.out.println(" b. " + fn.cal());
} else if (j == 'c') {
System.out.println(" c. " + fn.cal());
} else if (j == 'd') {
System.out.println(" d. " + fn.cal());
}
}
}
}
}
class ย่อยครับ Code (Java)
import java.util.Random;
public class Function1 {
Random random = new Random();
int num_random;
int rn_op = -3;
int x, y, result;
String[] condition = {"+", "-", "*", "/"};
public int random_Question() {
num_random = random.nextInt(200) + 100;
return num_random;
}
public int random_choice() {
do {
num_random = random.nextInt(100);
} while (num_random == 0 && num_random == 1);
{
return num_random;
}
}
public int random_Option() {
num_random = random.nextInt(3) + rn_op;
return num_random;
}
public String cal() {
String c = null;
for (String condition1 : condition) {
c = condition[random.nextInt(condition.length)];
c.toString();
}
switch (c) {
case "+":
return plus();
case "-":
return del();
case "*":
return multi();
default:
return divide();
}
}
public String plus() {
int x, y, result;
do {
x = random_choice();
y = random_choice();
result = x + y;
} while (result == num_random);
{
return x + " + " + y + " = " + (num_random);
}
}
public String del() {
int x = random_choice();
int y = random_choice();
int result = x - y;
while (result != num_random) {
x = random_choice();
y = random_choice();
result = x - y;
}
return x + " - " + y + " = " + (num_random);
}
public String multi() {
int x = random_choice();
int y = random_choice();
int result = x * y;
while (result != num_random) {
x = random_choice();
y = random_choice();
result = x * y;
}
return x + " * " + y + " = " + (num_random);
}
public String divide() {
int x = random_choice();
int y = random_choice();
int result = x / y;
while (result != num_random) {
x = random_choice();
y = random_choice();
result = x + y;
}
return x + " / " + y + " = " + (num_random);
}
}