- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个程序,允许用户输入 DVD Collection 或书籍 Collection 。
我对我所取得的成就感到相当满意(对编程来说仍然是一个新手)但我对我的搜索方法感到烦恼。
搜索确实有效并返回了用户查找的内容,但随后也打印出没有找到任何内容。我认为,原因是我编写代码的方式意味着在结束并返回菜单之前读取每个搜索参数。
我认为我可能需要使用 boolean 值或类似的值来在满足搜索条件时结束循环。
我相信任何有经验的程序员都会对我的代码摇头......
搜索方法:
/**
* Asks user to input Dvd title then compares
* with Dvd titles in collection
* @param none
* @return none
*/
public void searchDvd()
{
String temp = ""; // Temporary variable to hold dvd title
System.out.println ("\nPlease enter Dvd Title (full title) to search for: ");
temp= Genio.getString();
if(temp.equals(dvd1.getTitle()))
{
clrscr();
System.out.println("\nDvd is present in collection at location 1 (Dvd 1 in collection):\n\nTitle: " + dvd1.getTitle() + " \n Director: "
+ dvd1.getDirector() + " \n Lead Act: " + dvd1.getLead() + " \n Run Time: " + dvd1.getRunTime() + " \n Price: "
+ dvd1.getDvdPrice());
pressKey();
}
if(temp.equals(dvd2.getTitle()))
{
clrscr();
System.out.println("\nDvd is present in collection at location 2 (Dvd 2 in collection):\n\nTitle: " + dvd2.getTitle() + " \n Director: "
+ dvd2.getDirector() + " \n Lead Act: " + dvd2.getLead() + " \n Run Time: " + dvd2.getRunTime() + " \n Price: "
+ dvd2.getDvdPrice());
pressKey();
}
if(temp.equals(dvd3.getTitle()))
{
clrscr();
System.out.println("\nDvd is present in collection at location 3 (Dvd 3 in collection):\n\nTitle: " + dvd3.getTitle() + " \n Director: "
+ dvd3.getDirector() + " \n Lead Act: " + dvd3.getLead() + " \n Run Time: " + dvd3.getRunTime() + " \n Price: "
+ dvd3.getDvdPrice());
pressKey();
}
else
{
clrscr();
System.out.println("\nSorry, there were no Dvd's found with that title to display.\n ");
pressKey();
}
}
集合类(使用 main()):
public class Collection
{
//Declare private variables for use with class instances
private Dvd dvd1;
private Dvd dvd2;
private Dvd dvd3;
private Book book1;
private Book book2;
private Book book3;
public Collection()
{
//array = new int[2];
//dvd1 = dvd1;
//dvd2 = dvd2;
//dvd3 = dvd3;
dvd1 = new Dvd();
dvd2 = new Dvd();
dvd3 = new Dvd();
book1 = new Book();
book2 = new Book();
book3 = new Book();
}
public static void main(String args[])
{
//creates an instance of the collection class
Collection collection = new Collection();
collection.menu();
}
public void menu()
{
//declare the option field
int option;
char answer;
//start do while loop for the menu
do
{
//display the menu
clrscr();
System.out.println("");
System.out.println("\n\n~#~#~#~#~ DVD COLLECTION MENU ~#~#~#~#~\n\n");
System.out.println("\n<><><><> DVD's <><><><>\n");
System.out.println("1: Add (up to 3) Dvd's to Collection");
System.out.println("2: Display Dvd Collection");
System.out.println("3: Search Dvd Collection by collection");
System.out.println("\n<><><><> BOOK's <><><><>\n");
System.out.println("4: Add (up to 3) Books's to Collection");
System.out.println("5: Display Book Collection");
System.out.println("6: Search Book Collection by Title");
System.out.println("7: Quit program");
//prompt user to enter a selection
System.out.println("\nPlease select an option (1 - 7): ");
//use genio to get the user input
option=Genio.getInteger();
// Option 1 allows user to add up to 3 Dvd's to dvd collection
if (option == 1)
{
clrscr();
System.out.println("Enter Dvd 1 details:\n");
dvd1.setDvdInputs();
pressKey();
System.out.println("Enter Dvd 2 details:\n");
dvd2.setDvdInputs();
pressKey();
System.out.println("Enter Dvd 3 details:\n");
dvd3.setDvdInputs();
pressKey();
}
// Option 2 allows user to display Dvd collection
if (option == 2)
{
clrscr();
displayDvds();
}
// Option 3 allows the user to search the Dvd collection by title
if (option == 3)
{
clrscr();
searchDvd();
}
// Option 4 allows user add books to the book collection
if (option == 4)
{
clrscr();
System.out.println("Enter Book 1 details:\n");
book1.setBookInputs();
pressKey();
System.out.println("Enter Book 2 details:\n");
book2.setBookInputs();
pressKey();
System.out.println("Enter Book 3 details:\n");
book3.setBookInputs();
pressKey();
}
//i Option 5 allows the user to display the collection of books
if (option == 5)
{
clrscr();
displayBooks();
}
// Option 6 allows the user to search the Book collection by title
if (option == 6)
{
clrscr();
searchBook();
}
}
// Option 7 will print a message that tells that the program may be exited
while (option != 7);
clrscr();
System.out.println("You may now close the program. (click cross at top right)");
}
public void displayDvds()
{
float totalPrice = 0;
totalPrice = dvd1.getDvdPrice() + dvd2.getDvdPrice() + dvd3.getDvdPrice();
int totalRunTime = 0;
totalRunTime = dvd1.getRunTime() + dvd2.getRunTime() + dvd3.getRunTime();
if (dvd1.getTitle() == "" && dvd1.getDirector() == "" && dvd1.getLead() == "" && dvd1.getRunTime() == 0 && dvd1.getDvdPrice() == 0 && dvd2.getTitle() == "" &&
dvd2.getDirector() == "" && dvd2.getLead() == "" && dvd2.getRunTime() == 0 && dvd2.getDvdPrice() == 0 && dvd3.getTitle() == "" && dvd3.getDirector() == ""
&& dvd3.getLead() == "" && dvd3.getRunTime() == 0 && dvd3.getDvdPrice() == 0)
{
clrscr();
System.out.println("Sorry, there were no Dvd's in the collection to display.");
pressKey();
}
else
{
clrscr();
System.out.println(" \nDvd Collection:\n DVD1:\nTitle: " + dvd1.getTitle() + " \nDirector: "
+ dvd1.getDirector() + " \nLead Act: " + dvd1.getLead() + " \nRun Time: " + dvd1.getRunTime() + " \nPrice: £"
+ dvd1.getDvdPrice());
System.out.println(" \nDvd Collection:\n DVD2:\nTitle: " + dvd2.getTitle() + " \nDirector: "
+ dvd2.getDirector() + " \nLead Act: " + dvd2.getLead() + " \nRun Time: " + dvd2.getRunTime() + " \nPrice: £"
+ dvd2.getDvdPrice());
System.out.println(" \nDvd Collection:\n DVD3:\nTitle: " + dvd3.getTitle() + " \nDirector: "
+ dvd3.getDirector() + " \nLead Act: " + dvd3.getLead() + " \nRun Time: " + dvd3.getRunTime() + " \nPrice: £"
+ dvd3.getDvdPrice());
System.out.println(" \nTotal cost of combined Dvd's: £" + totalPrice);
System.out.println(" \nTotal Run Time of combined Dvd's: " + totalRunTime + " minutes.");
pressKey();
}
}
public void searchDvd()
{
String temp = ""; // Temporary variable to hold dvd title
System.out.println ("\nPlease enter Dvd Title (full title) to search for: ");
temp= Genio.getString();
if(temp.equals(dvd1.getTitle()))
{
clrscr();
System.out.println("\nDvd is present in collection at location 1 (Dvd 1 in collection):\n\nTitle: " + dvd1.getTitle() + " \n Director: "
+ dvd1.getDirector() + " \n Lead Act: " + dvd1.getLead() + " \n Run Time: " + dvd1.getRunTime() + " \n Price: "
+ dvd1.getDvdPrice());
pressKey();
}
if(temp.equals(dvd2.getTitle()))
{
clrscr();
System.out.println("\nDvd is present in collection at location 2 (Dvd 2 in collection):\n\nTitle: " + dvd2.getTitle() + " \n Director: "
+ dvd2.getDirector() + " \n Lead Act: " + dvd2.getLead() + " \n Run Time: " + dvd2.getRunTime() + " \n Price: "
+ dvd2.getDvdPrice());
pressKey();
}
if(temp.equals(dvd3.getTitle()))
{
clrscr();
System.out.println("\nDvd is present in collection at location 3 (Dvd 3 in collection):\n\nTitle: " + dvd3.getTitle() + " \n Director: "
+ dvd3.getDirector() + " \n Lead Act: " + dvd3.getLead() + " \n Run Time: " + dvd3.getRunTime() + " \n Price: "
+ dvd3.getDvdPrice());
pressKey();
}
else
{
clrscr();
System.out.println("\nSorry, there were no Dvd's found with that title to display.\n ");
pressKey();
}
}
public void displayBooks()
{
float totalbPrice = 0;
totalbPrice = book1.getBookPrice() + book2.getBookPrice() + book3.getBookPrice();
int totalPages;
totalPages = book1.getPages() + book2.getPages() + book3.getPages();
if (book1.getBookTitle() == "" && book1.getAuthor() == "" && book1.getGenre() == "" && book1.getPages() == 0 && book1.getBookPrice() == 0 && book2.getBookTitle() == "" ||
book2.getAuthor() == "" && book2.getGenre() == "" && book2.getPages() == 0 && book2.getBookPrice() == 0 && book3.getBookTitle() == "" && book3.getAuthor() == ""
&& book3.getGenre() == "" && book3.getPages() == 0 && book3.getBookPrice() == 0)
{
clrscr();
System.out.println("Sorry, there were no Book's in the collection to display.");
pressKey();
}
else
{
clrscr();
System.out.println(" \nBook Collection:\n BOOK 1: \nTitle: " + book1.getBookTitle() + " \nAuthor: "
+ book1.getAuthor() + " \nGenre: " + book1.getGenre() + " \nPages: " + book1.getPages() + " \nPrice: £"
+ book1.getBookPrice());
System.out.println(" \nBook Collection:\n BOOK 2: \nTitle: " + book2.getBookTitle() + " \nAuthor: "
+ book2.getAuthor() + " \nGenre: " + book2.getGenre() + " \nPages: " + book2.getPages() + " \nPrice: £"
+ book2.getBookPrice());
System.out.println(" \nBook Collection:\n BOOK 3: \nTitle: " + book3.getBookTitle() + " \nAuthor: "
+ book3.getAuthor() + " \nGenre: " + book3.getGenre() + " \nPages: " + book3.getPages() + " \nPrice: £"
+ book3.getBookPrice());
System.out.println(" \nTotal cost of combined Book's: £" + totalbPrice);
System.out.println(" \nTotal number of combined book pages: " + totalPages + " pages.");
pressKey();
}
}
public void searchBook()
{
String tempb; // Temporary variable to hold book title
System.out.println ("\nPlease enter Book Title (full title) to search for: ");
tempb= Genio.getString();
if(tempb.equals(book1.getBookTitle()))
{
clrscr();
System.out.println("\nBook is present in collection at location 1 (Book 1 in collection):\n\nTitle: " + book1.getBookTitle() + " \nAuthor: "
+ book1.getAuthor() + " \nLead Act: " + book1.getGenre() + " \nRun Time: " + book1.getPages() + " \nPrice: "
+ book1.getBookPrice());
pressKey();
}
if(tempb.equals(book2.getBookTitle()))
{
clrscr();
System.out.println("\nBook is present in collection at location 2 (Book 2 in collection):\n\nTitle: " + book2.getBookTitle() + " \nAuthor: "
+ book2.getAuthor() + " \nLead Act: " + book2.getGenre() + " \nRun Time: " + book2.getPages() + " \nPrice: £"
+ book2.getBookPrice());
pressKey();
}
if(tempb.equals(book3.getBookTitle()))
{
clrscr();
System.out.println("\nBook is present in collection at location 3 (Book 3 in collection):\n\nTitle: " + book3.getBookTitle() + " \nAuthor: "
+ book3.getAuthor() + " \nLead Act: " + book3.getGenre() + " \nRun Time: " + book3.getPages() + " \nPrice: £"
+ book3.getBookPrice());
pressKey();
}
else
{
clrscr();
System.out.println("\nSorry, there were no Book's found with that title to display.\n ");
pressKey();
}
}
public static void clrscr()
{
for ( int i=1;i<=50;i++)
System.out.println();
}
public static void pressKey()
{
String s;
System.out.print("\nPress return to continue : \n");
s = Genio.getString();
}
}
Dvd 类类与此相同):
public class Dvd
{
// instance Dvd variables
private String dvdTitle = ""; // Title of dvd set to empty
private String dvdDirector = ""; // Director of dvd set to empty
private String dvdLead = ""; // Lead actor/actress of dvd set to empty
private int dvdRunTime = 0; //Dvd run time in minutes
private float dvdPrice = 0; //Value of dvd
public Dvd( )
{
dvdTitle = "";
dvdDirector = "";
dvdLead = "";
dvdRunTime = 0;
dvdPrice = 0;
}
/
public void setDvdInputs()
{
System.out.println("Please enter the Dvd Title: ");
dvdTitle=Genio.getString();
System.out.println("Please enter the Dvd Director: ");
dvdDirector=Genio.getString();
System.out.println("Please enter the Dvd Lead Actor/Actress: ");
dvdLead=Genio.getString();
System.out.println("Please enter the Dvd Run Time: ");
dvdRunTime=Genio.getInteger();
System.out.println("Please enter the Dvd Cost: ");
dvdPrice=Genio.getFloat();
}
public String getTitle(){
return dvdTitle;
}
public String getDirector(){
return dvdDirector;
}
public String getLead(){
return dvdLead;
}
public int getRunTime()
{
return dvdRunTime;
}
public float getDvdPrice()
{
return dvdPrice;
}
}
书籍类:
public class Book
{
// instance Dvd variables
private String bookTitle = ""; // Title of dvd set to empty
private String bookAuthor = ""; // Director of dvd set to empty
private String bookGenre = ""; // Lead actor/actress of dvd set to empty
private int bookPages = 0; //Dvd run time in minutes
private float bookPrice = 0; //Value of dvd
public Book( )
{
bookTitle = "";
bookAuthor = "";
bookGenre = "";
bookPages = 0;
bookPrice = 0;
}
public void setBookInputs()
{
System.out.println("Please enter the Book Title: ");
bookTitle=Genio.getString();
System.out.println("Please enter the Book Author: ");
bookAuthor=Genio.getString();
System.out.println("Please enter the Book Genre: ");
bookGenre=Genio.getString();
System.out.println("Please enter the Book Page Number: ");
bookPages=Genio.getInteger(); //should be dvdRunTime = Genio.getDouble();
System.out.println("Please enter the Book Cost: ");
bookPrice=Genio.getFloat(); //should be dvdPrice = Genio.getFloat();
}
public String getBookTitle(){
return bookTitle;
}
public String getAuthor(){
return bookAuthor;
}
public String getGenre(){
return bookGenre;
}
public int getPages()
{
return bookPages;
}
public float getBookPrice()
{
return bookPrice;
}
}
Genio(用户输入)类:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Genio
{
public Genio()
{
}
private static String getStr()
{
String inputLine = "";
BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
try
{
inputLine = reader.readLine();
}
catch(Exception exc)
{
System.out.println ("There was an error during reading: "
+ exc.getMessage());
}
return inputLine;
}
public static int getInteger()
{
int temp=0;
boolean OK = false;
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
do
{
try
{
temp = Integer.parseInt(keyboard.readLine());
OK = true;
}
catch (Exception eRef)
{
if (eRef instanceof NumberFormatException)
{
System.out.print("Integer value needed: ");
}
else
{
System.out.println("Please report this error: "+eRef.toString());
}
}
} while(OK == false);
return(temp);
}
public static float getFloat()
{
float temp=0;
boolean OK = false;
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
do
{
try
{
temp = Float.parseFloat(keyboard.readLine());
OK = true;
}
catch (Exception eRef)
{
if (eRef instanceof NumberFormatException)
{
System.out.print("Number needed: ");
}
else
{
System.out.println("Please report this error: "+eRef.toString());
}
}
} while(OK == false);
return(temp);
}
public static double getDouble()
{
double temp=0;
boolean OK = false;
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
do
{
try
{
temp = Double.parseDouble(keyboard.readLine());
OK = true;
}
catch (Exception eRef)
{
if (eRef instanceof NumberFormatException)
{
System.out.print("Number needed: ");
}
else
{
System.out.println("Please report this error: "+eRef.toString());
}
}
} while(OK == false);
return(temp);
}
public static char getCharacter()
{
String tempStr="";
char temp=' ';
boolean OK = false;
do
{
try
{
tempStr = getStr();
temp = tempStr.charAt(0);
OK = true;
}
catch (Exception eRef)
{
if (eRef instanceof StringIndexOutOfBoundsException)
{
// means nothing was entered so prompt ...
System.out.print("Enter a character: ");
}
else
{
System.out.println("Please report this error: "+eRef.toString());
}
}
} while(OK == false);
return(temp);
}
public static String getString()
{
String temp="";
try
{
temp = getStr();
}
catch (Exception eRef)
{
System.out.println("Please report this error: "+eRef.toString());
}
return(temp);
}
}
最佳答案
您的问题是由单独的 if 语句
引起的。
if(something) {
}
if(somethingElse) // This is separate from the one above.
你可以使用 else if
来链接它们..
if(something) {
}
else if(somethingelse) {
}
else {
System.out.println("Sorry, none could be found");
}
或者如果您希望代码更高效并且您使用的是 JDK7+,则可以使用 switch
..
switch(input) {
case "Something":
// Do something
break;
default:
System.out.println("Sorry, none could be found");
break;
}
最后,如果您想保持完全相同的 if
结构,那么您可以使用 boolean
值。
if(something)
{
found = true;
}
if(!found)
{
System.out.println("Sorry, none could be found");
}
为了完整起见,我添加了最后一个,但我不推荐它。使用前两个选项之一。
关于java - 改进我的搜索方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26486738/
我对编码还比较陌生,但并非完全没有经验。处理有关金融计算器的学校作业。如果你们中的任何人可以查看我的代码以了解不良做法/可能的改进等,那就太好了。 我确实添加了一个“动画”启动(有很多 printf
小目标Trick 论文链接: https://paperswithcode.com/paper/slicing-aided-hyper-inference-and-fine-tuning 代码链接:h
if (firstPositionCpc && (firstPosition > 0 && firstPositionCpc 0 && topOfPageCpc 0 && firstPageCpc
我有 2 个表:“packages”和“items”。 “packages”有以下列:pack_id | item_id “items”有以下列......:item_id |输入 一个包可以有多个
我目前有一个 Pandas Dataframe,我在其中执行列之间的比较。我发现一种情况,在进行比较时存在空列,由于某种原因比较返回 else 值。我添加了一个额外的语句来将其清理为空。看看我是否可以
我正在处理一个查询,通过首先舍入它们的主要日期时间键来连接一个数据库中的多个表。数据库包含来自 openhab 的性能数据,每个表只有一个名为 Time 的主日期时间行和一个名为 Value 的值行。
问候 我有一个程序创建一个类的多个实例,在所有实例上运行相同的长时间运行的 Update 方法并等待完成。我从 this question 开始关注 Kev 的方法将更新添加到 ThreadPool.
我想在下学期的类(class)中取得领先,所以我制作了这个基本版本的 Blackjack 来开始理解 C 的基础知识,我希望您有任何想法可以帮助我更好地理解 C 和其正常的编码实践。 C 中的很多东西
我有一个要求,比如: 给定一个数组,其中包含随机数。需要输出元素出现的次数,有自带解决方案: var myArr = [3,2,1,2,3,1,4,5,4,6,7,7,9,1,123,0,123];
这是我的数据库项目。 表user_ select id, name from user_; id | name ----+---------- 1 | bartek 2 | bartek
我已经完成了一个小批量脚本来调整(动态)一些图像的大小: for a in *.{png,PNG,jpg,JPG,jpeg,JPEG,bmp,BMP} ; do convert "$a" -resiz
是否有更 pythonic 的方法来执行以下代码?我想在一行中完成 parsed_rows 是一个可以返回大小为 3 或 None 的元组的函数。 parsed_rows = [ parse_row(
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improv
下面的代码完成了我想要的,但还有其他更像 python 风格的方式吗? 文件格式: key1:value1,key2:value2,... key21:value21,key22:value22,..
如果两个英文单词只包含相同的字母,则它们是相似的。例如,food 和 good 不相似,但 dog 和 good 相似。 (如果A与B相似,则A中的所有字母都包含在B中,B中的所有字母都包含在A中。)
我有以下结构来表示二叉树: typedef struct node *pnode; typedef struct node { int val; pnode left; pnode
我有一个区域,它由受约束的 delaunay 三角剖分表示。我正在解决在两点之间寻找路径的问题。我正在使用 Marcelo Kallmann 提供的论文作为解决此问题的引用点。然而,而不是使用 Kal
如果我需要检查文本(字符串)中是否存在单词 A 或单词 B,如果我这样做会有性能差异: if(text.contains(wordA) || text.contains(wordB)) 要使用一些正则
Adjust To 我有上面这个简单的页面,上面有一个标签和一个文本框。我想在文本框中输入文本。 对我有帮助的 XPATH 是 //*[contains(tex
以下伪代码的elisp代码 if "the emacs version is less than 23.1.x" do something else something-else 写成 (if
我是一名优秀的程序员,十分优秀!