编程新手 - 在 repl.it/CD18/4 中解析 运行 代码时错误到达文件末尾
new to programming - error reached end of file while parsing when running code in repl.it/CD18/4
我是编程新手,正在尝试编写一个脚本,如果输入 s是一个pangram,否则不是pangram。当我编译脚本时出现错误 "reached end of file while parsing." 我相信我有平衡括号。任何帮助将不胜感激。
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 循环,例如查找用户是否键入退出或其他内容,但应该有所帮助
[编辑] 添加了退出条款
我是编程新手,正在尝试编写一个脚本,如果输入 s是一个pangram,否则不是pangram。当我编译脚本时出现错误 "reached end of file while parsing." 我相信我有平衡括号。任何帮助将不胜感激。
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 循环,例如查找用户是否键入退出或其他内容,但应该有所帮助
[编辑] 添加了退出条款