JAVA 扫描程序在使用 bag 时导致 if 语句问题

JAVA scanner causes if statement problems when using bag

提问人:G-Arthur 提问时间:3/19/2021 更新时间:3/20/2021 访问量:75

问:

我是 JAVA 的新手,一直在使用 IDE,每当我尝试检查 bag 是否包含与给定输入相同的字符串时,JAVA 都会将其计为 FALSE,即使 if 语句(例如“is input equal to 1”和“is 1 inside the bag”)传递为 true。这是我的代码摘录,我将不胜感激任何帮助和建议。

          //user input
      System.out.println("Please enter a string (to exit, enter 'exit'): ");
      a=sc.next();
      if (a.equals("1")) {System.out.println("adpkgnosıfbgojadnofabsndofgna");}
      if (ValidAnswers1.contains("1")) {System.out.println("adpkgnosıfbgojadnofabsndofgna");}
      
      
      //error detection. after I learn bag it will become if bag contains string s.
      if (ValidAnswers1.contains(a)) {correct_input=1;} else {correct_input=0;}
      while (correct_input==0) 
      {
          System.out.println("you entered:"+ a+".");
          System.out.println("Please enter a valid string (to exit, enter 'exit')");
          a = sc.next();
          if (ValidAnswers1.contains(a)) {correct_input=1;} else {correct_input=0;}
      }

控制台打印出两个 keymashes,然后转移到 while 循环中。我已经通过使用固定变量进行测试来检查以确保 while 循环正确,但是当使用扫描仪时,它似乎有错误。

Java Eclipse java.util.scanner

评论

0赞 comdotlinux 3/19/2021
它对我有用吗?请输入一个字符串(要退出,请输入“exit”): 1 adpkgnosıfbgojadnofabsndofgna adpkgnosıfbgojadnofabsndofgna 进程完成,退出代码为 0

答:

0赞 Dam 3/19/2021 #1

我不明白,你真正想要的是什么,我做了测试,但工作正常。看看类,也许是变量中的一些错误或其他东西

public class Main {
    
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        String a;
        String ValidAnswers1 = "1";
        int correct_input = 0;
        
      //user input
      System.out.println("Please enter a string (to exit, enter 'exit'): ");
      a = sc.next();
      
      if (a.equals("1")) {
          System.out.println("adpkgnosıfbgojadnofabsndofgna");
      }
      if (ValidAnswers1.contains("1")) {
          System.out.println("adpkgnosıfbgojadnofabsndofgna");
      }
      
      //error detection. after I learn bag it will become if bag contains string s.
      if (ValidAnswers1.contains(a)) {
          correct_input=1;
      } else {
          correct_input=0;
      }
      
      while (correct_input==0) {
          
          System.out.println("you entered:"+ a+".");
          System.out.println("Please enter a valid string (to exit, enter 'exit')");
          a = sc.next();
          
          if (ValidAnswers1.contains(a)) {
              correct_input=1;
          } else {
              correct_input=0;
          }
      }
    }

输出如下:

Please enter a string (to exit, enter 'exit'): 
a
adpkgnos?fbgojadnofabsndofgna
you entered:a.
Please enter a valid string (to exit, enter 'exit')
1
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------

评论

0赞 G-Arthur 3/19/2021
你能用袋子试试吗?在我的代码中,ValidAnswers1 是一个袋子,我认为这是问题的原因。或者把它做成一个像“1 2 3 4 exit”这样的字符串,然后使用 .contains() 会更好吗?
0赞 Dam 3/20/2021
我不知道“bag”这个词,但我相信创建一个更容易的字符串数组会更容易。
0赞 Dam 3/20/2021
我来举个例子
0赞 Dam 3/20/2021
我完成了这个例子,试着理解我做了什么,如果你不明白再问一次,我将“correct_input”的值改为布尔值,因为我认为会更有条理和安全,此外这是一个很好的做法,因为代码只处理两个数字。
0赞 Dam 3/20/2021 #2
      //Create a new Object Scan in the memmory
      Scanner scan = new Scanner(System.in);
      
      String input;
      String[] validAnswers = new String[]{"1","2","3","exit"};
      
      boolean isCorrect = false;

      //The method equalsIgnoreCase means that the text can be in uppercase too;
      /*The number between the tags "[]" means the number in the array since arrays
      starts with number "0" */
      
    do{
          System.out.println("Please enter a string (to exit, enter 'exit'): ");
          input = scan.next();

        if(input.equalsIgnoreCase(validAnswers[0])){
          
          isCorrect = true;
          System.out.println("Number 1");
          
        }else if(input.equalsIgnoreCase(validAnswers[1])){
          isCorrect = true;
          System.out.println("Number 2");
          
        }else if(input.equalsIgnoreCase(validAnswers[2])){
          
          isCorrect = true;    
          System.out.println("Number 3");
          
        }else if(input.equalsIgnoreCase(validAnswers[3])){
          
          isCorrect = true;
          System.out.println("EXIT!");
          
          //You could use the method System.exit(0) to finish the program;
          //If you put "1" in the exit value means that the prgram finished with some error;
          //System.exit(0);
        }else{
        //If the Answers is different from all of the others;
          System.out.println("you entered: " + input +".");
          System.out.println("Please enter a valid string (to exit, enter 'exit')");
        }
    }while(isCorrect != true);
    //I used the method do{}while because it's the only method that will exacute at least once;
  }