Java - 使用 txt 文件从双向链表中计算质数的最佳方法是什么?
Java - Whats the best method for prime number counting from a doubly linked list using the txt files?
我对使用 java 还很陌生,所以可能没有必要给这个页面评分(除非你真的想要,我不在乎),所以我只是一个学生并且我不喜欢编程。
我正在查看这里的起始代码页,但我对应该使用什么方法来计算素数感到困惑。我的 Java 包包含 3 个编码页; 3 是 Mymain (dllmain)、Mylist(dlllist) 和 Mynode(dllnode)。
我知道我至少需要输入一种方法来计算素数。
接下来,我将向您展示 Mymain 脚本和 Mylist 中的两个质数方法以及一个 Mynode 构造函数,以防您需要查看此代码的工作原理。
谢谢。
package sample3;
import java.io.*;
public class Mymain {
public static void main(String[] args) {
// Declaring variables for "Mynode"
Mynode D1 = null; //"D" stands for Data
Mynode D2 = null;
Mynode D3 = null;
// Declaring variables for "Mylist"
Mylist L1 = new Mylist(); //The "L" stands for List.
Mylist L2 = new Mylist();
Mylist L3 = new Mylist();
/* Using the buffered reader to scan the following txt files.
*
* f1.txt // The "f" stands for file.
* f2.txt
* f3.txt
*
*/
String read = "";
BufferedReader br = null; //Buffered reader as scanner.
try {br = new BufferedReader(new FileReader("f1.txt"));
while ((read = br.readLine()) != null) {
Mynode Data = new Mynode(Integer.parseInt(read));
D1 = Data;
L1.addStart(Data);}
br.close(); //close file
br = new BufferedReader(new FileReader("f2.txt"));
while ((read = br.readLine()) != null) {
Mynode Data = new Mynode(Integer.parseInt(read));
D2 = Data;
L2.addStart(Data);}
br.close(); //close file
br = new BufferedReader(new FileReader("f3.txt"));
while ((read = br.readLine()) != null) {
Mynode Data = new Mynode(Integer.parseInt(read));
D3 = Data;
L3.addStart(Data);}
br.close(); //close file
}
/* Error checker:
*
* Checks if the file is valid.
* Checks the input and output of this package.
* Checks if all the figures in the file is numeric.
*
*/
catch (FileNotFoundException e)
{e.getMessage();}
catch (IOException e)
{e.getMessage();}
catch (NumberFormatException e)
{e.getMessage();}
//Introduction for describing the program when it runs
System.out.println("Outputting all the following data:\n");
System.out.println("Displaying node count from each file in order, reversed order \nprime numbers and intersections.");
System.out.println("This program contains: files - f1.txt, f2.txt, f3.txt \n");
System.out.println("____________________________________________________________\n");
// Outputting the results from all the lists
System.out.println("Numbers list 1- \t\tNode count:" + L1.numNodes() + "\n"); //Displaying all the numbers from the first number to the last number.
L1.printHead(D1);
System.out.println("\nIn reversed order: "); //Displaying all the numbers in backwards order from the last number to the first number.
L1.printBackwards(D1);
System.out.println("\n------------------------------------------------------------");
System.out.println("\nNumbers list 2- \t\tNode count:" + L2.numNodes() + "\n"); //Displaying all the numbers from the first number to the last number.
L2.printHead(D2);
System.out.println("\nIn reversed order: "); //Displaying all the numbers in backwards order from the last number to the first number.
L2.printBackwards(D2);
System.out.println("\n-------------------------------------------------------------");
System.out.println("\nNumbers list 3- \t\tNode count:" + L3.numNodes() + "\n"); //Displaying all the numbers from the first number to the last number.
L3.printHead(D3);
System.out.println("\nIn reversed order: "); //Displaying all the numbers in backwards order from the last number to the first number.
L3.printBackwards(D3);
System.out.println("\n-------------------------------------------------------------");
System.out.println("\n\nPrime numbers from List1-"); //Displaying all the existing prime numbers.
L1.printPrime(D1);
System.out.println("\n\nPrime numbers from List2-"); //Displaying all the existing prime numbers.
L2.printPrime(D2);
System.out.println("\n\nPrime numbers from List3-"); //Displaying all the existing prime numbers.
L3.printPrime(D3);
Mylist Ans1 = Mylist.Intersect(L1, L2); //Displaying the numbers that intersects from the lists 1,2 and 3.
Mylist Ans2 = Mylist.Intersect (Ans1, L3);
System.out.println("\n\nIntersections");
Ans2.printHead(Ans2.Head);
}
}
这里应该输出从文件中提取的素数的两个素数代码。
- 第一个是素数计数器,我不确定如何更改它以使其正常工作。
第二种方法有效,程序为运行.
时可以输出显示所有素数
第一个适用于数组 txt 文件但不适用于双链表的质数代码。
public static int primeChecker(int[] array) {
int PrimeNo = 0;
for(int i=0;i<array.length;i++) {
if(isPrime(array[i])) {
PrimeNo++;
}
}
return PrimeNo;
}
和第二个。
//Print Primes
public void printPrime(Mynode p) {
int k = 0;
while (p != null)
{
int x = p.Data;
if (isPrime(x))
{
k++;
System.out.print(p.Data + "\t");
if (k % 5 == 0) {
System.out.println("");
k = 0;
}
}
p = p.Next;
}
最后是 "Mynode" 代码页。
Mynode {
public int Data;
public Mynode Next;
public Mynode Prev;
public Mynode (int Data) {
Prev = null;
this.Data = Data;
Next = null;
抱歉代码太长。
要为双链表的 Mylist 实现重载 primeChecker 方法,您需要循环遍历链表的每个元素并在每次遇到素数时递增一个变量,就像您对为数组重载了 primeChecker 方法。我不确定您的 Mylist 双链表的确切实现,但它可能看起来像这样。
public static int primeChecker(Mylist list) {
int PrimeNo = 0;
//insert whatever method here gives you the first node of Mylist
//I inserted start() to demonstrate, that may or may not be right
Mynode p = list.start();
while (p != null)
{
int x = p.Data;
if (isPrime(x))
{
PrimeNo++;
}
p = p.Next;
}
return PrimeNo;
}
我对使用 java 还很陌生,所以可能没有必要给这个页面评分(除非你真的想要,我不在乎),所以我只是一个学生并且我不喜欢编程。
我正在查看这里的起始代码页,但我对应该使用什么方法来计算素数感到困惑。我的 Java 包包含 3 个编码页; 3 是 Mymain (dllmain)、Mylist(dlllist) 和 Mynode(dllnode)。
我知道我至少需要输入一种方法来计算素数。
接下来,我将向您展示 Mymain 脚本和 Mylist 中的两个质数方法以及一个 Mynode 构造函数,以防您需要查看此代码的工作原理。
谢谢。
package sample3;
import java.io.*;
public class Mymain {
public static void main(String[] args) {
// Declaring variables for "Mynode"
Mynode D1 = null; //"D" stands for Data
Mynode D2 = null;
Mynode D3 = null;
// Declaring variables for "Mylist"
Mylist L1 = new Mylist(); //The "L" stands for List.
Mylist L2 = new Mylist();
Mylist L3 = new Mylist();
/* Using the buffered reader to scan the following txt files.
*
* f1.txt // The "f" stands for file.
* f2.txt
* f3.txt
*
*/
String read = "";
BufferedReader br = null; //Buffered reader as scanner.
try {br = new BufferedReader(new FileReader("f1.txt"));
while ((read = br.readLine()) != null) {
Mynode Data = new Mynode(Integer.parseInt(read));
D1 = Data;
L1.addStart(Data);}
br.close(); //close file
br = new BufferedReader(new FileReader("f2.txt"));
while ((read = br.readLine()) != null) {
Mynode Data = new Mynode(Integer.parseInt(read));
D2 = Data;
L2.addStart(Data);}
br.close(); //close file
br = new BufferedReader(new FileReader("f3.txt"));
while ((read = br.readLine()) != null) {
Mynode Data = new Mynode(Integer.parseInt(read));
D3 = Data;
L3.addStart(Data);}
br.close(); //close file
}
/* Error checker:
*
* Checks if the file is valid.
* Checks the input and output of this package.
* Checks if all the figures in the file is numeric.
*
*/
catch (FileNotFoundException e)
{e.getMessage();}
catch (IOException e)
{e.getMessage();}
catch (NumberFormatException e)
{e.getMessage();}
//Introduction for describing the program when it runs
System.out.println("Outputting all the following data:\n");
System.out.println("Displaying node count from each file in order, reversed order \nprime numbers and intersections.");
System.out.println("This program contains: files - f1.txt, f2.txt, f3.txt \n");
System.out.println("____________________________________________________________\n");
// Outputting the results from all the lists
System.out.println("Numbers list 1- \t\tNode count:" + L1.numNodes() + "\n"); //Displaying all the numbers from the first number to the last number.
L1.printHead(D1);
System.out.println("\nIn reversed order: "); //Displaying all the numbers in backwards order from the last number to the first number.
L1.printBackwards(D1);
System.out.println("\n------------------------------------------------------------");
System.out.println("\nNumbers list 2- \t\tNode count:" + L2.numNodes() + "\n"); //Displaying all the numbers from the first number to the last number.
L2.printHead(D2);
System.out.println("\nIn reversed order: "); //Displaying all the numbers in backwards order from the last number to the first number.
L2.printBackwards(D2);
System.out.println("\n-------------------------------------------------------------");
System.out.println("\nNumbers list 3- \t\tNode count:" + L3.numNodes() + "\n"); //Displaying all the numbers from the first number to the last number.
L3.printHead(D3);
System.out.println("\nIn reversed order: "); //Displaying all the numbers in backwards order from the last number to the first number.
L3.printBackwards(D3);
System.out.println("\n-------------------------------------------------------------");
System.out.println("\n\nPrime numbers from List1-"); //Displaying all the existing prime numbers.
L1.printPrime(D1);
System.out.println("\n\nPrime numbers from List2-"); //Displaying all the existing prime numbers.
L2.printPrime(D2);
System.out.println("\n\nPrime numbers from List3-"); //Displaying all the existing prime numbers.
L3.printPrime(D3);
Mylist Ans1 = Mylist.Intersect(L1, L2); //Displaying the numbers that intersects from the lists 1,2 and 3.
Mylist Ans2 = Mylist.Intersect (Ans1, L3);
System.out.println("\n\nIntersections");
Ans2.printHead(Ans2.Head);
}
}
这里应该输出从文件中提取的素数的两个素数代码。
- 第一个是素数计数器,我不确定如何更改它以使其正常工作。
第二种方法有效,程序为运行.
时可以输出显示所有素数第一个适用于数组 txt 文件但不适用于双链表的质数代码。
public static int primeChecker(int[] array) {
int PrimeNo = 0;
for(int i=0;i<array.length;i++) {
if(isPrime(array[i])) {
PrimeNo++;
}
}
return PrimeNo;
}
和第二个。
//Print Primes
public void printPrime(Mynode p) {
int k = 0;
while (p != null)
{
int x = p.Data;
if (isPrime(x))
{
k++;
System.out.print(p.Data + "\t");
if (k % 5 == 0) {
System.out.println("");
k = 0;
}
}
p = p.Next;
}
最后是 "Mynode" 代码页。
Mynode {
public int Data;
public Mynode Next;
public Mynode Prev;
public Mynode (int Data) {
Prev = null;
this.Data = Data;
Next = null;
抱歉代码太长。
要为双链表的 Mylist 实现重载 primeChecker 方法,您需要循环遍历链表的每个元素并在每次遇到素数时递增一个变量,就像您对为数组重载了 primeChecker 方法。我不确定您的 Mylist 双链表的确切实现,但它可能看起来像这样。
public static int primeChecker(Mylist list) {
int PrimeNo = 0;
//insert whatever method here gives you the first node of Mylist
//I inserted start() to demonstrate, that may or may not be right
Mynode p = list.start();
while (p != null)
{
int x = p.Data;
if (isPrime(x))
{
PrimeNo++;
}
p = p.Next;
}
return PrimeNo;
}