Java 接受数组中的字符串输入并检查数组中字符串的可用性
所以我对java完全陌生,我想创建一个代码来接受来自用户的字符串输入,并将其存储到数组中。在下一个语句中,我将在终端中键入一个值,我希望代码将我的字符串输入与数组中的一个字符串进行比较,并在字符串可用时在终端上打印,反之亦然。我的代码的第一部分是正确的(希望如此),但是我在比较字符串时遇到了一个问题。我觉得它不会用我在代码中的输入检查字符串。这是我的密码,有人能帮我吗?非常感谢你Java 接受数组中的字符串输入并检查数组中字符串的可用性,java,arrays,string,Java,Arrays,String,所以我对java完全陌生,我想创建一个代码来接受来自用户的字符串输入,并将其存储到数组中。在下一个语句中,我将在终端中键入一个值,我希望代码将我的字符串输入与数组中的一个字符串进行比较,并在字符串可用时在终端上打印,反之亦然。我的代码的第一部分是正确的(希望如此),但是我在比较字符串时遇到了一个问题。我觉得它不会用我在代码中的输入检查字符串。这是我的密码,有人能帮我吗?非常感谢你 import java.util.Scanner; class Course { public static vo
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");
}
}
}
}
import java.util.Scanner;
班级课程{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
字符串a[]=新字符串[20]//假设最多20个字符串
System.out.println(“输入课程编号”);
int no_of_courses=sc.nextInt();//字符串数
如果(没有任何课程我已经修改了你的代码,并在需要解释的地方发表了评论。请仔细检查
import java.util.Scanner;
class Course {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter no. of courses");
int no_of_courses = sc.nextInt(); // number of strings
String a[] = new String[no_of_courses]; // do not assume when you have proper data.
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 flag = false;
for (int i = 0; i < no_of_courses; i++) {
if (a[i].equals(search)) // I feel the problem is here
{
flag = true;//do not print here. just decide whether course is available or not
break;
}
}
//at the end of for loop check your flag and print accordingly.
if(flag) {
System.out.println(search + "course is available");
}else {
System.out.println(search + "course is not available");
}
}
}
}
import java.util.Scanner;
班级课程{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入课程编号”);
int no_of_courses=sc.nextInt();//字符串数
字符串a[]=新字符串[no_of_courses];//当您有正确的数据时,不要假设。
if(no_of_courses我注意到了一些事情-你的程序运行到底了吗?当我复制/粘贴到我的ide时,我注意到了一些缺少的分号,正如Yhlas所说,你最后的if/else语句语法不正确
这与你的程序是否能给出正确答案无关,但你的最后一个循环会反复打印,因为它会检查a中的每个元素,每次循环并发现不匹配时,它都会打印出一些内容{
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");
}
}
}
}
公共静态void main(字符串[]args)
{
扫描仪sc=新的扫描仪(System.in);
字符串a[]=新字符串[20];//假设最多20个字符串
System.out.println(“输入课程编号”);
int no_of_courses=sc.nextInt();//字符串数
如果(没有课程这真的是一个很好的努力,你几乎成功了。所以有几件事
由于您输入的是课程数,因此只需使用该值初始化数组(在实际需要之前尝试不初始化内容是一种很好的做法)
如果您正在进行String
比较,且区分大小写并不重要,请使用.equalsIgnoreCase(String)
为了解决您的问题,您只需要一个布尔
变量来指示是否已找到匹配项。最初,这将是假
(未找到匹配项),您将在数组中运行,直到找到匹配项。一旦找到,将标记为真
,并将中断循环(你做得对)
只有在完成循环后,才能打印出是否找到匹配项
看看:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
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 {
String a[] = new String[no_of_courses];
System.out.println("Enter course names:");
for (int i = 0; i < a.length; 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 courseFound = Boolean.FALSE;
for(int i = 0; i < a.length; i++) {
if (a[i].equalsIgnoreCase(search)) {
courseFound = Boolean.TRUE;
break;
}
}
if(courseFound) {
System.out.println(search + "course is available");
} else {
System.out.println(search + " course is not available");
}
}
}
我认为“if/else”部分的语法是错误的。它应该是以下格式:if(condition){…code}else{..code}嗯,我实际上认为在将我的字符串输入与数组中已经存在的字符串输入进行比较时存在问题。因为字符串是引用类型,点等于方法有点复杂。事实上,由于我今天刚加入stackoverflow,我无法投票,我需要15分钟的声誉,但我接受了答案:)是的,实际上它运行到了最后,但它抛出了一些例外。哇!这是一个非常详细的解释。我现在明白我错在哪里了。非常感谢没有问题,请勾选左角的已回答复选框。
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");
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
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 {
String a[] = new String[no_of_courses];
System.out.println("Enter course names:");
for (int i = 0; i < a.length; 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 courseFound = Boolean.FALSE;
for(int i = 0; i < a.length; i++) {
if (a[i].equalsIgnoreCase(search)) {
courseFound = Boolean.TRUE;
break;
}
}
if(courseFound) {
System.out.println(search + "course is available");
} else {
System.out.println(search + " course is not available");
}
}
}
if(Arrays.stream(a).anyMatch(i -> i.equalsIgnoreCase(search))) {
System.out.println(search + " course is available");
} else {
System.out.println(search + " course is not available");
}