2017-03-03 139 views
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); 
       } 
    } 
+3

只是用不同的變量爲新輸入和舊的輸入 – UnknowableIneffable

+1

您好,歡迎計算器!你應該看看在指引http://stackoverflow.com/help/how-to-ask你應該提供的代碼示例等 對於你的問題,你會做的就是INT價格= nextInt()像你說的創建另一個變量originalPrice =價格。併爲新價格和originalPrice請求舊價格。 –

+0

感謝您的幫助 – peanut

回答

0

有多個錯誤。

  • 你只處理int值,所以刪除doubles
  • initialBalance應該是不可變的,因而它被設置final
  • spending變量應該被初始化爲0
package com.stackoverflow.q42588622; 

import java.util.Scanner; 

@SuppressWarnings("javadoc") 
public class Answer { 

    public static void main(String[] args) { 

     // get in the habit of cleaning up resources 
     try (Scanner keyboard = new Scanner(System.in)) { 

      // initial amount 
      System.out.println("Enter the initial amount: "); 

      final int initialBalance = keyboard.nextInt(); 

      int numItems = 0; 

      // Same concept as balance 
      // Only dealing with ints 
      // double assets = 0.0; 

      int balance = initialBalance; 

      // Spending is initially 0. 
      // Only dealing with ints 
      // double spending = originalPrice - assets; 

      int spending = 0; 

      if (initialBalance <= 10) { 
       System.out.println("Please save money and come back later!!"); 
       numItems = 1; 
       spending = 0; 
      } else { 

       while (balance > 10) { 

        System.out.println("Do you want to make purchases (Y/N)? "); 

        char c = keyboard.next() 
         .charAt(0); 

        if (c == 'Y' || 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 (balance - price >= 10) { 
         System.out.println("A purchase is accepted"); 
         balance -= price; 
         spending += price; 
         // Removing as initialBalance is immutable 
         // 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 + "    " + balance + " " + "  " + spending); 

     } 

    } 

} 
0

我想你應該嘗試使用ArrayList。例如:

//Inside loop 
ArrayList<Integer> stuff = new ArrayList<>(); 
price = Keyboard.nextInt(); 
stuff.add(price); 

//Outside loop; gets first input 
stuff.get(0);