Results 21 to 27 of 27
- 08-04-2012, 06:17 PM #21
Member
- Join Date
- Aug 2012
- Posts
- 13
- Rep Power
- 0
- 08-04-2012, 06:26 PM #22
Member
- Join Date
- Aug 2012
- Posts
- 13
- Rep Power
- 0
Re: Need help with + - * / calculator
In case anyone wants the code:
Java Code:import java.util.Scanner; public class calculator { public static void main(String args[]) { Scanner input = new Scanner(System.in); double fnum, snum, answer; String name; System.out.println("Do you want to add?"); name = input.nextLine(); if (name.equalsIgnoreCase("Yes")) { System.out.println("Please enter first number:"); fnum = input.nextDouble(); System.out.println("Please enter second number:"); snum = input.nextDouble(); answer = fnum + snum; System.out.println(answer); } else { System.out.println("okay"); } System.out.println("Do you want to subtract?"); name = input.nextLine(); if (name.equalsIgnoreCase("Yes")) { System.out.println("Please enter first number:"); fnum = input.nextDouble(); System.out.println("Please enter second number:"); snum = input.nextDouble(); answer = fnum - snum; System.out.println(answer); } else { System.out.println("okay"); } System.out.println("Do you want to multiply?"); name = input.nextLine(); if (name.equalsIgnoreCase("Yes")) { System.out.println("Please enter first number:"); fnum = input.nextDouble(); System.out.println("Please enter second number:"); snum = input.nextDouble(); answer = fnum * snum; System.out.println(answer); } else { System.out.println("okay"); } System.out.println("Do you want to divide?"); name = input.nextLine(); if (name.equalsIgnoreCase("Yes")) { System.out.println("Please enter first number:"); fnum = input.nextDouble(); System.out.println("Please enter second number:"); snum = input.nextDouble(); answer = fnum / snum; System.out.println(answer); } else { System.out.println("okay"); } } }
- 08-04-2012, 06:32 PM #23
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Need help with + - * / calculator
What numbers are you inputting for the addition part, and what numbers are you inputting for the subtraction part?
"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 08-04-2012, 06:38 PM #24
Member
- Join Date
- Aug 2012
- Posts
- 13
- Rep Power
- 0
Re: Need help with + - * / calculator
just any old numbers, it just uses the answer to answer the next question.
I'm using integers.
- 08-04-2012, 06:50 PM #25
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Need help with + - * / calculator
The problem seems to be arising from the nextLine() statement. I remember a scenario like this from before. It doesn't even ask if the user wants to subtract. For the purposes of making your program work, you can replace nextLine() with next(). As far as why nextLine() is causing a problem, maybe Furarable or somebody else can provide some insight.
Last edited by awinston; 08-04-2012 at 06:54 PM.
"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 08-04-2012, 07:02 PM #26
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Need help with + - * / calculator
Here is an article about why this issue occurs: Stop Input Skipping in Java | Ryan Rampersad
Apparently it happens whenever nextInt() is followed by nextLine(). The article above suggests a solution of having a meaningless nextLine() in order to bypass the skipping, but I can't believe that this is the best way to solve the problem."Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 08-04-2012, 07:13 PM #27
Member
- Join Date
- Aug 2012
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
calculator help
By sirstroud in forum New To JavaReplies: 0Last Post: 04-01-2012, 05:58 AM -
Help with AWT CALCULATOR
By Megan Dosnueve in forum AWT / SwingReplies: 2Last Post: 04-04-2011, 05:49 PM -
Help in a calculator
By Ayannie in forum New To JavaReplies: 6Last Post: 01-04-2011, 08:21 PM -
Calculator
By water in forum AWT / SwingReplies: 4Last Post: 09-23-2009, 06:00 AM -
help with calculator
By kalibballer in forum New To JavaReplies: 8Last Post: 04-01-2009, 12:57 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks