Java 逻辑错误导致语句在While循环中打印两次
因此,我目前遇到的问题是,在我完成所有步骤后,“输入您的命令(反向、先替换、后替换、全部删除、删除)”语句会打印两次 我认为正在发生的是循环执行了两次,但我不知道为什么。如果您能帮助解决这个问题,我们将不胜感激。如果我的代码格式不好,请提前道歉,因为我仍在学习如何正确设置格式Java 逻辑错误导致语句在While循环中打印两次,java,Java,因此,我目前遇到的问题是,在我完成所有步骤后,“输入您的命令(反向、先替换、后替换、全部删除、删除)”语句会打印两次 我认为正在发生的是循环执行了两次,但我不知道为什么。如果您能帮助解决这个问题,我们将不胜感激。如果我的代码格式不好,请提前道歉,因为我仍在学习如何正确设置格式 import java.util.Scanner; public class StringChangerenter { public static void main(String[] args) {
import java.util.Scanner;
public class StringChangerenter {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
// Output Variables
String userInput = "";
// Variables
String removeChar = "", removeAllChar = "";
int removeIndex = 0;
// First Output
System.out.println("Enter the string to be manipulated");
userInput = keyboard.nextLine();
String command = "";
// While loop
while (!command.equalsIgnoreCase("quit")) {
// Output
System.out.println("Enter your command (reverse, replace first, replace last, remove all, remove)");
command = keyboard.nextLine();
if (command.equalsIgnoreCase("remove")) {
System.out.println("Enter the character to remove");
removeChar = keyboard.nextLine();
int totalCount = 0;
for (int j = 0; j < userInput.length(); j++) {
if (userInput.charAt(j) == removeChar.charAt(0)) {
totalCount = totalCount + 1;
}
}
System.out.println("Enter the " + removeChar
+ " you would like to remove (Not the index - 1 = 1st, 2 = 2nd, etc.):");
removeIndex = keyboard.nextInt();
int currentIndex = 1;
if (removeIndex <= totalCount) {
for (int i = 0; i < userInput.length(); i++) {
if (userInput.charAt(i) == removeChar.charAt(0)) {
if (currentIndex == removeIndex) {
String firstpartOfString = userInput.substring(0, i);
String secondpartOfString = userInput.substring(i + 1, userInput.length());
System.out.println("The new sentence is " + firstpartOfString + secondpartOfString);
userInput = firstpartOfString + secondpartOfString;
break;
} else {
currentIndex = currentIndex + 1;
}
}
}
} else {
System.out.println("Can't find " + removeChar + " occuring at " + removeIndex + " int the string.");
}
// Remove All Code
} else if (command.equalsIgnoreCase("remove all")) {
System.out.println("Enter the character to remove");
removeAllChar = keyboard.next();
String newString = "";
for (int i = 0; i < userInput.length(); i++) {
if (userInput.charAt(i) != removeAllChar.charAt(0)) {
newString = newString + userInput.charAt(i);
}
}
userInput = newString;
System.out.println("The new sentence is " + userInput);
}
// Bracket for while loop
}
}
}
import java.util.Scanner;
公共类StringChangerenter{
公共静态void main(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
//输出变量
字符串userInput=“”;
//变数
字符串removeChar=“”,removeAllChar=“”;
int-removeIndex=0;
//第一输出
System.out.println(“输入要操作的字符串”);
userInput=keyboard.nextLine();
String命令=”;
//While循环
而(!command.equalsIgnoreCase(“quit”)){
//输出
System.out.println(“输入命令(反转、先替换、后替换、全部删除、删除)”;
command=keyboard.nextLine();
if(command.equalsIgnoreCase(“remove”)){
System.out.println(“输入要删除的字符”);
removeChar=keyboard.nextLine();
int totalCount=0;
对于(int j=0;j 如果(removeIndex发生的情况是,while循环的条件是
while (!command.equalsIgnoreCase("quit"))
这在英语中的意思是,只要command不等于“quit”,就运行这个循环
在循环内部,命令实际上从未设置为“quit”。例如,如果我将输入字符串设置为“abcde”,并要求删除位置1处的“c”。
然后,逻辑将命令设置为“删除”
然后将最终值打印为“abde”。现在,当循环结束时,命令仍然是“remove”,因此循环再次执行
一个可能的解决方案是显式询问用户是否希望使用do while循环重试。另外,我只是一个提示,我知道您使用了nextLine。建议在next int之后立即使用nextLine。请参阅以下内容以了解原因:
如果要运行任何其他命令,请明确获得用户同意,这就是您编写的代码:
public static void main (String[] args) throws java.lang.Exception
{
Scanner keyboard = new Scanner(System.in);
// Output Variables
String userInput = "";
// Variables
String removeChar = "", removeAllChar = "";
int removeIndex = 0;
// First Output
System.out.println("Enter the string to be manipulated");
userInput = keyboard.nextLine();
String command = "";
String retry = "";
// While loop
do {
// Output
System.out.println("Enter your command (reverse, replace first, replace last, remove all, remove)");
command = keyboard.nextLine();
if (command.equalsIgnoreCase("remove")) {
System.out.println("Enter the character to remove");
removeChar = keyboard.nextLine();
int totalCount = 0;
for (int j = 0; j < userInput.length(); j++) {
if (userInput.charAt(j) == removeChar.charAt(0)) {
totalCount = totalCount + 1;
}
}
System.out.println("Enter the " + removeChar
+ " you would like to remove (Not the index - 1 = 1st, 2 = 2nd, etc.):");
removeIndex = keyboard.nextInt();
keyboard.nextLine();
int currentIndex = 1;
if (removeIndex <= totalCount) {
for (int i = 0; i < userInput.length(); i++) {
if (userInput.charAt(i) == removeChar.charAt(0)) {
if (currentIndex == removeIndex) {
String firstpartOfString = userInput.substring(0, i);
String secondpartOfString = userInput.substring(i + 1, userInput.length());
System.out.println("The new sentence is " + firstpartOfString + secondpartOfString);
userInput = firstpartOfString + secondpartOfString;
break;
} else {
currentIndex = currentIndex + 1;
}
}
}
} else {
System.out.println("Can't find " + removeChar + " occuring at " + removeIndex + " int the string.");
}
// Remove All Code
} else if (command.equalsIgnoreCase("remove all")) {
System.out.println("Enter the character to remove");
removeAllChar = keyboard.next();
String newString = "";
for (int i = 0; i < userInput.length(); i++) {
if (userInput.charAt(i) != removeAllChar.charAt(0)) {
newString = newString + userInput.charAt(i);
}
}
userInput = newString;
System.out.println("The new sentence is " + userInput);
}
System.out.println("Do you want to go again?");
retry = keyboard.nextLine();
// Bracket for while loop
}while("yes".equalsIgnoreCase(retry));
}
publicstaticvoidmain(字符串[]args)抛出java.lang.Exception
{
扫描仪键盘=新扫描仪(System.in);
//输出变量
字符串userInput=“”;
//变数
字符串removeChar=“”,removeAllChar=“”;
int-removeIndex=0;
//第一输出
System.out.println(“输入要操作的字符串”);
userInput=keyboard.nextLine();
String命令=”;
字符串retry=“”;
//While循环
做{
//输出
System.out.println(“输入命令(反转、先替换、后替换、全部删除、删除)”;
command=keyboard.nextLine();
if(command.equalsIgnoreCase(“remove”)){
System.out.println(“输入要删除的字符”);
removeChar=keyboard.nextLine();
int totalCount=0;
对于(int j=0;j 如果(removeIndex发生的情况是,while循环的条件是
while (!command.equalsIgnoreCase("quit"))
这在英语中的意思是,只要command不等于“quit”,就运行这个循环
在循环内部,命令实际上从未设置为“quit”。例如,如果我将输入字符串设置为“abcde”,并要求删除位置1处的“c”。
然后,逻辑将命令设置为“删除”
然后将最终值打印为“abde”。现在,当循环结束时,命令仍然是“remove”,因此循环再次执行
一个可能的解决方案是显式询问用户是否希望使用do while循环重试。另外,我只是一个提示,我知道您使用了nextLine。建议在next int之后立即使用nextLine。请参阅以下内容以了解原因:
如果要运行任何其他命令,请明确获得用户同意,这就是您编写的代码:
public static void main (String[] args) throws java.lang.Exception
{
Scanner keyboard = new Scanner(System.in);
// Output Variables
String userInput = "";
// Variables
String removeChar = "", removeAllChar = "";
int removeIndex = 0;
// First Output
System.out.println("Enter the string to be manipulated");
userInput = keyboard.nextLine();
String command = "";
String retry = "";
// While loop
do {
// Output
System.out.println("Enter your command (reverse, replace first, replace last, remove all, remove)");
command = keyboard.nextLine();
if (command.equalsIgnoreCase("remove")) {
System.out.println("Enter the character to remove");
removeChar = keyboard.nextLine();
int totalCount = 0;
for (int j = 0; j < userInput.length(); j++) {
if (userInput.charAt(j) == removeChar.charAt(0)) {
totalCount = totalCount + 1;
}
}
System.out.println("Enter the " + removeChar
+ " you would like to remove (Not the index - 1 = 1st, 2 = 2nd, etc.):");
removeIndex = keyboard.nextInt();
keyboard.nextLine();
int currentIndex = 1;
if (removeIndex <= totalCount) {
for (int i = 0; i < userInput.length(); i++) {
if (userInput.charAt(i) == removeChar.charAt(0)) {
if (currentIndex == removeIndex) {
String firstpartOfString = userInput.substring(0, i);
String secondpartOfString = userInput.substring(i + 1, userInput.length());
System.out.println("The new sentence is " + firstpartOfString + secondpartOfString);
userInput = firstpartOfString + secondpartOfString;
break;
} else {
currentIndex = currentIndex + 1;
}
}
}
} else {
System.out.println("Can't find " + removeChar + " occuring at " + removeIndex + " int the string.");
}
// Remove All Code
} else if (command.equalsIgnoreCase("remove all")) {
System.out.println("Enter the character to remove");
removeAllChar = keyboard.next();
String newString = "";
for (int i = 0; i < userInput.length(); i++) {
if (userInput.charAt(i) != removeAllChar.charAt(0)) {
newString = newString + userInput.charAt(i);
}
}
userInput = newString;
System.out.println("The new sentence is " + userInput);
}
System.out.println("Do you want to go again?");
retry = keyboard.nextLine();
// Bracket for while loop
}while("yes".equalsIgnoreCase(retry));
}
publicstaticvoidmain(字符串[]args)抛出java.lang.Exception
{
扫描仪键盘=新扫描仪(System.in);
//输出变量
字符串userInput=“”;
//变数
字符串removeChar=“”,removeAllChar=“”;
int-removeIndex=0;
//第一输出
System.out.println(“输入要操作的字符串”);
userInput=keyboard.nextLine();
String命令=”;
字符串retry=“”;
//While循环
做{
//输出
System.out.println(“输入命令(反转、先替换、后替换、全部删除、删除)”;
command=keyboard.nextLine();
if(command.equalsIgnoreCase(“remove”)){
System.out.println(“输入要删除的字符”);
removeChar=keyboard.nextLine();
int totalCount=0;
对于(int j=0;j