0
我的工作分配,但無法這樣做,因爲這個問題的功能。保存Java變量
在節目的一開始,我創建再變
int initial = Keyboard.nextInt() (obviously user input)
程序進入其中「初始」的值被改變很多次的循環。但是在節目的最後,當我們退出循環,我需要使用新的「初始」值,並且還可以爲用戶最初輸入的確切值。
我有困難獲取程序,找出初始值,因爲退出循環任何時候,我嘗試在「初始」變量調用後,我只得到了改變號碼不是第一個用戶輸入。幫助我如何解決這個問題,將不勝感激,謝謝。
public class Question3 {
public static void main(String[] args) {
//declare scanner
Scanner keyboard = new Scanner (System.in);
//initial amount
System.out.println("Enter the initial amount: ");
int initial = keyboard.nextInt();
int numItems = 0;
double assets = 0;
int originalPrice=initial;
double spending = originalPrice-assets;
if(initial<=10)
{
System.out.println("Please save money and come back later!!");
numItems=1;
spending=0.0;
}
else
while (initial > 10)
{
System.out.println("Do you want to make purchases (Y/N)? ");
char c = keyboard.next().charAt(0);
if (c == 'Y')
{
System.out.println("Please enter the price of the item = ");
}
else
{
System.out.println("Lack of desire of Mr.Toto");
break;
}
int price = keyboard.nextInt();
if (initial-price>=10)
{
System.out.println("A purchase is accepted");
initial-=price;
assets=initial-price;
numItems++;
}
else
{
System.out.println("Insufficient assets!!");
System.out.println("Please enter the price of the item = ");
}
if(numItems==10)
{
System.out.println("Maximal number of purchases reached");
break;
}
}
//displaying the summary of mr totos purchases
System.out.println("-------------------------------------------");
System.out.println("Here is a summary of Mr.Toto's purchases.");
System.out.println("-------------------------------------------");
System.out.println("Number of items Assets Spending");
System.out.println(" "+numItems+" "+assets+" " +" "+spending);
}
}
只是用不同的變量爲新輸入和舊的輸入 – UnknowableIneffable
您好,歡迎計算器!你應該看看在指引http://stackoverflow.com/help/how-to-ask你應該提供的代碼示例等 對於你的問題,你會做的就是INT價格= nextInt()像你說的創建另一個變量originalPrice =價格。併爲新價格和originalPrice請求舊價格。 –
感謝您的幫助 – peanut