2012-09-08 6 views
0

내 프로그램이 끝날 무렵 사용자 입력을 문자라고하는 문자 배열로 바꾸 었습니다. 나는 편지를 통해 반복하고 배열의 각 "문자"에 대해 shiftCode의 값을 더하거나 뺍니다. shiftCode는 양수 또는 음수 일 수 있습니다. 나는 "편지"의 첫 글자에 1을 더하는 역할을하는 작은 부분을 가지고있다.사용자가 문자를 늘리거나 줄이려면 CharToArray와 foreach를 사용하십시오.

누군가 편지에서 각 문자를 반복하고 shiftCode 값을 사용하여 더하거나 뺄 때 i ++ 사용법을 알려주시겠습니까?

는 나는 내가 각 문자를 통해 shiftCode의 값을 반복하는 방법을 알아낼 수없는 것

for(shiftCode; shiftCode === 26; shiftCode++) { 
    letter[EVERY LETTER IN THIS THING?] += shiftCode; 
} 

과 같을 것이라 생각합니다. 누군가가 올바른 방향으로 나를 가리킬 수 있다면 크게 감사 할 것입니다.

, 감사합니다 아론

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

/* 
* This program is designed to - 
* Work as a Ceasar Cipher 
*/ 

/** 
* 
* 
*/ 
public class Prog3 { 
    static String codeWord; 
    static int shiftCode; 
    static int i; 
    static char[] letter; 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) throws IOException { 
     // Instantiating that Buffer Class 
     // We are going to use this to read data from the user; in buffer 
     // For performance related reasons 
     BufferedReader reader; 

     // Building the reader variable here 
     // Just a basic input buffer (Holds things for us) 
     reader = new BufferedReader(new InputStreamReader(System.in)); 

     // Java speaks to us here/We get it to query our user 
     System.out.print("Please enter text to encrypt: "); 

     // Try to get their input here 
     try {  
      // Get their codeword using the reader 
      codeWord = reader.readLine(); 

      // What ever they give us is probably wrong anyways. 
      // Make that input lowercase 
      codeWord = codeWord.toUpperCase(); 
      letter = codeWord.toCharArray(); 
     } 
     // If they messed up the input we let them know here and end the prog. 
     catch(Throwable t) { 
      System.out.println(t.toString()); 
      System.out.println("You broke it. But you impressed me because" 
        + "I don't know how you did it!"); 
     } 

     // Java Speaks/Lets get their desired shift value 
     System.out.print("Please enter the shift value: "); 

     // Try for their input 
     try { 
       // We get their number here 
       shiftCode = Integer.parseInt(reader.readLine()); 
     } 
     // Again; if the user broke it. We let them know. 
     catch(java.lang.NumberFormatException ioe) { 
      System.out.println(ioe.toString()); 
      System.out.println("How did you break this? Use a number next time!"); 
     } 
     letter[1] += 1; 
     System.out.println(letter[1]); 
    } 
} 
+0

['for']에 대한 Java Tutorials 문서 (http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html)를 읽어보십시오. –

+0

정말 고마워요!if (shiftCode <0) { letter [i] - = shiftCode;에 대한 – ILikeTurtles

답변

2

다음은 배열을 반복 할 수있는 하나의 방법입니다.

for(int i = 0; i < letter.length; i++) { 
     // using i, you can manipulate and access all elements of the array. 
     letter[i] -= shiftCode; // may want more logic in this case. 
    } 

또한 오류 상태를 잘 처리하지 못합니다. 을 모두 try...catch 블록 안에 reader으로 처리하는 코드의을 감싸 야합니다.

+0

(i = 0; i 0) { letter [i] + = shiftCode; } System.out.println (letter [i]); } 반환 시프트 값을 입력하십시오 : -5 F F W T S 을하지만 때 나는에 음수를 넣어; 그것은 부정을 사용하지 않습니다. 그것이 부정을 이해하고 사용하게하는 방법이 있습니까? – ILikeTurtles

관련 문제