我是编程的新手,所以我很想保持这个简单。编译器接受我的代码,但是当我运行程序并输入例如字母A时,我只会遇到大量错误。我之前尝试使用String字母而不是int字母,但我只是编译器错误,说明我无法将字符串转换为字符或其他东西。我真的很困惑,可以使用快速解释和修复,所以我可以得到一个数字。这是我的代码:
import java.util.Scanner;
import java.lang.String;
public class PhoneAlgorithm {
public static void main(String[] args){
int digit = -1;
Scanner in;
in = new Scanner(System.in);
System.out.print("Enter an uppercase letter to find out the corresponding digit on a telephone: ");
int letter;
letter = Integer.parseInt(in.next());
if (letter == 'A' || letter == 'B' || letter == 'C') {
digit = 2; }
else if (letter == 'D' || letter == 'E' || letter == 'F') {
digit = 3; }
else if (letter == 'G' || letter == 'H' || letter == 'I') {
digit = 4; }
else if (letter == 'J' || letter == 'K' || letter == 'L') {
digit = 5; }
else if (letter == 'M' || letter == 'N' || letter == 'O') {
digit = 6; }
else if (letter == 'P' || letter == 'Q' || letter == 'R' || letter == 'S') {
digit = 7; }
else if (letter == 'T' || letter == 'U' || letter == 'V') {
digit = 8; }
else if (letter == 'W' || letter == 'X' || letter == 'Y' || letter == 'Z') {
digit = 9; }
else if (letter >= 'a' && letter >= '3') {
System.out.print("You did not enter a valid uppercase letter. Try again!");
}
if (digit != -1) {
System.out.println("The corresponding digit on your telephone is: " + digit);
}
}
}
答案 0 :(得分:1)
当您使用parseInt(str)
时,如果参数Exception
无法转换为整数,您将获得str
。
您必须使用char
,因为您要将输入与单个字符进行比较:
char letter;
letter = in.nextLine().charAt(0);
str.charAt(index)
返回指定索引处的char值。 答案 1 :(得分:0)
我修改了你的代码,我想这就是你要找的......
import java.util.Scanner;
public class Try {
public static void main(String[] args) {
//declarations
char letter;
int digit=0;
// Asking the user to enterstring
System.out.println("Enter the string");
String enterString;
//creating a scanner object and reading the string
Scanner input = new Scanner(System.in);
enterString= input.next();
System.out.println("Entered string is "+enterString);
int temp=0;
for(int i=0;i<enterString.length();i++){
letter=(char)enterString.codePointAt(i);
if (letter == 'A' || letter == 'B' || letter == 'C') {
digit = digit*10+2; }
else if (letter == 'D' || letter == 'E' || letter == 'F') {
digit = digit*10+3; }
else if (letter == 'G' || letter == 'H' || letter == 'I') {
digit = digit*10+4; }
else if (letter == 'J' || letter == 'K' || letter == 'L') {
digit = digit*10+5; }
else if (letter == 'M' || letter == 'N' || letter == 'O') {
digit = digit*10+6; }
else if (letter == 'P' || letter == 'Q' || letter == 'R' || letter == 'S') {
digit = digit*10+7; }
else if (letter == 'T' || letter == 'U' || letter == 'V') {
digit = digit*10+8; }
else if (letter == 'W' || letter == 'X' || letter == 'Y' || letter == 'Z') {
digit = digit*10+9; }
else if (letter >= 'a' && letter >= '3') {
System.out.print("You did not enter a valid uppercase letter. Try again!");
}
/*if (digit != 0) {
System.out.println("The corresponding digit on your telephone is: " + digit);
}*/
}
if (digit != 0) {
System.out.println("The corresponding digit on your telephone is: " + digit);
}
}
}