我是编程的新手,并且一直在尝试通过编写一个简单的将Caesar转换应用于某些文本的程序来学习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);
}
}
此代码的问题在于,当要求用户输入整数时,如果输入了其他任何内容,则会出现异常。我认为这将是学习处理异常的好地方,并且一直在参考本指南中有关如何为此使用try-catch块的信息。
我遇到的问题是代码(如下)似乎完全忽略了我的try块。我认为这是因为我的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);
}
}
那么,为什么这一小小的变化突然阻止了“转变”的宣布呢?
变量仅限于声明它们的作用域。有关更多信息,请参见关于Java中变量作用域的这个不错的小教程(或者,如果您想获得技术方面的知识,请参阅JLS第6.3节,在这种情况下,以“ The scope of ...本地变量声明”)。
范围的最简单解释{ ... }
是声明它们的对。
在您的情况下:
...
try {
int shift = sc.nextInt();
} ...
该变量shift
在{ ... }
该try
块的外部不可见。您将不得不在更高的范围内声明它,例如,作为方法的局部变量。但是,在try
块的情况下,如果仅将声明移到外面,您仍然会遇到“可能未使用变量的变量”警告,因为在这种情况下:
int shift;
try {
shift = sc.nextInt();
} catch (...) {
...
}
nextInt()
引发异常的代码路径仍然可以保持shift
未初始化状态。在这种情况下,要解决此问题,一种方法是对其进行初始化:
int shift = 0;
try {
shift = sc.nextInt();
} catch (...) {
...
}
另一个选择是确保即使抛出异常也可以获取值:
int shift;
try {
shift = sc.nextInt();
} catch (...) {
shift = 0;
...
}
第三种选择是,以这样一种方式来构造代码,即shift
在引发异常时也不会尝试使用它,尽管这并不完全适合您的示例(但出于完整性考虑):
int shift;
try {
shift = sc.nextInt();
} catch (Exception x) {
throw x;
}
// shift can never be used uninitialized here
第四种选择是,以shift
无需在try
块外使用的方式来构造代码:
try {
int shift = sc.nextInt();
// do everything that needs to be done with shift here
} catch (...) {
...
}
// shift is unneeded here
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句