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();

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



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

    }
}
4

3 回答 3

1

如果我正确理解你想要做什么,你已经有多少次secondvalue进入firstvalue,当你找到total。您只需要使用如下语句将其打印出来:

System.out.println(secondvalue + " goes into " + firstvalue + ", " + total + "times.");
于 2013-02-23T17:30:32.170 回答
0
System.out.println(firstNumber + " goes into " + secondNumber + " " + total + " times. The remainder is " + remainder + ".");

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

于 2013-02-23T17:30:28.183 回答
-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);

    }
}

我希望这对你有用...

于 2013-02-23T17:38:20.890 回答