如何根据Java中的响应打印出内容?

如何根据Java中的响应打印出内容?,java,Java,我是Java新手,正在编写一个测试程序。代码如下: import java.util.Scanner; public class Book { private String title; private String author; private String pages; public static void main(String[] args) { // Book #1 // =1= Book ano

我是Java新手,正在编写一个测试程序。代码如下:

import java.util.Scanner;

public class Book {
    private String title;
    private String author;
    private String pages;

    public static void main(String[] args) {

        // Book #1
        // =1=

        Book anothermedium = new Book();
        anothermedium.title = "Another Medium";
        anothermedium.author = "Jen Smith";
        anothermedium.pages = "387";

        // Book #2
        // =2=

        Book whateveryouwant = new Book();
        whateveryouwant.title = "Whatever You Want";
        whateveryouwant.author = "Bob Gray";
        whateveryouwant.pages = "424";

        System.out.println("Enter the name of a book to see its details  ");
        System.out.println("or input Catalog to see the catalog.");
        Scanner scan = new Scanner(System.in);
        String input = scan.nextLine();
    }
}
我试图做到这一点,当您输入类似于
Catalog
的内容时,响应将类似于:

当前目录由以下书籍组成:

另一种媒介

随便你

我很抱歉,如果这已经被张贴。我搜索了一下,找不到任何关于我的问题的答案。

使用if语句

if(input.toLowerCase().equals("catalog"){
   //do what you need to do here
   System.out.println("The current catalog consists of the books: ");
   System.out.println(anothermedium.title);
   System.out.println(whateveryouwant.title);
}

要使条件区分大小写,应将输入字符串转换为小写。如果您不理解语法,则基本上会计算If语句中的条件。如果If的计算结果为true,则括号之间的任何内容都将被执行。如果它的计算结果为false,那么它将跳过两个括号中的内容,继续处理代码的其余部分。希望这是你需要的。祝你好运

我建议您将每本
书籍
添加到一个
列表
,该列表可以循环查找正确的元素,或者可以用于打印整个目录。类似于下面的实现

这样,如果用户想要一本特定的书,
else if()
可以匹配书名并打印正确的
,否则如果用户想要查看电子图书目录,他们将只显示
书名

List<Book> listBook = new ArrayList<>();
listBook.add( whateveryouwant  );
listBook.add( anothermedium  );

System.out.println( "Enter the name of a book to see its details  " );
System.out.println( "or input Catalog to see the catalog." );
Scanner scan = new Scanner( System.in );
String input = scan.nextLine();

for( Book book : listBook )
{
    if( "catalog".equalsIgnoreCase( input ) )
    {
        System.out.println( "Title: " + book.getTitle() );
    }
    else if( book.getTitle().equalsIgnoreCase( input ) )
    {
        System.out.println( book.toString() );
    }
}
List listBook=new ArrayList();
添加(您想要的任何内容);
添加(无热介质);
System.out.println(“输入图书名称以查看其详细信息”);
System.out.println(“或输入目录以查看目录”);
扫描仪扫描=新扫描仪(System.in);
字符串输入=scan.nextLine();
for(书籍:listBook)
{
if(“目录”.equalsIgnoreCase(输入))
{
System.out.println(“标题:+book.getTitle());
}
else if(book.getTitle().equalsIgnoreCase(输入))
{
System.out.println(book.toString());
}
}

您可以创建这些图书对象的arrayList。在该列表中添加每本书的对象。运行此代码应该可以工作

 public class Book {
private String title;
private String author;
private String pages;
public void Book(){
  }
 public String getTitle(){
     return title;
 }
public static void main(String[] args) {

    // Book #1
    // =1=
    ArrayList<Book> catalog = new ArrayList<Book>();

    Book anothermedium = new Book();
    anothermedium.title = "Another Medium";
    anothermedium.author = "Jen Smith";
    anothermedium.pages = "387";
    catalog.add(anothermedium);
    // Book #2
    // =2=

    Book whateveryouwant = new Book();
    whateveryouwant.title = "Whatever You Want";
    whateveryouwant.author = "Bob Gray";
    whateveryouwant.pages = "424";
    catalog.add(whateveryouwant);

    System.out.println("Enter the name of a book to see its details");
    System.out.println("or input Catalog to see the catalog.");

    Scanner scan = new Scanner(System.in);
    String input = scan.nextLine();
    if(input.equals("catalog"))
    {
        for(int i=0;i<catalog.size();i++)
        {
            System.out.println(catalog.get(i).getTitle());
        }
    }
}}
公共课堂教材{
私有字符串标题;
私有字符串作者;
私有字符串页;
公开作废书籍(){
}
公共字符串getTitle(){
返回标题;
}
公共静态void main(字符串[]args){
//第1册
// =1=
ArrayList catalog=新的ArrayList();
Book Anothermium=新书();
anothermedium.title=“另一种介质”;
anothermedium.author=“Jen Smith”;
anothermedium.pages=“387”;
目录.添加(无热介质);
//第二册
// =2=
想买什么就买什么=新书();
你想要什么就做什么。title=“你想要什么就做什么”;
无论你想要什么。作者=“鲍勃·格雷”;
你想要什么都行。pages=“424”;
目录。添加(任何您想要的);
System.out.println(“输入图书名称以查看其详细信息”);
System.out.println(“或输入目录以查看目录”);
扫描仪扫描=新扫描仪(System.in);
字符串输入=scan.nextLine();
if(input.equals(“目录”))
{

对于(inti=0;i,这里有几个不同的问题需要解决

  • 如何根据 用户的输入文本

  • 如何迭代一个文件的内容并显示其名称

  • <> P>一个要记住的是java是面向对象的。你几乎总是想考虑你要解决的问题,并且创建代表你问题空间内各种类型事物的类。在这种情况下,你已经描述了一个<代码>书实体和一个<代码>目录>代码>实体。这些通常是单独的类。来自主程序类(包含
    main
    方法)

    您可以通过使用语句将输入与应用程序知道的常量进行比较来解决第一个问题。您可以通过围绕问题结构定义有意义的类,并使用希望看到的输出定义
    toString
    方法来解决下一个问题

    import java.util.Scanner;
    import java.util.List;
    import java.util.ArrayList;
    
    public class CatalogViewer {
    
        private static final String VIEW_CATALOG_KEY = "catalog";
    
        public static class Book {
            private String title;
            private String author;
            private String pages;
    
            public Book(String title, String author, String pages) {
                this.title = title;
                this.author = author;
                this.pages = pages;
            }
    
            @Override
            public String toString() {
                return title;
            }
        }
    
        public static class Catalog {
            private List<Book> books = new ArrayList<Book>();
    
            public List<Book> getBooks() {
                return books;
            }
    
            @Override
            public String toString() {
                StringBuilder message = new StringBuilder();
    
                message.append("The current catalog consists of the books:\n");
    
                for (Book book : books) {
                    message.append("\n" + book + "\n");
                }
    
                return message.toString();
            }
        }
    
        public static void main(String[] args) {
            Catalog catalog = new Catalog();
    
            catalog.getBooks().add(new Book("Another Medium", "Jen Smith", "387"));
            catalog.getBooks().add(new Book("Whatever You Want", "Bob Gray", "424"));
    
            System.out.println("Enter the name of a book to see its details  ");
            System.out.println("or input Catalog to see the catalog.");
    
            Scanner scan = new Scanner(System.in);
            String input = scan.nextLine();
    
            if (input.trim().equalsIgnoreCase(VIEW_CATALOG_KEY)) {
                System.out.println(catalog);
            }
        }
    }
    
    import java.util.Scanner;
    导入java.util.List;
    导入java.util.ArrayList;
    公共类目录查看器{
    私有静态最终字符串视图\u CATALOG\u KEY=“CATALOG”;
    公共静态类书{
    私有字符串标题;
    私有字符串作者;
    私有字符串页;
    公共书籍(字符串标题、字符串作者、字符串页面){
    this.title=标题;
    this.author=作者;
    this.pages=页面;
    }
    @凌驾
    公共字符串toString(){
    返回标题;
    }
    }
    公共静态类目录{
    private List books=new ArrayList();
    公共列表getBooks(){
    还书;
    }
    @凌驾
    公共字符串toString(){
    StringBuilder消息=新建StringBuilder();
    message.append(“当前目录由以下书籍组成:\n”);
    用于(书籍:书籍){
    message.append(“\n”+book+”\n”);
    }
    返回消息.toString();
    }
    }
    公共静态void main(字符串[]args){
    Catalog Catalog=新目录();
    catalog.getBooks().add(新书(“另一种媒体”、“Jen Smith”、“387”);
    catalog.getBooks().add(新书(“随心所欲”、“鲍勃·格雷”、“424”);
    System.out.println(“输入图书名称以查看其详细信息”);
    System.out.println(“或输入目录以查看目录”);
    扫描仪扫描=新扫描仪(System.in);
    字符串输入=scan.nextLine();
    if(input.trim().equalsIgnoreCase(视图\目录\键)){
    系统输出打印LN(目录);
    }
    }
    }
    
    my bad忘记了一对圆括号。现在就检查一下,这样就行了。下面是我写的代码:当我在另一个介质中键入时,它工作得很好,但它只是循环使用你想要的任何东西。你忘记了一个右括号}用于检查输入是否为“另一个介质”的if语句。因此,添加一个“}”a