我是编程新手,一直在尝试通过编写一个简单的程序来学习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