簡體   English   中英

JAVA:印刷機打印奇怪的符號?

[英]JAVA: printwriter printing weird symbols?

我正在處理一個從文件讀取數據、將數據放入鏈接節點、使用冒泡排序對鏈接節點進行排序並打印到輸出文件的項目。 每次在冒泡排序中進行交換/交換/傳遞時,我應該計算它並打印出對任何給定列表進行排序所需的交換/交換/傳遞次數。 我的代碼似乎工作正常,它會很好地打印到我的屏幕上,但是在我的輸出文件而不是數字中,我得到了像“Ⴖ”這樣的奇怪符號?? 提前致謝!!

package assignment1;

import java.io.PrintWriter;

public class Sorting {

    private int comparisons;
    private int exchanges;
    private int totalC;
    private int totalE;
    private int totalP;
    private long time;
    private int pass;
    private static PrintWriter printWriter;
    private static int n; //size list 

    public Sorting(int size, PrintWriter pw) {
        n = size;
        printWriter = pw;
    }

}

對我的列表進行排序的方法。 (它已成功排序 - 那里沒有問題。但是,我的變量更新或我對顯示方法的調用必須是不穩定的。它應該在每次“通過”該方法后打印,並打印它每次進行的比較和交換次數通過。然后我應該在最后打印所有內容的總量。這是不起作用的部分。當我嘗試為每次迭代打印變量時,我得到了一長串隨機符號和字母。

public void bubbleSort(LinkedNode head) {

    LinkedNode previous; 
    LinkedNode current; 
    LinkedNode next; 
    comparisons = 0;
    exchanges = 0;
    time = 0;
    pass = 0;
    totalC = 0;
    totalE = 0;
    totalP = 0;

    boolean isSorted = true;

    //if list is empty or only 1 item is in list -> it is sorted
    if (head == null || head.getNext() == null) {
        return;
    }

    long start = System.currentTimeMillis(); //begin count for bubbleSort

    while(isSorted) {
        comparisons = 0;
        exchanges = 0;
        previous = null;
        current = head;
        next = head.getNext();
        isSorted = false;

        while(next != null) {
            comparisons ++; //increment counter for each comparison made

            if (current.getElement() > next.getElement()) {
                if (head == current) {
                    head = next;
                }
                else {
                    previous.setNext(next);
                }
                current.setNext(next.getNext());
                next.setNext(current);
                isSorted = true;
                current = next;
                exchanges++;    

            }
            previous = current;
            current = previous.getNext();
            next = current.getNext();
        }

        pass++; //increment counter for each run through
        totalP += pass;
        totalE += exchanges;
        totalC += comparisons;
    }

    long elapsedTimeMillis = System.currentTimeMillis()-start;
    time = elapsedTimeMillis;
}

唯一正確輸出到我的文件的部分是時間變量。 在我的屏幕上,我得到的傳遞次數為 4,239,但在我的輸出文件中,我得到了奇怪的符號。

void bubbleSortDisplay(LinkedList myList, PrintWriter pw) {
    System.out.printf("time   pass    cmp     exch ");
    printWriter.write("time   pass    cmp     exch ");
    printWriter.write("\n----------------------------\n");

    System.out.printf("\n", time);
    printWriter.write(time + "ms     ");
    System.out.print(time + "ms     ");
    printWriter.write(totalP);
    System.out.print(totalP);
}

問題是PrintWriterwrite(int c)方法將其參數解釋為字符,因此零被寫入文件作為不可見的 NUL 字符(數值 0)而不是所需的“0”字符(數值 48 )。

pw.print(ci)調用pw.print(ci)而不是pw.write(ci) saveCI()應該可以解決問題。

嘗試以這種方式替換您的代碼:

printWriter.write(""+totalP);

(用空字符串連接 totalP)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM