-3

当我运行它时,我得到了这个:

Enter 2 Integers, Do note that you will Get a Quotient and a Remainder.
Enter the First Number: 3
Now enter the Second Number: 9
Your Quotient is: 0
Your Remainder is: 3

它应该是:

Enter 2 Integers, Do note that you will Get a Quotient and a Remainder.
Enter the First Number: 3
Now enter the Second Number: 9
Your Quotient is: 3
Your Remainder is: 0

代码:

import java.util.Scanner;
public class Remainder {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        int a = 0, b = 0, quotient, remainder;
        int smallerNumber = 0;
        int biggerNumber = 0;

        System.out.println("Enter 2 Integers, Do note that you will Get a Quotient and a Remainder.");
        System.out.print("Enter the First Number: ");
        a = reader.nextInt();
        System.out.print("Now enter the Second Number: ");
        b = reader.nextInt();

        remainder = (a % b);

        quotient = (a / b);
        remainder = (a % b);

        if(a > b){
            biggerNumber = a;
            a = b;
        }else{
            smallerNumber = a;
            biggerNumber = b;
        }
        System.out.print("Your Quotient is: ");
        System.out.println(quotient);
        if (remainder > 0){
            System.out.print("Your Remainder is: ");
            System.out.println(remainder);
        }
    }
}
4

2 回答 2

3

您输入的数字顺序错误。你正在做a / b,并且3 / 9在整数数学中为零。先输入 9,再输入 3,就可以了。

另请注意,在计算余数和商之后,您正在寻找更大和更小的数字(并且您计算前者两次)。交换代码的顺序,并划分bigger/smaller以获得更好的结果......

  System.out.println("Enter 2 Integers, Do note that you will Get a Quotient and a Remainder.");
  System.out.print("Enter the First Number: ");
  a = reader.nextInt();
  System.out.print("Now enter the Second Number: ");
  b = reader.nextInt();

  if(a > b){
      biggerNumber = a;
      smallerNumber = b;
  } 
  else {
      smallerNumber = a;
      biggerNumber = b;
  }
  remainder = biggerNumber % smallerNumber;
  quotient = biggerNumber / smallerNumber;
于 2013-11-07T02:10:28.847 回答
0

你有 a 和 b 倒退。反而,

quotient = b / a;
remainder = b % a; 

此外,a 和 b 可能应该重命名为 first 和 second ...

于 2013-11-07T02:10:12.093 回答