正如标题所示,此程序用于查明数字是大写,小写还是数字。我在查找如何编写此程序的运行程序时遇到了一些麻烦。任何帮助将不胜感激:^)
主要课程:
import static java.lang.System.*;
import java.util.Scanner;
public class characteranalyzer
{
private char theChar;
public characteranalyzer(char c)
{
theChar = c;
}
public void setChar(char c)
{
theChar = c;
}
public char getChar()
{
char getChar = theChar;
return getChar;
}
public boolean isUpper()
{
if (((int)theChar)>64&&((int)theChar)<91){
boolean isUpper = true;
return isUpper;
}
else{
boolean isUpper = false;
return isUpper;
}
}
public boolean isLower()
{
if (((int)theChar)>96&&((int)theChar)<123){
boolean isLower= true;
return isLower;
}
else{
boolean isLower= false;
return isLower;
}
}
public boolean isNumber()
{
if (((int)theChar)>48 &&((int)theChar+48)<58){
boolean isNumber = true;
return isNumber;
}
else{
boolean isNumber= false;
return isNumber;
}
}
public int getASCII( )
{
return ((int)theChar);
}
public String toString()
{
if(isNumber()== true && isUpper() == false && isLower() == false){
String chartype = "a number";
return ""+getChar() + " is "+chartype+". ASCII == " + getASCII() + "\n";
}
else if(isNumber() == false && isUpper() == true && isLower() == false){
String chartype = "an uppercase character";
return ""+getChar() + " is "+chartype+". ASCII == " + getASCII() + "\n";
}
else if(isNumber()== false && isUpper() == false && isLower() == true){
String chartype = "a lowercase character";
return ""+getChar() + " is "+chartype+". ASCII == " + getASCII() + "\n";
}
return null;
}
}
RUNNER CLASS:
import static java.lang.System.*;
import java.util.Scanner;
public class characterrunner
{
public static void main ( String[] args )
{
Scanner keyboard = new Scanner(System.in);
out.print("Enter a letter :: ");
char letter = keyboard.next().charAt(0);
characteranalyzer test = new characteranalyzer(letter);
out.println(test); //A
out.print("Enter a letter :: ");
char letter2 = keyboard.next().charAt(0);
characteranalyzer test2 = new characteranalyzer(letter2);
out.println(test2); //l
out.print("Enter a letter :: ");
char letter3 = keyboard.next().charAt(0);
characteranalyzer test3 = new characteranalyzer(letter3);
out.println(test3); //a
out.print("Enter a letter :: ");
char letter4 = keyboard.next().charAt(0);
characteranalyzer test4 = new characteranalyzer(letter4);
out.println(test4); //7
out.print("Enter a letter :: ");
char letter5 = keyboard.next().charAt(0);
characteranalyzer test5 = new characteranalyzer(letter5);
out.println(test5); //D
out.print("Enter a letter :: ");
char letter6 = keyboard.next().charAt(0);
characteranalyzer test6 = new characteranalyzer(letter6);
out.println(test6); //2
out.print("Enter a letter :: ");
char letter7 = keyboard.next().charAt(0);
characteranalyzer test7 = new characteranalyzer(letter7);
out.println(test7); //X
out.print("Enter a letter :: ");
char letter8 = keyboard.next().charAt(0);
characteranalyzer test8 = new characteranalyzer(letter8);
out.println(test8); //Z
out.print("Enter a letter :: ");
char letter9 = keyboard.next().charAt(0);
characteranalyzer test9 = new characteranalyzer(letter9);
out.println(test9); //9
}
}
答案 0 :(得分:2)
您可以检查ASCII值:
如果字符的ascii值介于48和57之间(包括两者),那么它就是一个数字。
如果介于65和90之间(包括两者),那么它是一个大写字母。
如果介于97和122之间(包括两者),那么它是一个小写字母
您可以将此link引用为ASCII表
答案 1 :(得分:2)
我建议您使用Character
,因为它有isUpperCase(char)
,isLowerCase(char)
和isDigit(char)
。你也可以使用像
Scanner keyboard = new Scanner(System.in);
while (true) {
System.out.println("Please enter a letter :: quit to stop");
String str = (keyboard.hasNextLine()) ? keyboard.nextLine()
: "quit";
if (str.equalsIgnoreCase("quit")) {
break;
}
char ch = str.charAt(0);
if (Character.isUpperCase(ch)) {
System.out.printf("%c is uppercase%n", ch);
} else if (Character.isLowerCase(ch)) {
System.out.printf("%c is lowercasee%n", ch);
} else if (Character.isDigit(ch)) {
System.out.printf("%c is a digit%n", ch);
} else {
System.out.printf("%c is not uppercase, lowercase "
+ "or a digit%n", ch);
}
}
当String str
没有更多输入时,? :
conditional operator (or ternary)允许您使用"quit"
初始化Scanner
。