我试图找出我哪里出错了。该代码假设采用两个随机数,找出一个是否是另一个的因素,并根据您的答案打印出各种结果。
我不知道我哪里出错了,但由于某种原因,代码不会进入我的 if 语句。
//import Scanner class
import java.util.Scanner;
//add class structure
public class Factor
{
//add main method
public static void main(String[] args)
{
//initialize the first number
int number1;
//initialize the second number
int number2;
String yes = "yes";
String no = "no";
//generate random number 1
number1 = (int)(Math.random()*11);
//generate random number 2
number2 = (int)(Math.random()*101);
//if number one is larger, swap
if (number2 < number1)
{
//store the value of number 1
int store = number1;
//number 1 becomes the same magnitude as number 2
number1 = number2;
//number 2 is now the stored value
number2 = store;
}
//construct instance of scanner
Scanner myScanner;
//input for scanner
myScanner= new Scanner( System.in);
//system statement
System.out.println("Is " + number1 + " a factor of " + number2 + " ? (yes or no)");
//accept user input
String answer = myScanner.nextLine();
//divide to see if a factor
double number3 = (double)number2 / (double)number1;
//setup inital parameters
if (answer == yes){
//setput more parameters
if ((int)number3 == number3) {
//ouput statement
System.out.println("Enter the value of x that (x * " + number1 + " = " + number2 + " : ");
//accept user input
int userinput = myScanner.nextInt();
//setup parameters
if (userinput == number3){
//output statement
System.out.println("Your answer is correct!");
}
//response
else {
//output statement
System.out.println("Actually, " + number3 + " is the correct answer.");
}
}
//setup parameters
if ((int)number3 != number3) {
System.out.println("Actually, " + number1 + " isn't a factor of " + number2 + " .");
}
}
//setup new parameters
if (answer == no) {
//setup parameters
if ((int)number3 == number3) {
//output statement
System.out.println("Actually, " + number1 + " is a factor of " + number2 + " .");
}
//response statement
else {
//output statement
System.out.println("Your answer is correct!");
}
}
}
}