检查 if 语句中的两个条件。

huangapple go评论85阅读模式
英文:

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(&quot;Enter word and chars: &quot;);
        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 &lt; 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 &lt; text.length(); i++) {
              if(count==2)
                 break;
              if (text.charAt(i) == ch1 &amp;&amp; isCharAIndexNotPrinted) {
                count++;
                isCharAIndexNotPrinted = false;
                System.out.println(&quot;char1 is &quot; + i);
              }
               if (text.charAt(i) == ch2 &amp;&amp; isCharBIndexNotPrinted) {
                count++;
                isCharBIndexNotPrinted = false;
                System.out.println(&quot;char2 is &quot; + i);
              }
          }
      }
    }

huangapple
  • 本文由 发表于 2020年8月21日 18:14:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63520920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定