由於這是一個家庭作業問題,我不會爲您提供問題的答案,但希望我能夠幫助您瞭解如何構建您的程序,並解釋如何使用Scanner類收集來自用戶的輸入。其餘的將取決於你。
首先,您需要爲您的主程序開發僞代碼。基本上是基於應該發生的事件的執行流程。
僞代碼不是編譯的代碼,但在計算程序結構時非常有用。這是您的程序的僞代碼。
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循環,基本思路是「不斷詢問用戶的數量,直到我們有一個有效的數量」。最簡單的方法是創建一個布爾變量,我們稱之爲hasQuantity,然後循環,直到該值爲真。
我們的僞代碼現在變爲:
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.
}
}
希望這會有所幫助。隨時提出更多問題。
你的問題又是什麼? – NullUserException 2012-03-29 15:25:08