- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
方法 testPerfect
应该从 getPerfect
接收一个数字,对数字进行分解并将数字放入数组中。我目前有数组打印,但全为零。关于将数字的因子放入数组中的任何建议?
public class aermel_Perfect
{
public static void main ( String args [] )
{
int gN;
int gP = getPerfect();
int [] array = new int[100];
//int printFactors;
System.out.println(Arrays.toString(array));
}
public static int getNum() //Get amount of numbers to check
{
Scanner input = new Scanner ( System.in );
System.out.print( "How many numbers would you like to test? " );
int count = input.nextInt();
int perfect = 1;
boolean vN = validateNum(count, perfect);
while(!vN)
{
System.out.print (" How many numbers would you like to test? ");
count = input.nextInt();
vN = validateNum(count, perfect);
}
return count;
}
public static boolean validateNum( int count, int perfect ) //Check if number is valid
{
if (( count <= 0) || ( perfect <= 0))
{
System.out.print( "Non-positive numbers are not allowed.\n");
}
else
{
return true;
}
return false;
}
public static int getPerfect() //Gets the numbers to test
{
Scanner input = new Scanner ( System.in );
int perfect = -1;
int count = getNum();
System.out.print("Please enter a perfect number: " );
perfect = input.nextInt();
boolean vN = validateNum(perfect, count);
while (!vN)
{
System.out.print("Please enter a perfect number: ");
perfect = input.nextInt();
vN=validateNum(perfect, count);
}
return perfect;
}
public static int[] testPerfect( int perfect, int[] array )
{
testPerfect(perfect, array);
int limit = (int) Math.ceil(Math.sqrt(perfect));
int index = 0;
for ( int i = 1; i <=limit; i++)
{
array[index++] = i;
perfect /= i;
}
array[index] = perfect;
return array;
}
}
最佳答案
也许是这样的?首先,您需要使用从 getPerfect()
接收的 gP
和 array
从主函数调用 testPerfect()
> 作为参数。其次,您需要删除调用 testPerfect()
,它是方法 testPerfect()
的第一行,否则这将导致无限递归调用。
public class aermel_Perfect
{
public static void main ( String args [] )
{
int gN;
int gP = getPerfect();
int [] array = new int[100];
array=testPerfect(gP,array);
//int printFactors;
System.out.println(Arrays.toString(array));
}
public static int getNum() //Get amount of numbers to check
{
Scanner input = new Scanner ( System.in );
System.out.print( "How many numbers would you like to test? " );
int count = input.nextInt();
int perfect = 1;
boolean vN = validateNum(count, perfect);
while(!vN)
{
System.out.print (" How many numbers would you like to test? ");
count = input.nextInt();
vN = validateNum(count, perfect);
}
return count;
}
public static boolean validateNum( int count, int perfect ) //Check if number is valid
{
if (( count <= 0) || ( perfect <= 0))
{
System.out.print( "Non-positive numbers are not allowed.\n");
}
else
{
return true;
}
return false;
}
public static int getPerfect() //Gets the numbers to test
{
Scanner input = new Scanner ( System.in );
int perfect = -1;
int count = getNum();
System.out.print("Please enter a perfect number: " );
perfect = input.nextInt();
boolean vN = validateNum(perfect, count);
while (!vN)
{
System.out.print("Please enter a perfect number: ");
perfect = input.nextInt();
vN=validateNum(perfect, count);
}
return perfect;
}
public static int[] testPerfect( int perfect, int[] array )
{
//testPerfect(perfect, array);
int limit = (int) Math.ceil(Math.sqrt(perfect));
int index = 0;
for ( int i = 1; i <=limit; i++)
{
array[index++] = i;
perfect /= i;
}
array[index] = perfect;
return array;
}
}
关于java - 将数字因式分解放入数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13449509/
我很抱歉提出一个关于 Eigen3 优化计算方案的一般性问题。假设我们确实拥有两个 Eigen3 矩阵,M 和 N。假设我们需要计算以下内容: Eigen::Matrix M; Eigen::Matr
如果我使用 Sympy 获得了以下方程: 是否可以排列变量,使 x 和 L 在方程中仅显示为 x/L? 最佳答案 用另一个符号(例如 y)替换 x/L 似乎对我有用: >>> import sympy
我不确定我的标题应该是什么。但是,我试图对我的代码进行很好的验证。以下是我的函数中唯一允许使用的代码:c0001、c0002、c0003、c0004、c0005、C0001、C0002、C0003、C
这周我开始较少样式表开发。 我当前的元素有许多不同颜色的链接,例如: #dev-team a { color: #D09EBA; } #admin-team a { color: #0
我试图通过分解 N 来找到 D。 我的 N 是 265291078722948385089717069136983657793 我发现 P & Q 使用 n = p.q P - 1471697682
这个问题在这里已经有了答案: Partitioning in JavaScript [duplicate] (7 个答案) 关闭 7 年前。 假设我有一个数组 = [0,1,2,3,4,5,6],我
我有这个数据框:基本上每一行都是一个客户一天执行的一笔交易。同一客户在同一天和不同日期进行多笔交易。我想获得一个列来显示客户之前访问的次数。 id date purchase id1 date1
我是一名优秀的程序员,十分优秀!