在java中我正在尝试制作一个程序来完成我的数学作业(不是为了实际作弊,只是想学习java)而且我有一个for循环让我得到给定数字的所有因素,但我可以&# 39;弄清楚如何成对保存for循环的所有输出(如果可能的话)以便稍后在数学问题中进行测试来解决它,这里是代码。 (将根据要求添加所需信息,首先发布)
import java.util.Scanner;
public class factoring {
public static void main(String[] args) {
String varible;
String secondOperator;
String firstOperator;
int power;
int greatestCommonFactor;
long factorTo;
long factorBy;
String factors = null;
Scanner userInput = new Scanner(System.in);
System.out.println("Please enter the greatest common factor (Default to 1)");
greatestCommonFactor = userInput.nextInt();
System.out.println("Please input the varible");
varible = userInput.next();
System.out.println("Please enter the power");
power = userInput.nextInt();
System.out.println("Please enter the first operator");
firstOperator = userInput.next();
System.out.println("Please enter what your factoring to");
factorTo = userInput.nextInt();
System.out.println("Please enter the second operator");
secondOperator = userInput.next();
System.out.println("Please enter what you're factoring by");
factorBy = userInput.nextInt();
for(int i = 1; i * i <= factorBy; i++) {
if (factorBy % i == 0) {
if (i * i != factorBy) factors = factorBy / i + " and " + i;
}
}
}
}
答案 0 :(得分:1)
有一种更简单的方法来计算最大的公因子或除数,称为欧几里德算法。
int gcd(int a, int b) {
while(0 != b) {
r = a%b; a = b; b = r;
}
return a;
}
答案 1 :(得分:0)
您可以使用文件保存结果。
尝试以下代码
db=> SELECT EXTRACT(DOY FROM CURRENT_DATE)::int;
date_part
-----------
41