Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javaoop如何获取变量的用户输入_Java_Oop - Fatal编程技术网

javaoop如何获取变量的用户输入

javaoop如何获取变量的用户输入,java,oop,Java,Oop,首先,我很抱歉,如果很难理解我,因为我不是以英语为母语的人,我的观点可能很难理解 我正在尝试建立一个非常基本的图书馆预订系统,但我在获取用户输入以调整书籍细节方面遇到了问题 我试图做的是从用户输入中获取书籍的详细信息,但我不知道如何正确地做到这一点。我尝试了我用来改变身份的相同风格,但似乎不起作用 我在检查和更改图书状态(从可用状态更改为已预订状态)时也遇到一些困难 这是主程序: public static void main(String[] args) { System.out.p

首先,我很抱歉,如果很难理解我,因为我不是以英语为母语的人,我的观点可能很难理解

我正在尝试建立一个非常基本的图书馆预订系统,但我在获取用户输入以调整书籍细节方面遇到了问题

我试图做的是从用户输入中获取书籍的详细信息,但我不知道如何正确地做到这一点。我尝试了我用来改变身份的相同风格,但似乎不起作用

我在检查和更改图书状态(从可用状态更改为已预订状态)时也遇到一些困难

这是主程序:

public static void main(String[] args) {

    System.out.println("Welcome to the basic library program! ");
    System.out.println("Insert author, name, genre, ISNB, page number ");

    // this one is wrong.
    Book booking = new Book("Mary","Random Book","Horror",69,69);

    System.out.println("Print book information? 1 = yes 2 = no?");
    int answer = input.nextInt();
    if (answer == 1) {
        booking.printBook();
    }
    else {

    }

    System.out.println("Check books status? 1= yes 2= no");
    int status = input.nextInt();
    if (status == 1) {
        booking.checkStatus(); 
    }
    else {

    }

    System.out.println("Change books status: 1 = Avaible 2 = Booked 3 = Lost ");
    int availability = input.nextInt();
    booking.changeAvailability(availability);
}
这是图书课:


您需要在主类中使用扫描仪。例如,在您的代码中:

Scanner input = new Scanner (System.in);
需要在尝试获取用户输入之前输入。这将允许您调用input.nextInt;成功


请参见前面的问题:

,因为第一个答案说您需要使用扫描仪。您所要做的就是将scanner变量直接传递到book创建的对象中。你的逻辑在一些事情上有点偏离,今晚我手头的时间太多了,所以我只是用我的方式来解决你的问题

import java.util.Scanner;

public class bookTest {

public static void main(String[] args) {


    Scanner input = new Scanner(System.in);


    System.out.println("Welcome to the basic library program! ");
    System.out.println("Insert author, name, genre, ISNB, page number ");
    String author = input.next();
    String name = input.next();
    String genre = input.next();
    int isbn = input.nextInt();
    int pageNumber = input.nextInt();


    Book booking = new Book(author,name ,genre,isbn,pageNumber);


    System.out.println("Change books status: 1 = Avaible 2 = Booked 3 = Lost ");
    int availability = input.nextInt();
    booking.changeAvailability(availability);


    System.out.println("Print book information? 1 = yes 2 = no?");
    int answer = input.nextInt();
    if (answer == 1) {
        booking.printBook();
    }


    System.out.println("Check book status? 1= yes 2 = no");
    int status = input.nextInt();
    if (status == 1) {
       booking.checkStatus(); 
    }




}



}
接下来是图书类redid

public class Book {

private String author;
private String name;
private String genre;
private int isbn;
private int pageNumber;
private int availability;


       public Book(String authorInput, String nameInput, String genreInput, int       isbnInput, int pageInput) {
       this.author = authorInput;
       this.name = nameInput;
       this.genre = genreInput;
       this.isbn = isbnInput;
       this.pageNumber = pageInput;

   }

public void changeAvailability(int setAvailability) {  
       if (setAvailability == 1) {
           availability = 1;
       }
       else if (setAvailability == 2) {
           availability = 2;
           }
       else if (setAvailability == 3) {
           availability = 3;
       }
       else {
              System.out.println("Unknown input: Program closing..");
              System.exit(1);
          }

   }

    //Instead of set use get
   public void checkStatus() {
       if (availability == 1) {
           System.out.println("Avaible.");
       }
       else if (availability == 2) {
           System.out.println("Not avaible.");         
           }
       else if (availability == 3) {
           System.out.println("Book missing.");
       }
       else {
              System.out.println("Unknown input: Program closing..");
              System.exit(1);
          }    
   }

   public void printBook() {

       System.out.println("Author: " + this.author + "\nName: " + this.name + "\nGenre: " + this.genre + "\nISBN: " + this.isbn + "\nPages: " + this.pageNumber);

       }
}

当您的ChangeAvailability方法中甚至没有条件availability==3时,您如何将其更改为booked。您的问题到底是什么还不清楚。您可能应该尝试编写代码来询问书本的详细信息,然后在书本上设置它们,然后告诉我们,对于您获得的某些输入,生成错误或输出错误是什么。问题是我希望从用户处询问详细信息。目前,详细信息随BookBook booking一起提供=new BookMary、Random Book、Horror、69,69;我没有读输入行,但我不知道如何正确地做。我的代码中有这个,但出于某种原因它没有复制粘贴它。
public class Book {

private String author;
private String name;
private String genre;
private int isbn;
private int pageNumber;
private int availability;


       public Book(String authorInput, String nameInput, String genreInput, int       isbnInput, int pageInput) {
       this.author = authorInput;
       this.name = nameInput;
       this.genre = genreInput;
       this.isbn = isbnInput;
       this.pageNumber = pageInput;

   }

public void changeAvailability(int setAvailability) {  
       if (setAvailability == 1) {
           availability = 1;
       }
       else if (setAvailability == 2) {
           availability = 2;
           }
       else if (setAvailability == 3) {
           availability = 3;
       }
       else {
              System.out.println("Unknown input: Program closing..");
              System.exit(1);
          }

   }

    //Instead of set use get
   public void checkStatus() {
       if (availability == 1) {
           System.out.println("Avaible.");
       }
       else if (availability == 2) {
           System.out.println("Not avaible.");         
           }
       else if (availability == 3) {
           System.out.println("Book missing.");
       }
       else {
              System.out.println("Unknown input: Program closing..");
              System.exit(1);
          }    
   }

   public void printBook() {

       System.out.println("Author: " + this.author + "\nName: " + this.name + "\nGenre: " + this.genre + "\nISBN: " + this.isbn + "\nPages: " + this.pageNumber);

       }
}