由于这是一个家庭作业问题,因此我不会为您提供问题的答案,但希望我能够帮助您弄清楚如何构建您的程序,并解释如何使用用于收集用户输入的扫描程序类。剩下的就看你自己了。
首先,您需要为您的主程序开发伪代码。基本上是基于应该发生的事件的执行流程。
伪代码不是可以编译的代码,但在弄清楚程序的结构时很有用。这是您的程序的伪代码。
show greeting with choices.
get choice from user
if choice is valid and choice is not exit
prompt user for quantity
if quantity is valid
calculate total and show it to the user
restart program
if quantity is invalid
prompt user for a valid quantity again
if choice is valid and choice is exit
show exit message and exit program
if choice is invalid
show error message and restart program
请注意,在成功获取购买总成本后,我们会“重新启动程序”。如果你更高级,这可能需要调用一个函数,但我猜你还是个初学者,所以不止一次做同样的事情应该会让你想起一个循环。在这种情况下是一个while循环。
因此我们可以把这个伪代码改写成下面的样子
done = false
while not done
get choice from user
if choice is valid and choice is not exit
prompt user for quantity
if quantity is valid
calculate total and show it to the user
if quantity is invalid
prompt user for a valid quantity again
if choice is valid and choice is exit
done = true
if choice is not valid
show error message
exit program
现在,请注意当用户输入无效数量(即:不是整数 > 1 的内容)时,我们如何再次请求数量。多次做同样的事情?没错,这意味着我们可能应该再次使用另一个 while 循环。
对于第二个 while 循环,基本思路是“不断向用户询问数量,直到我们获得有效数量”。实现这一点的最简单方法是创建一个我们称为 haveQuantity 的布尔变量,然后循环直到该值为真。
我们的伪代码现在变成:
done = false
while not done
get choice from user
if choice is valid and choice is not exit
haveQuantity = false
while not haveQuantity
prompt user for quantity
get quantity from user
if quantity is valid
haveQuantity = true
calculate total and show it to the user
if choice is valid and choice is exit
done = true
if choice is not valid
show error message
exit program
这应该是您程序的一般结构。在下一节中,我将向您展示如何正确使用扫描仪类来获取用户的输入。
public class EchoInt
{
import java.util.Scanner;
public static void main(String[] args)
{
//Declaration of variables outside the while loop
Scanner scan = new Scanner(System.in); //declaring variables outside of a loop saves space and speeds up execution as the jvm does not need to reallocate space for an object inside the loop.
boolean done = false; //this will be our conditional for the while loop
int input = -1;
while(!done) //while done is equal to false.
{
System.out.println("Please enter a positive int to echo or 0 to exit: ");
if(scan.hasNextInt()) //If the user has inputted a valid int
input = scan.nextInt(); //set the value of input to that int.
else //The scanner does not have a integer token to consume
{
/*
THIS IS IMPORTANT. If the scanner actually does have a token
which was not an int. For example if the user entered a string,
you need to consume the token to prepare to accept further tokens.
*/
if(scan.hasNext())
scan.next(); //Actually consumes the token
input = -1; //This is used to indicate that an invalid input was submitted
}
if(input == 0) //The user chose to exit the program
done = true; //set done to true to kick out of the while loop
else if(input == -1) //This means the user inputed an invalid input
System.out.println("ERROR! Try again."); //show error message
else //The user inputted valid input
System.out.println("echo: "+input); //Echo the int
}
scan.close(); //We are done, so close the scanner
System.out.println("Exiting. Goodbye!"); //Show a goodbye message
System.exit(0); //exit the program. The zero tells us we exited without errors.
}
}
希望这会有所帮助。并随时提出更多问题。