所以我很难弄清楚如何将用户输入的关于捐赠的书的类型并使用该信息来计算捐赠积分。购买场景也是如此。我有一小部分的提示,我的任务,以及我的代码,我已经到目前为止。我不是在寻求答案,而是一些指导。我觉得我有所有的信息来做这件事,但只是把它整理好。谢谢你的阅读。我必须使用循环,这是赋值的一部分。
提示:编写一个程序,允许一个顾客完成对旧书店的访问。该程序必须允许在键盘上输入客户名称。每位顾客将输入捐赠的图书数量和购买的图书数量。对于每一本被捐赠的书,用户将输入书的类型:精装书或平装书。用户需要输入关于所购买的每本书的相同信息。该程序必须为客户打印出一张收据,显示捐赠获得的总积分、购买图书的总成本以及客户在访问二手书店时欠下的金额。给客户的收据应按以下格式打印出来:
代码:
package bookStoreRecieptpackage;
import java.util.Scanner;
public class BookStoreRecieptClass {
public static void main(String[] args) {
// declare variables
Scanner input = new Scanner(System.in); // open input stream
String cleanUpStr; // clean up keyboard buffer
int numBooksDonated; // number of books donated by customer
int numBooksPurchased; // number of books purchased by customer
double totalCredits; // total credits earned by customer
double purchaseTotal; // total purchase of books by customer
double visitTotal; // total visit cost for customer
String bookType; // Type of book customer donating/purchasing
String customerName; // Name of customer
//int ctr; // counter
double costHardbacks; // cost of hardbacks in order
double costPaperbacks; // cost of paperbacks in order
// declare constants
final double HARDBACKDONATION = 0.75; // donation credit for one hardback book
final double PAPERBACKDONATION = 0.25; // donation credit for one paperback book
final double COSTHARDBACK = 1.0; // cost of one hardback book
final double COSTPAPERBACK = 0.50; // cost of one paperback book
final String HARDBACK = "H"; // Hardback book
final String PAPERBACK = "P"; // Paperback book
// initialize variables
cleanUpStr = "none yet";
numBooksDonated = 0;
numBooksPurchased = 0;
totalCredits = 0.0;
purchaseTotal = 0.0;
visitTotal = 0.0;
bookType = "none yet";
customerName = "none yet";
// print out your name
System.out.println("Input until 100 for Tolantio Ganzi");
//Prompt user to enter customer’s name.
System.out.println("Please enter customers name: ");
customerName = input.nextLine();
//Prompt user to enter total number of books being donated
do {
//prompt user to enter number of books being donated
System.out.println("Enter number of books being donated: ");
numBooksDonated = input.nextInt();
cleanUpStr = input.nextLine( );
if(numBooksDonated < 0) {
System.out.println(numBooksDonated + " is not a valid number. ");
}
else {
System.out.println("What type of book are you donating? Enter H or P.");
bookType = input.nextLine();
}
} while(numBooksDonated < 0);
//Prompt user to enter in the type of book [hard, paper]
//Validate total number of books being donated [loop, if, else]
//Prompt user to enter total number of books being purchased
//Prompt user to enter in the type of book [hard, paper]
//Validate total number of books being purchased [loop, if, else]
//Calculate
visitTotal = (purchaseTotal - totalCredits);
//Print output
System.out.println("Customers namett" + customerName);
System.out.println("Number books donatedt " + numBooksDonated);
System.out.printf("Donate Credittt$%.2f ", totalCredits);
System.out.println("Number Books purchasett " + numBooksPurchased);
System.out.printf("Purchase Totaltt$%.2f ", purchaseTotal);
System.out.printf("Total Owed for Visittt$%.2f ", visitTotal);
// close input stream
input.close( );
} //end main
} //end class
您已经在代码周围有一个do-while循环,提示用户输入他们的图书事务。也许在输入一次信息后,用户需要被询问他们是否有更多的捐赠或购买?看看我下面的例子,你可能会这样做。您可以使用do-while循环来完成此操作,但我发现while循环更易于阅读。我删除了所有与演示无关的注释和变量。
public class BookStoreRecieptClass {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter your name: ");
String customerName = input.nextLine();
int totalBooksDonated = 0;
boolean customerIsNotDone = true;
while (customerIsNotDone) {
System.out.println("Enter number of books being donated: ");
final String booksDonated = input.nextLine();
try {
final int booksDonatedInt = Integer.parseInt(booksDonated);
if (booksDonatedInt > 0) {
totalBooksDonated += booksDonatedInt;
} else {
throw new NumberFormatException();
}
} catch (NumberFormatException exception) {
System.out.println(booksDonated + " is not a valid number of books. ");
continue;
}
// Ask the customer if she wishes to continue
System.out.println("Type any key to continue, or type 'quit' to finish.");
final String next = input.nextLine();
if (next.equalsIgnoreCase("quit")) {
// exit the loop
customerIsNotDone = false;
}
}
System.out.println("Customers namett" + customerName);
System.out.println("Number books donatedt " + totalBooksDonated);
input.close();
}
}