간단한 대체 암호 (a = b, b = c 등)를 사용하여 일반 텍스트 메시지를 암호 텍스트 메시지로 변환하는 코드를 작성했습니다. 공백은 "1"로 대체되었습니다.Does는 여분의 공백, 문자 return 또는 nil이있는 문자열을 생성합니까?
그런 다음 'gets'를 사용하여 다른 메시지를 입력 할 수 있도록하고 싶습니다. 이로 인해 "gets"를 "gets.strip"으로 대체하여 해결 한 문제가 발생했습니다. 가져 오기가 입력 된 문자열에 시작 또는 끝 부분에 공백을 추가하는 경우 왜 문제가 발생 했습니까? 나는 공간을 다루는 방법을 가지고 있었고, 실제로는 그것을 ""사용하는 것인가, 아니면 더 나은 단어가 없기 때문에 무의미한 공간인가 아니면 문자를 반환 할 것인가?
base_alph =[" ", "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"]
code_alph = ["1", "z", "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"]
x=0
base_msgarray = []
code_msgarray = []
puts "type what you want to translate below"
base_msg = gets.strip # without the .strip the program doesn't run
msg_size = base_msg.length
puts "the message is translated as:"
loop do #this section populates an array with all the characters from the gets
break if x >= msg_size
base_msgarray.push(base_msg[x])
x += 1
end
# this section passes in the values of the base_msgarray to |letter| to then determine its key
# in base_alph then that key is passed into code_alph to find the translated letter which
# is then used to populate the code_msgarray
base_msgarray.each do |letter|
code_index = base_alph.index(letter)
code_msgarray.push(code_alph[code_index])
end
# this section displays the values of code_msgarray in a more text-like fashion
code_msgarray.each do |codeletter|
print codeletter
end
gets # for some reason if i dont have this here the terminal window disappears before I can read anything
실제 입력 값과 예상 출력 및 코드 생성의 잘못된 부분을 알려주십시오.차례 차례로 우리는 당신을 빨리 도울 수 있습니다. –