-1

我试图找出我哪里出错了。该代码假设采用两个随机数,找出一个是否是另一个的因素,并根据您的答案打印出各种结果。

我不知道我哪里出错了,但由于某种原因,代码不会进入我的 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!");
               }
          }
     }
}
4

2 回答 2

2

另一个字符串比较。:)

  if (answer == yes){

应该

  if (answer.equals(yes)){

使用equals()方法检查字符串是否相等==运算符只检查两个引用变量是否引用同一个字符串对象。

于 2013-02-19T23:33:33.157 回答
0

除了字符串比较之外,我还建议添加,您可以将变量“number3”声明为 Double 并在 if 子句中而不是将其强制转换为 int,调用 intValue() 方法,这样就可以了喜欢

Double number3 = (double)number2 / (double)number1;
....
if (number3.intValue() == number3) {....}   //instead of if ((int)number3 == number3) 
于 2013-02-19T23:53:05.683 回答