密码矢量无效

时间:2016-07-17 16:31:13

标签: java vector cryptography

Cryptogram problem descirption photo. 所以这个任务的重点是编写2个向量。一个向量将保持字符串对象的常量向量。然后我们应该将这个第一个向量的元素复制到一个2'向量中。 2'nd矢量应该用星号代替所有字母。之后,允许用户输入他们想要替换的字母,用他们选择的字母,然后我们应该连续显示第一个常量向量,带有2'向量(由用户更改)是输入),而其余的单词是星号。我的问题是,每当我想我替换用户要求的元素并重新打印时,它总是给我带有“*”的相同向量。有谁看到问题可能是什么?任何提示也都有用。 (我仍然是Java和矢量的初学者)。这也是作业的要求:

import java.util.Collections;
import java.util.Scanner;
import java.util.Vector;

public class Cryptogram {
public static void main(String args[]){
    Scanner input = new Scanner(System.in);
    String word = new String("X AQC'M YCQO OKJM BQG OJCM   
    DGM X SJC'M WXZP XM JCB TQVP");
    final Vector<String> vec = new Vector<String>();
    for(int i=0; i<word.length(); i++){
        vec.add(i, Character.toString(word.charAt(i)));
    }
    Vector<String> vec2 = new Vector<String>();
    for(int i=0; i<vec.size(); i++){
        System.out.print(vec.elementAt(i));
        vec2.add(i, vec.elementAt(i));
    }
    System.out.print("\n");
    Collections.fill(vec2,"*");
    vec2.set(1, " ");
    vec2.set(5, "'");
    vec2.set(7, " ");
    vec2.set(12, " ");
    vec2.set(17, " ");
    vec2.set(21, " ");
    vec2.set(26, " ");
    vec2.set(30, " ");
    vec2.set(32, " ");
    vec2.set(36, "'");
    vec2.set(38, " ");
    vec2.set(43, " ");
    vec2.set(46, " ");
    vec2.set(50, " ");
    for(int i=0; i<vec2.size(); i++){
        System.out.print(vec2.elementAt(i));
    }
    boolean flag = true;
    String letter_replace;
    String new_letter;
    System.out.print("\n \n");
    while(flag){
        System.out.print("Replace a letter from encoded message: ");
        letter_replace = input.nextLine();
        System.out.print("Replace with which letter (A-Z,*): ");
        new_letter = input.nextLine();
        System.out.print("\n");
        for(int i=0; i<vec2.size(); i++){
            System.out.print(vec.elementAt(i));
        }
        System.out.print("\n");
        for(int i=0; i<vec2.size(); i++){
            if(vec2.elementAt(i)==letter_replace){
                vec2.setElementAt(new_letter, i);
            }
            System.out.print(vec2.elementAt(i));
        }
        System.out.print("\n \n");
    }
}
}

1 个答案:

答案 0 :(得分:0)

您的问题是您在vec2(星号矢量)中搜索,而您应该在vec中搜索(编码的消息)

import java.util.Collections;
import java.util.Scanner;
import java.util.Vector;

public class Cryptogram {
public static void main(String args[]){
    Scanner input = new Scanner(System.in);
    String word = new String("X AQC'M YCQO OKJM BQG OJCM DGM X SJC'M WXZP XM JCB TQVP");
    final Vector<String> vec = new Vector<String>();
    for(int i=0; i<word.length(); i++){
        vec.add(i, Character.toString(word.charAt(i)));
    }
    Vector<String> vec2 = new Vector<String>();
    for(int i=0; i<vec.size(); i++){
        System.out.print(vec.elementAt(i));
        vec2.add(i, vec.elementAt(i));
    }
    System.out.print("\n");
    Collections.fill(vec2,"*");
    vec2.set(1, " ");
    vec2.set(5, "'");
    vec2.set(7, " ");
    vec2.set(12, " ");
    vec2.set(17, " ");
    vec2.set(21, " ");
    vec2.set(26, " ");
    vec2.set(30, " ");
    vec2.set(32, " ");
    vec2.set(36, "'");
    vec2.set(38, " ");
    vec2.set(43, " ");
    vec2.set(46, " ");
    vec2.set(50, " ");
    for(int i=0; i<vec2.size(); i++){
        System.out.print(vec2.elementAt(i));
    }
    boolean flag = true;
    String letter_replace;
    String new_letter;
    System.out.print("\n \n");
    while(flag){
        System.out.print("Replace a letter from encoded message: ");
        letter_replace = input.nextLine();
        System.out.print("Replace with which letter (A-Z,*): ");
        new_letter = input.nextLine();
        System.out.print("\n");
        for(int i=0; i<vec2.size(); i++){
            System.out.print(vec.elementAt(i));
        }
        System.out.print("\n");
        for(int i=0; i<vec.size(); i++){
            if(vec.elementAt(i).equals(letter_replace)){
                vec2.setElementAt(new_letter, i);
            }
            System.out.print(vec2.elementAt(i));
        }
        System.out.print("\n \n");
    }
}
}