-1

I'm trying to find where I went wrong. The code is suppose to take two random numbers, find out if one is a factor of the other and print out various results based on your answer.

I don't know where I went wrong, but for some reason, the code will not proceed into my if statement.

//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!");
               }
          }
     }
}

2 Answers2

2

Another String comparision . :)

  if (answer == yes){

should be

  if (answer.equals(yes)){

Use equals() method to check string equality. == operator just checks if two reference variables refer to the same string object.

PermGenError
  • 45,977
  • 8
  • 87
  • 106
0

I would also add, as a suggestion and besides the string comparation, that you could declare the variable 'number3' as a Double and in the if clause instead of cast it to int, invoke the method intValue(), so it would be something like

Double number3 = (double)number2 / (double)number1;
....
if (number3.intValue() == number3) {....}   //instead of if ((int)number3 == number3) 
maxivis
  • 1,727
  • 3
  • 21
  • 35