新程序员试图在代码中找到错误

时间:2013-02-19 23:31:57

标签: java if-statement random

我试图找到我错的地方。代码假设取两个随机数,找出一个是另一个的因子,并根据你的答案打印出各种结果。

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

2 个答案:

答案 0 :(得分:2)

另一个字符串比较。 :)

  if (answer == yes){

应该是

  if (answer.equals(yes)){

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

答案 1 :(得分:0)

我还要添加一个建议,除了字符串比较之外,你可以将变量'number3'声明为Double,而在if子句中而不是将它转换为int,调用方法intValue(),所以它就像是

Double number3 = (double)number2 / (double)number1;
....
if (number3.intValue() == number3) {....}   //instead of if ((int)number3 == number3)