如何在输入后让我的程序执行? [英] How do I get my program to execute after input?

查看:81
本文介绍了如何在输入后让我的程序执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的学校给了我一个为e库创建BlueJ程序的项目。我正在运行线性搜索,虽然编译器没有显示任何错误,但在获取booksearch的输入后,程序仍未执行(在下面的摘录中)。基本上,在取得书的名称后,要搜索的是,没有显示存在或不存在。该程序突然终止。怎么办?



我尝试过:



.. 。

System.out.println(输入要搜索的书:);

字符串booksearch = sc.nextLine();

int j;

for(j = 0; j< 10; j ++)

{

if(booksearch == book [j] )

System.out.println(这本书出现在+ j);

休息;

}

if(j == 10)

System.out.println(找不到书);

...

My school gave me a project to create a BlueJ program for an e library. I was running a linear search and although the compiler doesn't show any error, the program doesn't execute after taking the input for "booksearch" (in the excerpt below). Basically, after taking the name of the book taht is to be searched for, no display of present or absent is there. The program abruptly terminates. What to do?

What I have tried:

...
System.out.println("Enter the book to be searched: ");
String booksearch= sc.nextLine();
int j;
for(j=0;j<10;j++)
{
if (booksearch==book[j])
System.out.println("The book is present at " + j);
break;
}
if(j==10)
System.out.println("Book not found");
...

推荐答案

我在这里看到了多个问题。

1.
I see multiple problem in this.
1.
if (booksearch==book[j])

应该是

if (booksearch.equals(book[j]))



2.错误的中断声明,应该是


2. wrong break statement, it should be

if (booksearch==book[j]) {
        System.out.println("The book is present at " + j);
        break;
}



3.不知道你如何初始化书籍数组。


3. don't know how you initialized book array.


这篇关于如何在输入后让我的程序执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆