Java 字符串编辑函数循环正在跳过if语句
我正在尝试制作一个程序,让用户可以使用下面列出的3种方法来编辑字符串(替换、删除、插入),它们工作得很好。问题是,一旦我执行了第一个操作,它就会询问他们是否希望同时执行下两个操作,而不是等待下面if语句的输入 我的密码是Java 字符串编辑函数循环正在跳过if语句,java,if-statement,while-loop,Java,If Statement,While Loop,我正在尝试制作一个程序,让用户可以使用下面列出的3种方法来编辑字符串(替换、删除、插入),它们工作得很好。问题是,一旦我执行了第一个操作,它就会询问他们是否希望同时执行下两个操作,而不是等待下面if语句的输入 我的密码是 StringBuilder s = new StringBuilder(""); boolean q = true; System.out.print("Enter a string :"); Strin
StringBuilder s = new StringBuilder("");
boolean q = true;
System.out.print("Enter a string :");
String c = userinput.nextLine();
s.append(c);
while (q = true) {
System.out.print("would you like to replace a character? (Y/N)");
String replace1 = userinput.nextLine();
if (replace1.equals("Y")) {
System.out.print("Which char would you like to replace?");
int r = userinput.nextInt();
System.out.print("What char would you like to replace it with?");
String j = userinput.next();
s.deleteCharAt(r);
s.insert(r, j);
System.out.println("updated string is: " + s);
}
System.out.println("would you like to eliminate a character? (Y/N)");
String replace2 = userinput.nextLine();
if (replace2.equals("Y")) {
System.out.print("Which char would you like to eliminate?");
int r = userinput.nextInt();
s.deleteCharAt(r);
System.out.println("updated string is: " + s);
}
System.out.println("would you like to insert a character? (Y/N)");
String replace3 = userinput.nextLine();
if (replace3.equals("Y")) {
System.out.print("Which char would you like to insert?");
String i = userinput.next();
System.out.print("Where would you like to insert it?");
int r = userinput.nextInt();
s.insert(r, i);
System.out.println("updated string is: " + s);
}
System.out.println("would you like to edit more? (Y/N)");
String quit = userinput.nextLine();
if (quit.equals("N")) {
System.out.println("you have exited the string editor");
System.exit(0);
}
}
}
}
- 输入字符串:sam是否要替换字符?(是/否)是
- 您要替换哪个字符?0
- 你想喝点什么 将其替换为?B
- 更新的字符串是:Bam您想消除吗 一个角色?(是/否)
- 是否要插入一个字符?(是/否)
它不允许我选择删除任何字符,因为当循环条件错误时,它跳过到最后一个操作用于分配值,'='用于比较。我尝试将userinput.nextLine()更改为userinput.next(),一切正常 也可以尝试使用equalsIgnoreCase而不是equals方法
Scanner userinput = new Scanner(System.in);
StringBuilder s = new StringBuilder("");
boolean q = true;
System.out.print("Enter a string :");
String c = userinput.next();
s.append(c);
while (q == true) {
System.out.print("would you like to replace a character? (Y/N)");
String replace1 = userinput.next();
if (replace1.equals("Y")) {
System.out.print("Which char would you like to replace?");
int r = userinput.nextInt();
System.out.print("What char would you like to replace it with?");
String j = userinput.next();
s.deleteCharAt(r);
s.insert(r, j);
System.out.println("updated string is: " + s);
}
System.out.println("would you like to eliminate a character? (Y/N)");
String replace2 = userinput.next();
if (replace2.equals("Y")) {
System.out.print("Which char would you like to eliminate?");
int r = userinput.nextInt();
s.deleteCharAt(r);
System.out.println("updated string is: " + s);
}
System.out.println("would you like to insert a character? (Y/N)");
String replace3 = userinput.next();
if (replace3.equals("Y")) {
System.out.print("Which char would you like to insert?");
String i = userinput.next();
System.out.print("Where would you like to insert it?");
int r = userinput.nextInt();
s.insert(r, i);
System.out.println("updated string is: " + s);
}
System.out.println("would you like to edit more? (Y/N)");
String quit = userinput.next();
if (quit.equals("N")) {
System.out.println("you have exited the string editor");
System.exit(0);
}
}
while(q=true)
对您来说是个错误吗代码您如何知道它跳过了if语句?你调试过了吗?扫描仪
和nextLine
next
和nextInt
通常非常容易出错,因为控制台是基于行的。幸运的是,您可以测试代码片段。