andy6804tw
7/18/2016 - 9:00 AM

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=13952 這題除了英文字母大小寫之外其餘照輸出要考慮到字母越界的問題所以當超出大小寫z時要回到a故-26反之

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=13952

這題除了英文字母大小寫之外其餘照輸出要考慮到字母越界的問題所以當超出大小寫z時要回到a故-26反之

import java.util.*;    
    
public class Main {    
    
    public static void main(String[] args) {    
        Scanner scn = new Scanner(System.in);    
        int n = Integer.parseInt(scn.nextLine()), code = Integer.parseInt(scn.nextLine()), tot;    
        System.out.printf("%d\n%d\n",n,code);   
        if(n>26)n%=26;  
        while (n < 0) {  
            n += 26;  
        }  
        while (scn.hasNext()) {    
            String str = scn.nextLine();    
            char c[] = str.toCharArray();    
            for (int i = 0; i < c.length; i++) {    
                tot = c[i];    
                if (c[i] >= 97 && c[i] <= 122) {    
                    if (code == 0)    
                        tot = c[i] + n;    
                    else    
                        tot = c[i] - n;    
                    if (tot > 122)    
                        tot -= 26;    
                    if (tot < 97)    
                        tot += 26;    
                } else if (c[i] >= 65 && c[i] <= 90) {    
                    if (code == 0)    
                        tot = c[i] + n;    
                    else    
                        tot = c[i] - n;    
                    if (tot > 90)    
                        tot -= 26;    
                    if (tot < 65)    
                        tot += 26;    
                }    
                System.out.printf("%c", tot);    
            }    
            System.out.printf("\n");    
        }    
    }    
    	/* 
    題目:UTSA Basic Problem 2. 編碼器
    作者:1010
    時間:西元 2016 年 7 月 */
}