gpt4 book ai didi

java - 如何将较大的数除以较小的数

转载 作者:行者123 更新时间:2023-12-02 11:54:19 25 4
gpt4 key购买 nike

我试图将这个问题中较大的数字除以较小的数字,并以商的形式打印答案。我已经完成了将打印正确答案的代码,但我想知道如何将较大的数字除以较小的数字。我是否使用 if 语句?

case 1:
int number1;
int number2;

System.out.println("Please enter your first number");
number1 = s.nextInt();

System.out.println("Please enter your second number");
number2 = s.nextInt();

int quotient = number1 / number2;
int remainder = number1 % number2;

if(number1 > number2 || number2 > number1) {
System.out.println("Quotient = " + quotient + "r" + remainder);
}

break;

最佳答案

也许是这样的:

int smallerNumber;
int biggerNumber;

System.out.println("Please enter your first number");
smallerNumber = s.nextInt();


int temp;
System.out.println("Please enter your second number");
temp = s.nextInt();

smallerNumber=Math.min(temp,smallerNumber);
biggerNumber=Math.max(temp,smallerNumber);

或者使用数组然后对其进行排序:

List<Integer> arr= new ArrayList<>(2);

System.out.println("Please enter your first number");
arr.add( s.nextInt());


System.out.println("Please enter your second number");
arr.add( s.nextInt());

Collections.sort(arr);

//now arr.get(0) is smaller and arr.get(1) is bigger

关于java - 如何将较大的数除以较小的数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47704004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com