决定第二个数字是否是第一个数字的倍数的程序不能正常工作



如果第二个数字是第一个数字的倍数,则此程序应返回True。如果不是,则为False,并且应该执行三次。输出只是给出第一个正确答案。如何获得包含变量f和g的返回?

或者,如果这不是正确的做法,那又是什么呢?我需要让它们都来自同一种方法,否则我只会制作更多的方法,但事实上我被难住了。

非常感谢您的帮助。对不起我的套子。

import java.util.Scanner;
public class Numbers3 {
    // starts execution of java application
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int firstnumber = 0; // initialize integer first number
        int secondnumber = 0; // initialize integer second number
        int third = 0;
        int fourth = 0;
        int fifth = 0;
        int sixth = 0;
        // First input field
        System.out.print("Input first number ");
        firstnumber = input.nextInt();
        // Second input field
        System.out.print("Input second number ");
        secondnumber = input.nextInt();
        // makes result equal the Boolean output of isMultiple method
        Boolean result = isMultiple(firstnumber, secondnumber, third, fourth,
                fifth, sixth);
        System.out.println("" + result);
        System.out.println();
        System.out.print("input first number ");
        third = input.nextInt();
        System.out.print("input second number ");
        fourth = input.nextInt();
        System.out.println("" + result);
        System.out.println();
        System.out.print("input first number ");
        fifth = input.nextInt();
        System.out.print("input second number ");
        sixth = input.nextInt();
        System.out.println("" + result);
    }
    // creates method using the user input
    public static Boolean isMultiple(int a, int b, int w, int x, int y, int z) {
        Boolean e = null; // initialize boolean
        Boolean f = null;
        Boolean g = null;
        if (a % b != 0) // what the function does if the result is not 0
            e = false;
        // what the function will do if the function does result in 0
        if (a % b == 0)
            e = true;
        if (w % x != 0)
            f = false;
        if (w % x == 0)
            f = true;
        if (y % z != 0)
            g = false;
        if (y % z == 0)
            g = true;
        return e;
        // returns e as the result of this method.
    } // end program
} // end class

对于每次运行,都有两个输入
目标:使用isMultiple()检查第一个输入是否是第二个输入的倍数。

要运行它3次(或任意#次(,请将repeating code放入for循环中
#重复循环的次数存储在常量NUM_RUNS中。

代码:

import java.util.Scanner;
public class Numbers3 {
    private static final int NUM_RUNS = 3;
    // starts execution of java application
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        for(int i = 0; i < NUM_RUNS; i++) {
            // First input field
            System.out.print("Input first number: ");
            int firstNumber = input.nextInt();
            // Second input field
            System.out.print("Input second number: ");
            int secondNumber = input.nextInt();
            System.out.printf("%d is a multiple of %d: %s%n%n",
                    firstNumber, secondNumber,
                    isMultiple(firstNumber, secondNumber));
        }
    }
    public static boolean isMultiple(int a, int b) {
        return (a % b == 0);
    }
} // end class

输入/输出示例:

Input first number: 8
Input second number: 2
8 is a multiple of 2: true
Input first number: 7
Input second number: 3
7 is a multiple of 3: false
Input first number: 18
Input second number: 6
18 is a multiple of 6: true

我不确定fg是怎么回事,但这里有一个程序要求两个数字,三次,如果第二个是第一个的倍数,则打印truefalse

public static void main(String[] args) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    for (int i = 0; i < 3; i++) {
        System.out.print("Enter first number : ");
        int first = Integer.parseInt(reader.
        System.out.print("Enter second number : ");
        int second = Integer.parseInt(reader.readLine());
        boolean secondMultipleOfFirst = second % first == 0;
        System.out.println(secondMultipleOfFirst);
    }
}

我也知道你的方法出了什么问题。您计算的值是正确的,但每次都返回e,这是第一个结果。因此,接下来的两个输入给出了第一个结果。

使用循环,而不是设置更多的方法或查看返回哪个值的方法。这样,您可以取两个值,看看n2是否是n1的倍数。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dump_me;
import java.util.Scanner;
     public class Numbers3
    {
       // starts execution of java application
       public static void main( String[] args)
       {
           Scanner input = new Scanner (System.in );
           int firstnumber = 0; // initialize integer first number
           int secondnumber = 0; // initialize integer second number
    // makes result equal the Boolean output of isMultiple method
           String loop = "N";
           do{
   // First input field
           System.out.print("Input first number ");
           firstnumber = input.nextInt();
           // Second input field
           System.out.print("Input second number ");
           secondnumber = input.nextInt();
           Boolean result = isMultiple(firstnumber, secondnumber);
            System.out.println("" + result );
            System.out.println();
            System.out.println("Do you wan to continue? Press y to continue or n to exit" );
            loop = input.next();
           }while(loop.equalsIgnoreCase("y"));
   }
// creates method using the user input
public static Boolean isMultiple( int a, int b)
      {
          if(a==0 || b==0)
          {
              return false;
          }
          else
          {
              if(b % a ==0)
              {
                  return true;
              }
              else
              {
                  return false;
              }
          }
// returns e as the result of this method.
        } // end program
    } // end class

用户输入已被放入循环中。此程序将接受用户输入并检查值。

最新更新