保存java变量

时间:2017-03-03 21:20:57

标签: java loops variables

我正在处理作业,因为这个问题而无法执行任务。

在程序的最开始,我创建了一个变量

int initial = Keyboard.nextInt() (obviously user input)

程序然后进入一个循环,其中' initial'被改变了很多次。但是在程序结束时,当我们退出循环时,我需要使用新的'初始'值以及用户最初输入的确切值。

我很难让程序找到初始值,因为在我尝试调用' initial'之后退出循环时变量我只得到改变的数字而不是用户输入的第一个。请帮助我如何解决这个问题,谢谢。

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);
                }
    }

2 个答案:

答案 0 :(得分:0)

我认为你应该尝试使用ArrayList。例如:

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

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

答案 1 :(得分: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);

        }

    }

}