Java:For循环和If算法
我从一个作业中得到了这个问题,该作业使用Store.java和Book.java创建了一个出租书籍的商店。我已经完成了这项作业,但我很想对某一特定部分使用更好的算法 -- Book.javaJava:For循环和If算法,java,algorithm,loops,if-statement,Java,Algorithm,Loops,If Statement,我从一个作业中得到了这个问题,该作业使用Store.java和Book.java创建了一个出租书籍的商店。我已经完成了这项作业,但我很想对某一特定部分使用更好的算法 -- Book.java public class Book { private String name; Book(String name) this.name = name; public String getName() return name; } Store
public class Book {
private String name;
Book(String name)
this.name = name;
public String getName()
return name;
}
Store.java
public class Book {
private String name;
Book(String name)
this.name = name;
public String getName()
return name;
}
内干管()
假设,输入=魔鬼
现在,我需要做一个简单的搜索来检查特定的书是否存在
例如:
for(int i = 0; i < bookObj.length; i++) {
if(bookObj[i].getName().equals(input))
System.out.println("Book Found!");
}
for(int i = 0; i < bookObj.length; i++) {
if(bookObj[i].getName().equals(input))
System.out.println("Book Found!");
else
System.out.println("Book not Found!");
}
for(int i=0;i
显然,这是一个for循环,循环遍历对象数组并检查此类书籍是否存在。现在,当我想给出一个没有找到书的输出时,问题就出现了
例如:
for(int i = 0; i < bookObj.length; i++) {
if(bookObj[i].getName().equals(input))
System.out.println("Book Found!");
}
for(int i = 0; i < bookObj.length; i++) {
if(bookObj[i].getName().equals(input))
System.out.println("Book Found!");
else
System.out.println("Book not Found!");
}
for(int i=0;i
上述代码的问题是,找不到的书将被打印三次。我的目标是避免这样的问题。对此,我确实有解决方案,但我仍在寻找更好的方法,利用getName(),在我看来还有改进的余地
通常,在结构化编程中,我会执行以下操作:
for(int i = 0; i < bookObj.length; i++) {
if(bookObj[i].getName().equals(input))
System.out.println("Book Found!");
else if(i == bookObj.length - 1)
System.out.println("Book not Found!");
}
for(int i=0;i
这有助于判断循环是否已结束,搜索是否已结束,但搜索没有成功的结果
我应该如何以面向对象的方式来看待它
总之,我的问题是
您应该在数组中循环,并使用索引/布尔标志来存储是否找到了该书。然后根据索引/标志值在最后打印消息
int foundAtIndex = -1;
for(int i = 0; i < bookObj.length; i++) {
if(bookObj[i].getName().equals(input)) {
foundAtIndex = i; // store the actual index for later use
break; // no need to search further
}
}
if(foundAtIndex >= 0)
System.out.println("Book Found!");
else
System.out.println("Book not Found!");
int foundAtIndex=-1;
for(int i=0;i=0)
System.out.println(“找到书!”);
其他的
System.out.println(“找不到书!”);
或者(除非你的作业特别要求使用数组),你应该选择集合
,只需一次调用即可完成搜索
我应该如何以面向对象的方式来看待它
当查看单个方法时,过程式和OO风格之间并没有太大区别。当试图组织一组概念上相关的数据和对这些数据和方法进行操作时,差异开始出现在更高的层次上
OO范式是将方法与它们所操作的数据绑定在一起,并将两者封装到一致的对象和类中。这些类最好是重要领域概念的表示。因此,对于您的书店,您可能希望将所有与图书相关的代码放入您的book
类中。但是,上述搜索方法(及其操作的图书集合)与任何特定图书实例无关,因此您有不同的选择:
- 将藏书和搜索方法放入
(可能是普通会员),或商店
- 将它们作为
成员放入static
中Book
第一个选择更自然,所以我通常更喜欢这样。然而,在特定情况下,第二种选择可能更可取。在(OO)设计中,几乎没有明确的“是/否”答案——而是在不同的选项之间进行权衡,每个选项都有自己的优缺点。你可以介绍状态并记住你是否找到了这本书 如果未使用Java 1.4或更早版本,还可以使用foreach循环语法:
boolean bookFound = false;
for(Book currentBook : bookObj) {
if(currentBook.getName().equals(input))
//TODO: see above
}
此外,我建议查看,并用列表或集合替换阵列:
Set<Book> books = new HashSet<Book>();
books.put(new Book("Game Over"));
books.put(new Book("Shrek"));
books.put(new Book("Ghost"));
Set books=new HashSet();
书。放(新书(“游戏结束”);
书。放(新书(“史莱克”);
书。放(新书(“鬼”);
而且,当我们在读这本书时,您还可以考虑两本书何时相等,并相应地覆盖equals()和hashCode()。如果将equal()更改为检查标题,则只需使用
books.contains(新书(输入))编码>并让库为您完成工作。要更好地解决这个问题,您必须了解Java的强大不是来自语言本身,而是来自Java框架
您应该学习类的用法(不再使用数组)。然后,只需一行代码即可解决搜索问题:
ArrayList<Book> listOfBooks;
// init your list here
listOfBooks.contains(new Book(input));
arraylistofbooks;
//在这里列出你的清单
包含(新书(输入));
要使这项工作正常进行,还必须学习如何正确实现Book类的equals()方法
学习愉快 以下是一个有效的解决方案:
import java.util.Scanner;
public class Store {
private static class Book {
private String name;
Book(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public static void main(String[] args) {
String input;
Book[] bookObj = new Book[3];
bookObj[0] = new Book("Game Over");
bookObj[1] = new Book("Shrek");
bookObj[2] = new Book("Ghost");
Scanner console = new Scanner(System.in);
input = console.nextLine();
boolean found = false;
int i = 0;
while(!found && i < bookObj.length) {
if(bookObj[i].getName().equals(input)) {
System.out.println("Book Found at position : " + i);
found = true;
} else {
i++;
}
}
if(!found) {
System.out.println("Book not Found!");
}
// Here i contains the indice of the element found in the array.
}
}
import java.util.Scanner;
公共类商店{
私有静态类书{
私有字符串名称;
图书(字符串名称){
this.name=名称;
}
公共字符串getName(){
返回名称;
}
}
公共静态void main(字符串[]args){
字符串输入;
Book[]bookObj=新书[3];
bookObj[0]=新书(“游戏结束”);
bookObj[1]=新书(“史莱克”);
bookObj[2]=新书(“幽灵”);
扫描仪控制台=新扫描仪(System.in);
input=console.nextLine();
布尔值=false;
int i=0;
而(!found&&i public boolean zLibaryContains( String title ) {
books[bookCount] = title;
int xBook = 0;
while( true )
if( books[xBook].getTitle().equals( title ) )
return xBook != bookCount;
else xBook++;
}