1

私はランダム暗号であるこのプログラムを作りました。

public class SaadAbdullahCipher {
      private char[] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',   
                               'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',   
                               'y', 'z'};  
    private String alphabetString = "abcdefghijklmnopqrstuvwxyz";  
    private int[] alphabetASCII = new int[alphabet.length];  
    private int[] cipherOne;  
    private int[] cipherTwo;  
    private String userMessage;  
    private int randomShiftKey;  
    private Formatter x;  

    public SaadAbdullahCipher(String uM, int rSK) {  
        userMessage = uM;  
        randomShiftKey = rSK;  
        cipherOne = new int[userMessage.length()];  
    }  

    public void genNewAlphabet() {  
        for(int i = 0; i < alphabet.length; i++) {  
            alphabetASCII[i] = alphabet[i] + randomShiftKey;  
            if(alphabetASCII[i] >= 122) {  
                alphabetASCII[i] = alphabetASCII[i] - 26;  
            }  
        }  
    }  

    public void cipher() {    
        String[] userMessageString = new String[userMessage.length()];  
        for(int i = 0; i < userMessage.length(); i++) {  
            userMessageString[i] = userMessage.substring(i, i+1);  
        }  

        for(int counterOne = 0; counterOne < userMessageString.length; counterOne++) {  
            cipherOne[counterOne] = alphabetString.indexOf(userMessageString[counterOne]);    
        }  
        cipherTwo = new int[alphabetASCII.length];  
        System.out.print("Your encoded message is: ");  
        for(int counterTwo = 0; counterTwo < cipherOne.length; counterTwo++) {  
            cipherTwo[counterTwo] = alphabetASCII[cipherOne[counterTwo]];  
            System.out.print((char)cipherTwo[counterTwo]);  
        }  
    }  

    public void printTextFile() {  
        try {  
            x = new Formatter("Cipher.txt");  
        }  
        catch(Exception bad) {  
            System.out.println("Your have an air er!");  
        }  
        for(int i = 0; i < cipherTwo.length; i++) {  
            x.format("%s", (char)cipherTwo[i]);  
        }  
        x.close();  
    }  

    public  int[] getAlphabetASCII() {  
        return alphabetASCII;  
    }  
}  

主要():

public class SaadAbdullahCipherTester {
     public static void main(String [] args) {  
        Random ranNum = new Random();  
        Scanner input = new Scanner(System.in);  

        int randomShiftKey = 1 + ranNum.nextInt(24);  

        System.out.print("Please enter your message: ");  
        String userMessage = input.next();    
        System.out.println();  

        SaadAbdullahCipher test = new SaadAbdullahCipher(userMessage, randomShiftKey);  
        test.genNewAlphabet();  
        test.cipher();  
        test.printTextFile();  

        int[] randomABC = test.getAlphabetASCII();  

        SaadAbdullahCipherD testTwo = new SaadAbdullahCipherD(randomShiftKey, randomABC);  
        testTwo.openFile();  
        testTwo.readFile();  
        testTwo.closeFile();  
        testTwo.reverseAlphabet();  
        testTwo.decipher(); 
    }  
} 

すべてが正常に機能していますが、結果を印刷するときに、すべて大文字で表示したい (例: HI HOW ARE YOU) .toUpperCase をどこに追加しますか?

4

2 に答える 2

1

このコード ブロックの代わりに:

 System.out.print("Your encoded message is: ");  
    for(int counterTwo = 0; counterTwo < cipherOne.length; counterTwo++) {  
        cipherTwo[counterTwo] = alphabetASCII[cipherOne[counterTwo]];  
        System.out.print((char)cipherTwo[counterTwo]);  

これは非常に限られているため、文字配列をStringオブジェクトに変換する必要があります。

String ciphertext = new String(cipherTwo);
// Get all the characters into a String object.
System.out.println("your encoded message is: " + ciphertext.toUpperCase());
// Print the output and use the method to make it upper case.
于 2013-04-07T17:19:34.970 に答える
0

toUpperCase() メソッドは String オブジェクトにのみ適用できます。使用するには、文字の配列を String に変換する必要があります。これは、char 配列から String を作成することで実行できます。

String cipherAsStringUpperCase = new String(cipherTwo).toUpperCase();
于 2013-04-07T17:25:12.607 に答える