计算出一个数字在java中分成另一个数字的次数并将其打印出来

时间:2013-02-23 17:25:17

标签: java

我需要能够输入两个数字,将第一个数字除以第二个数字并输出它进入它的次数以及余数是多少。

我可以打印剩下的部分,但我该如何计算出数字除以的次数? 我的代码如下

import java.util.Scanner;

public class TotalAndRemainder
{

    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter your first number");
        int firstvalue = keyboard.nextInt();

        System.out.println("Enter your second number");
        int secondvalue = keyboard.nextInt();

        int total = firstvalue / secondvalue;
        int remainder = firstvalue % secondvalue;



        System.out.println("The remainder is " + remainder);

    }
}

3 个答案:

答案 0 :(得分:1)

如果我理解你正在尝试做什么,当你找到secondvalue时,firstvalue进入total已经有多少次了。你只需要用这样的语句打印出来:

System.out.println(secondvalue + " goes into " + firstvalue + ", " + total + "times.");

答案 1 :(得分:0)

System.out.println(firstNumber + " goes into " + secondNumber + " " + total + " times. The remainder is " + remainder + ".");

在java int\int=int中,即您的total字段已经为您提供了整数次数。

答案 2 :(得分:-1)

import java.util.Scanner;

public class TotalAndRemainder
{

    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter your first number");
        int firstvalue = keyboard.nextInt();

        System.out.println("Enter your second number");
        int secondvalue = keyboard.nextInt();
        if(firstvalue >= secondvalue)
        { 
           int total = firstvalue / secondvalue;
           int remainder = firstvalue % secondvalue;
        }
        else
        {
           int total = secondvalue / firstvalue;
           int remainder = secondvalue % firstvalue;
        }

        System.out.println("The total is " + total + "remainder is " + remainder);

    }
}

我希望这对你有用......