用 Java 创建一个带有 Book 类和 TestBook 类的程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26826220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a program with Book class and TestBook class in Java
提问by Nadhira Rizky
I'm trying to create java program that allows user to enter the book number and the program will show the price of the book chosen. I've created the book class and there's no syntax error in but most probably there's logic error and the class TestBook has errors in invoking the input. can you help me? im lost :( here's my code:
我正在尝试创建允许用户输入书号的 java 程序,该程序将显示所选书籍的价格。我已经创建了 book 类并且没有语法错误,但很可能存在逻辑错误,并且 TestBook 类在调用输入时有错误。你能帮助我吗?我迷路了:(这是我的代码:
class Book{
String title;
String author;
double price;
int option;
//constructor
Book(String title, String author, double newPrice){
title = title;
author = author;
price = newPrice;
}
public String getTitle(){
return title;
}
public String getAuthor(){
return author;
}
public double getPrice(){
return price;
}
public int getOption(){
return option;
}
public void setPrice(int option, double price, double newPrice){
if (option == 1){
price = 20.00;
newPrice = price;
}
else if (option == 2){
price = 15.00;
newPrice = price;
}
else if (option == 3){
price = 23.90;
newPrice = price;
}
else if (option == 4){
price = 27.30;
newPrice = price;
}
else if (option == 5){
price = 50.00;
newPrice = price;
}
else if (option == 6){
price = 13.50;
newPrice = price;
}
}
public void setOption(int newOption){
option = newOption;
}
}
The TestBook class
试卷类
import java.util.Scanner;
public class TestBook{
public static void main (String[]args){
Scanner input = new Scanner (System.in);
//The Book List
System.out.println("The Book List");
System.out.println("1)How to do programming 12th Edition by Liang.\n2)Malaysian Food Recipe by Nadia Bt Mahmud.\n3)What is Islam by Ustad Manzoor Malik.\n4)Urban Legend by Christine R.M.\n5)Fundamental of Calculus by Prof. Abu.\n6)How to raise your kids by Salsabila\n");
System.out.println("Choose your book number to know the price: ");
int option = input.nextInt();
Book b1 = new Book(theOption(option);// invoke the option to get the price
System.out.println("The price is: "+ b1.getPrice);
}
//Method option
public static int theOption(int option){
return option;
}
}
回答by Eran
You forgot ()
in your method calls :
你忘记()
了你的方法调用:
Replace
代替
System.out.println("The price is: "+ b1.getPrice);
with
和
System.out.println("The price is: "+ b1.getPrice());
Beside that error, you are calling a constructor that doesn't exist :
除了那个错误,你正在调用一个不存在的构造函数:
Book b3 = new Book (8));
Your Book constructor accepts 3 parameters. And you have an extra )
in that call.
您的 Book 构造函数接受 3 个参数。你)
在那个电话中有一个额外的。
回答by Marshall Tigerus
There are a lot of problems with your code.
你的代码有很多问题。
You ask for input on a book, but don't use a switch or if statement to use that input. You will always print all three outputs, regardless of what the user inputs.
You have a constructor for the Book object with two parameters, but when you call the constructor with Book b1 = new Book(1) you only use one. This will give an error unless you have a constructor with only one parameter.
In your SetPrice function, you are shadowing your object's variables, which menas the local copy of Price will get the new value, not the object's copy of Price.
您要求输入一本书,但不要使用 switch 或 if 语句来使用该输入。无论用户输入什么,您将始终打印所有三个输出。
您有一个带有两个参数的 Book 对象的构造函数,但是当您使用 Book b1 = new Book(1) 调用构造函数时,您只使用了一个。除非您有一个只有一个参数的构造函数,否则这将产生错误。
在您的 SetPrice 函数中,您正在隐藏对象的变量,这意味着 Price 的本地副本将获得新值,而不是 Price 的对象副本。
There are more but this will get you started.
还有更多,但这会让你开始。