我是编程新手,并尝试编写一个脚本,如果输入 s 是一个全语法,则输出包含 pangram 的行,否则不是全语法。当我编译脚本时,我收到错误“解析时到达文件末尾。”我相信我有平衡的括号。任何帮助将不胜感激。
import java.util.*;
class Main {
public static void main(String[] args) {
boolean flag = false;
Scanner key = new Scanner(System.in);
String s = key.nextLine();
String upperCaseStr = s.toUpperCase();
for(char alphabet = 'A'; alphabet <='Z'; alphabet++) {
if(upperCaseStr.indexOf(alphabet)==-1){
flag=true;
break;
}
}
if (flag){
System.out.print("not ");
}
System.out.println("pangram");
}
}
最佳答案
如果您想继续进行多个输入,请尝试此操作:
import java.util.*;
class Main {
public static void main(String[] args) {
boolean continue = true;
while (continue){
boolean flag = false;
Scanner key = new Scanner(System.in);
String s = key.nextLine();
String upperCaseStr = s.toUpperCase();
//if (upperCaseStr.trim() == "QUIT"){
// continue = false;
//} -> don't compare Object's values by their references
continue = "QUIT".equals(s.toUpperCase());
for(char alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
if(upperCaseStr.indexOf(alphabet)==-1){
flag=true;
break;
}
}
if (flag){
System.out.print(s + " is not a pangram");
}
else
System.out.println(s + " is a pangram");
}
}
}
虽然您需要做一些事情来打破 while 循环,例如查找用户是否键入 quit 或其他内容,但应该有所帮助
[编辑]添加退出子句
关于java - 编程新手 - 在 repl.it/CD18/4 中运行代码时解析时出现错误到达文件末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36493304/