2017-03-03 5 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

を使用StackOverflowのために歓迎!あなたは、ガイドラインを参照する必要がありますhttp://stackoverflow.com/help/how-to-askコード例などを提供する必要があります あなたの質問には、あなたが言っているように、int price = nextInt()を得るだけです。別の変数originalPrice = price。新しい価格のためのコール価格とoldのための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);