Results 11 to 17 of 17
Thread: Help with Java work.
-
10-16-09, 09:28 AM #11
Re: Help with Java work.
Originally Posted by Imisnew2
Kinda makes me miss school. If only real world programming problems were this much fun.
-
-
10-16-09, 10:08 AM #13
Re: Help with Java work.
Originally Posted by Ewok
I may check out the lonestar code just for shits though.
-
-
10-16-09, 11:18 PM #15
Re: Help with Java work.
I'm having trouble getting the min and max value. They come out as the same value.
Code:import java.util.Scanner; public class Calc { public static void main (String[] args) { Scanner scan = new Scanner(System.in); final int SENTINEL = -999; final Boolean DEBUG = false; final char OPERATOR1 = '+', OPERATOR2 = '-', OPERATOR3 = '*', OPERATOR4 = '/'; int numLines = 0; int min = 0; int max = 0; int num1 = 0; int num2 = 0; int result = 0; char math; if(scan.hasNextInt()) { num1 = scan.nextInt(); } else { System.out.println("Error: Input is not an integer."); System.exit(0); } System.out.println("=================="); System.out.println("Simple calculator"); System.out.println("=================="); while(num1 != SENTINEL) { math = scan.next().charAt(0); if(scan.hasNextInt()) { num2 = scan.nextInt(); } else { System.out.println("Error: Input is not an integer."); System.exit(0); } if(math == OPERATOR1) { result = num1 + num2; } if(math == OPERATOR2) { result = num1 - num2; } if(math == OPERATOR3) { result = num1 * num2; } if(math == OPERATOR4) { if(num2 == 0) { System.out.println("Error: Cannot devide by 0"); System.exit(0); } else { result = num1 / num2; } } if(math != OPERATOR1 && math != OPERATOR2 && math != OPERATOR3 && math != OPERATOR4) { System.out.println("Error: Operator is invalid."); System.exit(0); } System.out.println("Operation: " + num1 + " " + math + " " + num2); System.out.println("Result " + ": " + result); System.out.println(); if(result >= max) { result = max; } if(result <= min) { result = min; } numLines++; if(DEBUG) { System.out.println("Debug Information:"); System.out.println(" Number of lines read = " + numLines); System.out.println(" Current max: " + max); System.out.println(" Current min: " + min); } if(scan.hasNextInt()) { num1 = scan.nextInt(); } else { System.out.println("Error: Input is not an integer."); System.exit(0); } } System.out.println("Number of lines read: " + numLines); System.out.println("Maximum calculated value: " + max); System.out.println("Minimum calculated value: " + min); } }
-
- Join Date
- 11-26-06
- Location
- Anywhere you want to be.
- Posts
- 3,946
- Post Thanks / Like
- Blog Entries
- 1
10-17-09, 12:13 AM #16Re: Help with Java work.
In this assignment you must initialize max and min to the result of
the first valid calculation; a request for division by zero
is not valid. To do this you will need to use an "if" statement
in order to initialize max and min to the calculated value
when they have not yet been initialized and when division by zero
is not being requested.
example)
if (Input your boolean statement here)
{
iMin = result;
iMax = result;
}
so first things, for your max and min take off your '= 0'. They will be initialized to the FIRST VALID RESULT
-
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks