[英]Display Prime Factors Prompting User for Integer Using StackOfIntegers Java Class
So I have this assignment and I have been struggling any help or feedback would be welcomed :) 因此,我已经完成了这项工作,并且一直在努力寻求帮助或反馈,欢迎您:)
Problem 问题
(Displaying the prime factors) Write a program that prompts the user to enter a positive integer and displays all its smallest factors in decreasing order. (显示主要因子)编写一个程序,提示用户输入正整数,并以降序显示所有最小因子。 Using StackOfIntergers Class.
使用StackOfIntergers类。
Here's what I have so far and the program compiles and runs but getting prime numbers instead of the prime factors. 这是我到目前为止的内容,程序可以编译并运行,但是得到质数而不是质因数。
package primefactors;
import java.util.Scanner;
public class PrimeFactors {
public static void main(String[] args) {
System.out.print("Enter a positive number: ");
Scanner scanner = new Scanner (System.in);
final int number = scanner.nextInt();
int count = 0;
StackOfIntegers stack = new StackOfIntegers();
// Repeatedly find prime factors
for (int i = 2; i <= number; i++)
if (isPrime(i)) {
stack.push(i);
count++; // Increase the prime number count
}
// Print the prime factors
System.out.println("The prime numbers are \n");
final int NUMBER_PER_LINE = 10;
while (!stack.empty()) {
System.out.print(stack.pop() + " ");
if (stack.getSize() % NUMBER_PER_LINE == 0)
System.out.println(); // advance to the new line
}
}
public static boolean isPrime(int number) {
// Assume the number is prime
boolean isPrime = true;
for (int divisor = 2; divisor <= number / 2; divisor++) {
//If true, the number is not prime
if (number % divisor == 0) {
// Set isPrime to false, if the number is not prime
isPrime = false;
break; // Exit the for loop
}
}
return isPrime;
}
}
* Update#2 * So I needed to just get the prime factor working so this is what I ended up with after researching some more & coding. * Update#2 *所以我只需要使主要因素起作用,所以这是在研究了更多内容和编码后最终得出的结果。
It works. 有用。 Now I need to work on having the program show both prime factors and prime numbers in a nice list.
现在,我需要让程序在一个漂亮的列表中同时显示素数和素数。
Thanks for the feedback & suggestions. 感谢您的反馈和建议。
import java.text.MessageFormat;
import java.util.Scanner;
public class PrimeFactor {
public static void main(String[] args) {
System.out.print("Enter a positive number: ");
Scanner scanner = new Scanner (System.in);
int number = scanner.nextInt();
int count;
StackOfIntegers stack = new StackOfIntegers();
for (int i = 2; i<=(number); i++) {
count = 0;
while (number % i == 0) {
number /= i;
count++;
}
if (count == 0) continue;
stack.push(i);
count++;
}
System.out.println("The prime factors are \n");
final int NUMBER_PER_LINE = 10;
while (!stack.empty()) {
System.out.print(MessageFormat.format("{0} ", stack.pop()));
if (stack.getSize() % NUMBER_PER_LINE == 0)
System.out.println(); // advance to the new line
}
}
}
StackOfInterger Class StackOfInterger类
public class StackOfIntegers {
private int[] elements;
private int size;
/** Construct a stack with the default capacity 16 */
public StackOfIntegers() {
this(16);
}
/** Construct a stack with the specified maximum capacity */
public StackOfIntegers(int capacity) {
elements = new int[capacity];
}
/** Push a new integer into the top of the stack */
public int push(int value) {
if (size >= elements.length) {
int[] temp = new int[elements.length * 2];
System.arraycopy(elements, 0, temp, 0, elements.length);
elements = temp;
}
return elements[size++] = value;
}
/** Return and remove the top element from the stack */
public int pop() {
return elements[--size];
}
/** Return the top element from the stack */
public int peek() {
return elements[size - 1];
}
//whether the stack is empty */
public boolean empty() {
return size == 0;
}
/** Return the number of elements in the stack */
public int getSize() {
return size;
}
}
This works as described. 如描述的那样工作。 When I ran it with 20, I got the following output: 19 17 13 11 7 5 3 2
当我用20运行它时,得到以下输出:19 17 13 11 7 5 3 2
These are all prime numbers. 这些都是质数。 I'm not sure why you expect 5,3,2,2.
我不确定您为什么期望5,3,2,2。
I believe the next step is to go through all of these prime numbers and see if they are prime factors of 20. This should be 5 and 2. To do this, you should iterate over the list and see if the input % (which is modulus) the number you're testing is equal to 0. 我相信下一步是遍历所有这些素数,看看它们是否是20的素数。应该是5和2。为此,您应该遍历列表并查看输入%(即模数)您要测试的数字等于0。
Try this it returns the 5 smallest prime factors of a given number if there is 5 prime factors. 尝试此操作,如果有5个素数因子,它将返回给定数的5个最小素数因子。 If you wanted to display two lists, one for primes and one for prime factors why not create two stacks and display separatly.
如果要显示两个列表,一个用于质数,一个用于质因数,为什么不创建两个堆栈并分别显示。
public class Temp2 { 公共课程Temp2 {
public static void main(String[] args)
{
int number, integer, count = 0;
StackOfIntegers stack = new StackOfIntegers();
Scanner input = new Scanner(System.in);
System.out.println("Enter a positive integer: ");
integer = input.nextInt();
//Find prime factors
for (number = 2; number < integer; number++)
{
if (isPrime(number) && integer % number == 0)
{
stack.push(number);
count++;
if(count == 5) break;
}
}
// Print the smallest prime factors in decreasing order
System.out.println("The smallest prime factors of " + integer + " are ");
while (!stack.empty())
{
System.out.print(stack.pop() + ", ");
}
System.out.println(); //Advance to the new line
}//ENDMAIN
public static boolean isPrime(int number)
{
for (int divisor = 2; divisor <= number / 2; divisor++)
{
//If true, the number is not prime, return false
if (number % divisor == 0)
return false;
}
return true;
}
}//ENDCLASS } // ENDCLASS
A workable solution for this assignment question can be found here: http://www.besteduweb.com/assignment-solution-using-stack-of-integers-class.html 可以在以下位置找到有关此分配问题的可行解决方案: http : //www.besteduweb.com/assignment-solution-using-stack-of-integers-class.html
This is the 100% workable solution and works for me. 这是100%可行的解决方案,对我有用。 Recommended 100%.
推荐100%。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.