Calculating User Input in an Array (Java)?
-
Hello, i am trying to gather user input and store inside an array, which then adds the points of the array together. With this total i then want to subtract the total from another int (budget). Any Suggestions? I keep getting the result budget - the array length, rather than array total. E.g. Budget (100) - how many bills (2) = 98 this is my code...thanks import javax.swing.JOptionPane; import java.util.Scanner; public class input { public static void main (String[] args) { Scanner input = new Scanner(System.in); System.out.println("total budget?"); int budget = input.nextInt(); //allow user input; System.out.println("How many bills?"); int num = input.nextInt(); int array[] = new int[num]; System.out.println("Enter the amount " + num + " one by."); for (int i = 0 ; i < array.length; i++ ) { array[i] = input.nextInt(); } { int sum = budget-num; for (int x: array) { num+=x; System.out.println("You Have spent a toal of " + num + " pounds"); } System.out.println("You Have spent a toal of " + sum + " pounds"); { } } } }
-
Answer:
First off, this line is wrong: int sum = budget-num; That just means budget-number of bills. You're reusing "num" when you shouldn't be. Use a new variable name.
Paul at Yahoo! Answers Visit the source
Other answers
First off, this line is wrong: int sum = budget-num; That just means budget-number of bills. You're reusing "num" when you shouldn't be. Use a new variable name.
godfathe...
Related Q & A:
- How can I make Private Messages in Drupal 7 with user user pictures like in Facebook?Best solution by Drupal Answers
- How to post an array of complex objects (that has an array of complex objects) with jQuery?Best solution by Stack Overflow
- how can i remove an array from an array?Best solution by Stack Overflow
- Python: How do you get an input from the user into a list?Best solution by stackoverflow.com
- How to check on all the permissions granted to a user and apply the same permissions to another user?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.