免责声明:这是一项家庭作业
程序目标是:询问一个句子,然后: - 转换大写字母为小写字母(不使用 .toLowercase()) - 删除所有不是 a-z、A-Z 和 0-9 的字符 - 打印新的句子 - ... 还有更多内容,但对此不重要。
好的,我做了什么。
- 我将我的字符串(句子)转换为字符数组。
- 我创建了一个 for 循环来循环遍历我的数组中的所有字符。
- 如果一个字符是大写字母,我就用 ASCII 将它转换为小写字母。
我遇到的问题是: - 看起来我改变了字符 C,但它没有以小写字母的形式存储在我的数组中? - 我如何检测非允许字符,并从我的数组中删除它们?
我的代码:
import java.util.Scanner;
public class sentence {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String zin = "";
System.out.print("Voer een zin in: ");
if (scanner.hasNextLine())
zin = scanner.nextLine().trim();
if (zin.equals("")) {
System.out.print("Geen Invoer!");
System.exit(0);
}
char[] zinArray = zin.toCharArray();
for (int i = 0; i < zinArray.length; i++) {
char c = zinArray[i];
if (c >= 'A' && c <= 'Z') {
c = (char)(c + 32);
} else if (c >= 58 && c <= 64) {
} else if (c >= 91 && c <= 96) {
} else if (c 123 && c <= 126) {
}
}
}
}
有人能指点我一下方向吗?
谢谢 :)