0

暗号化テキストに解読する必要があるテキスト行があります。

私のテキスト行がabc def ghijklm n opq rstu vwx yz

次のような出力が必要です。aei qu c k rvzdhmptxbfjn y glosm

「キー」を 5 と入力したとします。コードは、テキスト ファイルからのテキストの文字列の配列の 5 番目ごとの要素を入力します。

これは私が思いついたコードであり、何をすべきかで壁にぶつかりました。

import java.io.*;
import java.util.*;

public class Files1 {


public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);
    int key;

    System.out.print("Enter file: ");
    String fileName = input.nextLine();
    System.out.print("Please enter your Cipher Key: ");
    key = input.nextInt();

    Scanner inputStream = null;
    System.out.println("File name is: " + fileName);

    try {
        inputStream = new Scanner(new File(fileName));
    } catch (FileNotFoundException e) {
        System.out.println("Error opening the file" + fileName);
        System.exit(0);
    }

    while (inputStream.hasNextLine()) {
        String text = inputStream.nextLine();
        System.out.println(text);

        char arrayText[] = text.toCharArray();
        for (int i = 0; i < arrayText.length; i += key) {
            System.out.print("\n" + arrayText[i]);
        }

    }
}

}

コンソールで何が起こっているかは次のとおりです。

Enter file: abc.txt

File name is: abc.txt
abc def ghijklm n opq rstu vwx yz

a

e

i



q

u
4

3 に答える 3

1

必要なのは循環リストです。

これは、配列を使用した循環リストの非常に単純で大まかな実装です。

import java.util.Iterator;
import java.util.List;

public class CircularList implements Iterator<String> {

    private String[] list;

    private int pointerIndex;

    private int key;

    public CircularList(String[] list, int key) {
        this.list = list;
        pointerIndex = 1 - key;
        this.key = key;
    }

    @Override
    public boolean hasNext() {
        if(list.length == 0){
            return false;
        }
        return true;
    }

    @Override
    public String next() {
        if(pointerIndex + key > list.length) {
            int diff = (list.length-1) - pointerIndex;
            pointerIndex = key - diff;
            return  list[pointerIndex];
        }else {
            pointerIndex = pointerIndex + key;
            return list[pointerIndex];
        }
    }

    @Override
    public void remove() {
        //Do Nothing
    }

}

循環的に反復できるリストができたら、既存の実装をこれに変更できます-

import java.io.*;
import java.util.*;

public class Files1 {

    public static void main(String[] args) {

        System.out.print("Enter file: ");
        Scanner input = new Scanner(System.in);
        String fileName = input.nextLine();
        Scanner inputStream = null;
        System.out.println("" + fileName);

        try {
            inputStream = new Scanner(new File(fileName));
        } catch (FileNotFoundException e) {
            System.out.println("Error opening the file: " + fileName);
            System.exit(0);
        }

        while (inputStream.hasNextLine()) {
            String text = inputStream.nextLine();
            System.out.println(text);

            String[] splits = text.split("");
            CircularList clist = new CircularList(splits, 5);

            for (int i = 0; i < splits.length -1; i += 1) {
                System.out.print("" + clist.next());
            }

        }
    }

}

出力-

Enter file: resources\abc.txt
resources\abc.txt
abc def ghijklm n opq rstu vwx yz
aei qu c k rvzdhmptxbfjn  y glosw

また、暗号の最後の文字は「m」ではなく「w」である必要があります。

于 2013-03-22T02:42:23.723 に答える
0

スペースに何が起こるべきか、またはラップアラウンドが必要なときに何が起こるかを指定しませんが、スペースが重要であり、ラップアラウンドが自然に発生すると仮定します。

for (int i = 0; i < text.length(); i++)
{
    System.out.print(text.charAt((i*5) % text.length()));
}

これは、予想される出力aei qu c k rvzdhmptxbfjn y gloswにエラーがあることを強く示唆しています。

于 2013-03-22T03:13:33.527 に答える
0

java.io をインポートします。; java.util をインポートします。;

パブリック クラス Files1 {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);
    int key;

    System.out.print("Enter file: ");
    String fileName = input.nextLine();
    System.out.print("Please enter your Cipher Key: ");
    key = input.nextInt();

    Scanner inputStream = null;
    System.out.println("File name is: " + fileName);

    try {
        inputStream = new Scanner(new File(fileName));
    } catch (FileNotFoundException e) {
        System.out.println("Error opening the file" + fileName);
        System.exit(0);
    }

    while (inputStream.hasNextLine()) {
        String text = inputStream.nextLine();
        System.out.println(text);
        for (int i = 0; i < text.length(); i++) {
            System.out.print(text.charAt((i * key) % text.length()));
        }

    }
}

}

EJP と Pai に感謝します。

私は多くのことを学びました!

于 2013-03-22T13:17:08.663 に答える