英文:
check two conditions in if statement
问题
我正在检查两个条件,并且我需要两者都起作用,
但只有其中一个起作用。我在哪里犯了错误?我需要打印出字符串文本中ch1和ch2的位置索引。
import java.util.Scanner;
public class TestIndexOf {
private static String text;
private static char ch1, ch2;
public static void main(String[] args) {
TestIndexOf test = new TestIndexOf();
test.getInput();
System.out.println(test.getIndex(text, ch1, ch2));
}
public static void getInput() {
Scanner scan = new Scanner(System.in);
System.out.println("输入单词和字符:");
text = scan.nextLine();
ch1 = scan.next().charAt(0);
ch2 = scan.next().charAt(0);
}
public static int getIndex(String text, char ch1, char ch2) {
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == ch1) {
return i;
}
if (text.charAt(i) == ch2) {
return i;
}
}
return -1;
}
}
英文:
I am checking for 2 conditions, and I need that both of them would work,
but work only one of them. Where I am making the mistake? I need to print indexes of positions of ch1 and ch2 in String text
import java.util.Scanner;
public class TestIndexOf {
private static String text;
private static char ch1, ch2;
public static void main(String[] args) {
TestIndexOf test = new TestIndexOf();
test.getInput();
System.out.println(test.getIndex(text, ch1, ch2));
}
public static void getInput() {
Scanner scan = new Scanner(System.in);
System.out.println("Enter word and chars: ");
text = scan.nextLine();
ch1 = scan.next().charAt(0);
ch2 = scan.next().charAt(0);
}
public static int getIndex(String text, char ch1, char ch2) {
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == ch1) {
return i;
}
if (text.charAt(i) == ch2) {
return i;
}
}
return -1;
}
}
答案1
得分: 2
如果我理解正确,您想要知道 char1 和 char2 的位置。
根据您编写的逻辑,它只能返回一个值。
您需要移除返回语句并在某个变量中收集结果。
然后在最后返回该变量。
或者类似这样的方式应该可以工作:
public class TestIndexOf {
public static void printIndex(String text, char ch1, char ch2) {
int count = 0;
boolean isCharAIndexNotPrinted = true;
boolean isCharBIndexNotPrinted = true;
for (int i = 0; i < text.length(); i++) {
if (count == 2)
break;
if (text.charAt(i) == ch1 && isCharAIndexNotPrinted) {
count++;
isCharAIndexNotPrinted = false;
System.out.println("char1 is " + i);
}
if (text.charAt(i) == ch2 && isCharBIndexNotPrinted) {
count++;
isCharBIndexNotPrinted = false;
System.out.println("char2 is " + i);
}
}
}
}
英文:
If I understand correctly, you want to know the position of both char1 and char2.
The way you have written your logic, it can only return one value.
You need to remove the return statement and collect the results in some variable.
Then return that variable instead at the end.
or something like this should work:
public class TestIndexOf {
// private static String text;
// private static char ch1, ch2;
public static void printIndex(String text, char ch1, char ch2) {
int count = 0;
boolean isCharAIndexNotPrinted = true;
boolean isCharBIndexNotPrinted = true;
for (int i = 0; i < text.length(); i++) {
if(count==2)
break;
if (text.charAt(i) == ch1 && isCharAIndexNotPrinted) {
count++;
isCharAIndexNotPrinted = false;
System.out.println("char1 is " + i);
}
if (text.charAt(i) == ch2 && isCharBIndexNotPrinted) {
count++;
isCharBIndexNotPrinted = false;
System.out.println("char2 is " + i);
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论