Results 1 to 20 of 22
- 03-06-2009, 02:07 PM #1
[SOLVED] if-else and boolean issues
Hey im trying to get this code to accept time in 24 hours and charge calls according to peak time and non-peak time but the output for non-peak hours is the same as peak which shouldnt be, please help.
Java Code:import java.util.Scanner; public class PhoneCalls { public static void main(String[] args) { Scanner key = new Scanner(System.in); System.out.print("Enter time in 24-hour mode(NEGATIVE integer to quit): "); int timeStart = key.nextInt(); int numOfCall = 0; // initialise the number of calls made double totalCost = 0.0; int hours = (timeStart/100); // separates the time int minutes = (timeStart%100); int length = 0;// initialise the length of call double callCost = 0.0; //loop begins here while (timeStart >= 0) { System.out.print("Enter distance on Kilometres: "); int distance = key.nextInt(); System.out.print("Enter length of call in minutes: "); length = key.nextInt(); //limits if (timeStart >= 7 || timeStart <= 19) // peak hours { if (distance > 60) // Trunk (STD) calls { callCost = 0.50; } else // local calls { callCost = 0.20; } } else if (timeStart < 7 && timeStart > 19) // non-peak hours { if (distance > 60) // Trunk (STD) calls { callCost = 0.25; } else // local calls { callCost = 0.10; } } double costPerCall = total(length, callCost); // calculates the cost per call totalCost+=costPerCall; numOfCall++; System.out.println("The call will cost RM "+ costPerCall); System.out.println(); System.out.print("Enter time in 24-hour mode(NEGATIVE integer to quit): "); timeStart = key.nextInt(); } System.out.println("The total cost for all calls is RM" + totalCost); } public static double total (int value1, double value2) { double total = (value1*value2); return total; } }
- 03-06-2009, 11:42 PM #2
Member
- Join Date
- Dec 2008
- Location
- Edinburgh, Scotland
- Posts
- 4
- Rep Power
- 0
Think of a number
Hi,
Give a number that IS NOT >=7 OR <=19
Give a number that IS < 7 AND > 19
Hope this helps,
Eric
- 03-07-2009, 02:12 AM #3
- 03-07-2009, 04:01 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Actually those are very basis in Java fundamentals. About operators. Not much difficult to remember. Best thing you can do is write all those such things on a paper and keep going for few times.
- 03-07-2009, 04:15 AM #5
- 03-07-2009, 04:23 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Just have a try lol. As you want to workout, practice make perfect all the way. :)
- 03-07-2009, 04:25 AM #7
still not working
it still doesnt give correct output, here's what i want to get:
Java Code:Enter time in 24-hour mode (NEGATIVE integer to quit): 0800 Enter distance in kilometres: 50 Enter length of call in minutes: 10 That call will cost RM2.00 Enter time in 24-hour mode (NEGATIVE integer to quit): 0 Enter distance in kilometres: 50 Enter length of call in minutes: 10 That call will cost RM1.00 Enter time in 24-hour mode (NEGATIVE integer to quit): 2222 Enter distance in kilometres: 70 Enter length of call in minutes: 10 That call will cost RM2.50 Enter time in 24-hour mode (NEGATIVE integer to quit): 1030 Enter distance in kilometres: 80 Enter length of call in minutes: 10 That call will cost RM5.00 Enter time in 24-hour mode (NEGATIVE integer to quit): 2020 Enter distance in kilometres: 60 Enter length of call in minutes: 11 That call will cost RM1.10 Enter time in 24-hour mode (NEGATIVE integer to quit): 900 Enter distance in kilometres: 61 Enter length of call in minutes: 11 That call will cost RM5.50 Enter time in 24-hour mode (NEGATIVE integer to quit): -999 The total cost of these calls is RM17.10 Average cost is RM2.85 Enter time in 24hr mode (NEGATIVE integer to quit): -999
- 03-07-2009, 04:29 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
So you want to exit the code if user given a negative number, is it?
- 03-07-2009, 04:38 AM #9
yes thats the main idea but thats not the problem, try and run it and see...the value of non-peak time doesnt work.
- 03-07-2009, 04:49 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes, it's wrong. You have made a big mistake there.
Can you tell me the meaning of this line of code. Do you know the meaning of &&?Java Code:else if (timeStart < 7 && timeStart > 19)
- 03-07-2009, 05:14 AM #11
- 03-07-2009, 05:22 AM #12
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
if you have a condition where x < 7 and x > 19, then you will never get true. no number is less than 7 but greater than 19. you wanna use or.
- 03-07-2009, 05:26 AM #13
- 03-07-2009, 09:32 PM #14
Member
- Join Date
- Dec 2008
- Location
- Edinburgh, Scotland
- Posts
- 4
- Rep Power
- 0
And or Or
Hi,
Obviously my first post was too subtle :D
Have you figured it out, did you get it fixed?
Eric.
Edit - p.s. You don't actually need the second If, you can just use Else since if a number is not between 7 & 19 then it must be less than 7 or greater than 19.Last edited by Eric Golf; 03-07-2009 at 09:36 PM. Reason: Add p.s.
- 03-08-2009, 06:22 AM #15
- 03-08-2009, 06:38 AM #16
It works!!!, yaay!!!!
i think this time i got it right, the timeStart limit shouldn't have been 7, and 19. I changed them to 700 and 1959 cause the user enters 24-hours...anyways...test it out for me again...i think it works, Here's the code: PS, thanks all for the help
Java Code:import java.util.Scanner; public class PhoneCalls { public static void main(String[] args) { Scanner key = new Scanner(System.in); System.out.print("Enter time in 24-hour mode(NEGATIVE integer to quit): "); int timeStart = key.nextInt(); int numOfCall = 0; // initialise the number of calls made double totalCost = 0.0; int hours = (timeStart/100); // separates the time int minutes = (timeStart%100); int length = 0;// initialise the length of call double callCost = 0.0; //loop begins here while (timeStart >= 0) { System.out.print("Enter distance on Kilometres: "); int distance = key.nextInt(); System.out.print("Enter length of call in minutes: "); length = key.nextInt(); //limits if (timeStart >= 700 && timeStart <= 1959) // peak hours { if (distance > 60) // Trunk (STD) calls { callCost = 0.50; } else // local calls { callCost = 0.20; } } else if (timeStart < 700 || timeStart > 1959) // non-peak hours { if (distance > 60) // Trunk (STD) calls { callCost = 0.25; } else // local calls { callCost = 0.10; } } double costPerCall = total(length, callCost); // calculates the cost per call totalCost+=costPerCall; numOfCall++; System.out.println("The call will cost RM "+ costPerCall); System.out.println(); System.out.print("Enter time in 24-hour mode(NEGATIVE integer to quit): "); timeStart = key.nextInt(); } System.out.println("The total cost for all calls is RM" + totalCost); } public static double total (int value1, double value2) { double total = (value1*value2); return total; } }
- 03-08-2009, 08:46 AM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Suggestion here. Take a peace of paper and write down your logic you want to evaluate on your application. Then try to put that logic in to a Java code. It make sense a lot. Just think about a step at a time. Mean time you can study bit about logical operators in Java.
- 03-08-2009, 10:10 AM #18
- 03-08-2009, 05:11 PM #19
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 03-08-2009, 09:41 PM #20
Similar Threads
-
How can I pass a boolean to notifyObservers()
By dumb_ass in forum New To JavaReplies: 6Last Post: 03-05-2009, 08:15 PM -
Simple Boolean
By jigglywiggly in forum New To JavaReplies: 3Last Post: 01-01-2009, 05:01 AM -
boolean to string
By otoro_java in forum New To JavaReplies: 2Last Post: 01-30-2008, 05:31 AM -
boolean variables
By ravian in forum New To JavaReplies: 3Last Post: 12-31-2007, 04:58 AM -
Boolean Expression
By ritwik07 in forum New To JavaReplies: 3Last Post: 07-11-2007, 04:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks