我是編程新手,一直在嘗試通過編寫一個簡單的程序來學習Java的基礎知識,該程序將應用凱撒轉換爲一些文本。我已經能夠做到這一點,迄今爲止我的代碼是這樣的:嘗試塊內的代碼被忽略,爲什麼?
- 詢問用戶他們想要移動文本的單位數。
- 提示用戶輸入一些文本。
- 通過該單位應用凱撒班次並打印結果。
這裏是工作代碼:
import java.util.Scanner;
class Shift{
public static void main(String[] args){
//This will scan for user input.
Scanner sc = new Scanner(System.in);
System.out.print("Shift by this many characters (0-25): ");
int shift = sc.nextInt();
sc.nextLine();//Skips over the whitespace after the integer
System.out.print("Enter Text: ");
String input = sc.nextLine();
sc.close();
//Initialise a character array containing every letter in the alphabet.
char[] alphabetArray = {'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'};
char[] alphabetArrayCaps = {'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'};
//Initialise the two variables that will be used in the next step.
char[] constantArray = input.toCharArray();
char[] output = input.toCharArray();
//Implement a Caesar shift by the given number of units.
for (int i=0; i < constantArray.length; i++){ //cycles through the user input character by character
for (int j=0; j <= 25; j++){ //cycles through the alphabet
if (constantArray[i] == alphabetArray[j]){
output[i] = alphabetArray[(j+shift)%26];
}
else if (constantArray[i] == alphabetArrayCaps[j]){
output[i] = alphabetArrayCaps[(j+shift)%26];
}
}
}
System.out.println(output);
}
}
這段代碼的問題是,當用戶被要求輸入一個整數,就會有一個例外,如果輸入別的。我認爲這將是瞭解處理異常的好地方,並且已經提到this guide關於如何使用try-catch塊來達到這個目的。
我遇到的問題是代碼(下面)似乎完全忽略了我的嘗試塊。我認爲這是因爲我的try塊包含整數「shift」被聲明的行,並且當我向下滾動到代碼中實際使用的「shift」的位置時,我得到一個警告,指出「shift無法解析爲一個變量「,它無法編譯。
下面是導致問題的代碼,唯一的區別是我在try塊中包含了一行,並在它後面添加了一個catch塊,它應該會打印一條錯誤消息(儘管我沒有將代碼編譯尚未有機會玩這個,看看它實際上做了什麼)。
import java.util.Scanner;
class Shift{
public static void main(String[] args){
//This will scan for user input.
Scanner sc = new Scanner(System.in);
System.out.print("Shift by this many characters (0-25): ");
try {
int shift = sc.nextInt();
}
catch (java.util.InputMismatchException e){
System.err.println("InputMismatchException: " + e.getMessage());
}
sc.nextLine();//Skips over the whitespace after the integer
System.out.print("Enter Text: ");
String input = sc.nextLine();
sc.close();
//Initialise a character array containing every letter in the alphabet.
char[] alphabetArray = {'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'};
char[] alphabetArrayCaps = {'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'};
//Initialise the two variables that will be used in the next step.
char[] constantArray = input.toCharArray();
char[] output = input.toCharArray();
//Implement a Caesar shift by the given number of units.
for (int i=0; i < constantArray.length; i++){ //cycles through the user input character by character
for (int j=0; j <= 25; j++){ //cycles through the alphabet
if (constantArray[i] == alphabetArray[j]){
output[i] = alphabetArray[(j+shift)%26];
}
else if (constantArray[i] == alphabetArrayCaps[j]){
output[i] = alphabetArrayCaps[(j+shift)%26];
}
}
}
System.out.println(output);
}
}
那麼,爲什麼這一小小的變化突然停止了「宣告」的「轉移」呢?
不在這裏給出答覆,但它會如果你改變你的char數組聲明爲'char [] alphabetArray =「abcdefghijklmnopqrstuvwxyz」.toCharArray'和'char [] alphabetArrayCaps =「ABCDEFGHIJKLMNOPRSTUVWXYZ」.toCharArray();' – mirvine 2014-11-23 21:35:43
@ itrollin98我在想,當我這樣做時,必須有一個更簡單的方法。這真是一個很棒的提示,謝謝。 – Peachy 2014-11-23 21:43:38