接受字符串输入到数组中并检查字符串在数组中的可用性

问题描述 投票:0回答:3

所以我对Java完全陌生,我想创建一个代码来接受来自用户的字符串输入,并将其存储到数组中。之后,在下一条语句中,我将在终端中键入一个值,并且我希望代码将我的字符串输入与数组中的字符串之一进行比较,并在字符串可用时在终端上打印可用,反之亦然。我的代码的第一部分是正确的(希望如此),但是在比较字符串时遇到了问题。我觉得它不检查我在代码中输入的字符串。这是我的代码,有人可以帮我吗?非常感谢。

import java.util.Scanner;
class Course {
 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  String a[] = new String[20] //assuming max 20 strings
  System.out.println("Enter no. of courses");
  int no_of_courses = sc.nextInt(); // number of strings
  if (no_of_courses <= 0)
   System.out.println("Invalid Range")
  else {
   System.out.println("Enter course names:");
   for (int i = 0; i < no_of_courses; i++) {
    a[i] = sc.next(); //accepting string inputs
   }
   System.out.println("Enter the course to be searched:");
   String search = sc.next() //entering a string to search
   for (int i = 0; i < no_of_courses; i++) {
    if (a[i].equals(search)) //I feel the problem is here
    {
     System.out.println(search + "course is available");
     break;
     else
      System.out.println(search + "course is not available");
    }
   }
  }
 }

我希望输出为

<string> course is available

当我的字符串与数组中的字符串匹配时,

<string> course is not available

当我输入的字符串与数组中的字符串不匹配时>>

但是没有输出:(

所以我对Java完全陌生,我想创建一个代码来接受来自用户的字符串输入,并将其存储到数组中。之后,在下一条语句中,我将在终端中键入一个值,...

java arrays string
3个回答
1
投票

我已经修改了您的代码,并在需要解释的地方注释了。仔细检查。


0
投票

我注意到了几件事-您的程序运行到最后吗?当我将其复制/粘贴到我的想法中时,我注意到一些缺失的分号,就像Yhlas所说的那样,您最后的if / else语句语法不正确。


0
投票
class Course {
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        String a[] = new String[20] ;                //assuming max 20 strings
                System.out.println("Enter no. of courses");
        int no_of_courses = sc.nextInt();           // number of strings
        if(no_of_courses <= 0)
            System.out.println("Invalid Range");
            else
            {
                System.out.println("Enter course names:");
                for(int i=0 ; i < no_of_courses ; i++)
                {
                    a[i] = sc.next();                            //accepting string inputs
                }
                System.out.println("Enter the course to be searched:");
                String search = sc.next()   ;             //entering a string to search
                boolean found = false;
                        for(int i = 0 ; i < no_of_courses ; i++)
                        {
                            if(a[i].equalsIgnoreCase(search))                   //I feel the problem is here
                            {
                                **found = true;**
                                break;
                            }   

                            }
                        if(found) {
                            System.out.println(search+ "course is available"); 
                        }else {
                            System.out.println(search+ "course is not available");
                        }
                        }
            }
    }
© www.soinside.com 2019 - 2024. All rights reserved.