试图让isMultiple正确执行

时间:2016-11-20 13:54:54

标签: java

这会执行,但它会将所有答案都返回为正数" 8是15"的倍数。即使它是假的。不确定我没看到什么。

这就是我所拥有的:

import java.util.*;
public class Multiples {

public static void main(String [] args){

    boolean run = true;

    while(run = true){
    Scanner input = new Scanner(System.in);
    System.out.print("Enter one number:");
    int num1 = input.nextInt();
    System.out.print("Enter a second number:");
    int num2 = input.nextInt();

    boolean result = isMultiple(num1,num2);


    if(result = true){
        System.out.println(num2 + " is a multiple of " + num1);
    }

    else{
        System.out.println(num2 + " is not a multiple of " + num1);
    }

    System.out.println("Do you want to enter another pair(y/n)?");
    String a = input.next();

    if(YesOrNo(a)){
       break;
    }

    }

}

    public static boolean YesOrNo(String a){

        if(a.equals("y"))
            return false;
        else if(a.equals("n"))
            return true;
        else
            return true;
    }

    public static boolean isMultiple (int x , int y){

         if(x % y == 0 || y % x == 0)
             return true;   
         else
             return false;

    }
}

1 个答案:

答案 0 :(得分:1)

if(result = true){

替换为

if(result == true){

(或简称)

if(result){

您正在分配而不是比较。