为什么我收到此错误:
AlphaPos.java:15:错误:找不到符号 字母串= kbd.NextLine();
symbol:方法NextLine() location:Scanner类型的变量kbd 1错误
这可能很简单。
提前感谢您的帮助!
import java.util.Scanner;
public class AlphaPos
{
public static void main(String[] args)
{
Scanner kbd = new Scanner(System.in);
int position = 0;
String alphabet = "abcdefghijklmnopqrstuvwxyz";
String alphabetCAP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.print("Please enter an upper or lower case letter:");
String letter = kbd.NextLine();
char L = letter.charAt(0);
if(Character.isLowerCase(L))
{
for(int i = 0 ; i < alphabet.length()-1; i++)
{
if(alphabet.charAt(i) == L)
position = i+1;
}
System.out.print("You entered "+letter+" in upper case it is number "+ position +" in the alphabet.");
}
if(Character.isUpperCase(L))
{
for(int i = 0 ; i < alphabetCAP.length()-1; i++)
{
if(alphabetCAP.charAt(i) == L)
position = i+1;
}
System.out.print(" You entered "+letter+" in upper case it is number "+ position +" in the alphabet.");
}
else
System.out.print(" You entered " +letter+ " and it is no in alphabet.");
}
}
答案 0 :(得分:0)
你的问题的答案是你正在使用Scanner.NextLine()而不是Scanner.nextLine。
但是为了对你的代码发表评论,定义字母表相当愚蠢,然后循环获取位置,为什么不得到字母,强制它是大写或小写,然后带走&#39; a&#39;或者&#39; A&#39;并添加一个,如下所示:
public static void main(String[] args) {
// note the readable names, dont use "kbd" use something meaningful.
Scanner input = new Scanner(System.in);
char letter = input.nextLine().toLowerCase().charAt(0);
int position = (letter - 'a') + 1;
System.out.println(position);
input.close();
}
请注意,您应该真正使用模式来确保输入符合您希望的形式,但我不会为您做任何事情。你可以自己解决这个问题。 https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html