gpt4 book ai didi

java - 使用 Java 中的 Book 类和 TestBook 类创建程序

转载 作者:行者123 更新时间:2023-11-30 03:40:59 25 4
gpt4 key购买 nike

我正在尝试创建允许用户输入这本书的java程序数字和程序将显示所选书籍的价格。我有创建了书籍类,但大多数没有语法错误可能存在逻辑错误并且 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;
}
}

TestBook 类

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;
}
}

最佳答案

您在方法调用中忘记了 () :

替换

System.out.println("The price is: "+ b1.getPrice);

System.out.println("The price is: "+ b1.getPrice());

除了该错误之外,您正在调用一个不存在的构造函数:

Book b3 = new Book (8));

您的 Book 构造函数接受 3 个参数。并且您在该调用中有一个额外的 )

关于java - 使用 Java 中的 Book 类和 TestBook 类创建程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26826220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com