![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
I'm sorta new to java and i haven't learned any of the higher commands yet. I have to complete an assignment in which i want the program to do the following:
Quote:
This is the code i'm using and its not working properly: import java.awt.*;
import hsa.Console;
public class cbook
{
static Console c = new Console ();
static public void main (String[] args)
{
double open, dorc, count, count1;
char entry;
c.clear ();
c.print ("Opening Balance: ");
open = c.readDouble ();
do
{
c.print ("Debit or Credit: ");
count = c.readInt ();
if (count >= open - count)
c.println ("Error!Please eneter a valid amount!");
}
while (count >= open - count);
c.print ("Balance: ");
c.println (open + count);
c.println ();
do
{
c.print ("Another Entry? ");
entry = c.readChar ();
if (entry == 'Y' || entry == 'y')
c.println ();
c.print ("Current Balance: ");
c.println (count + open, 2, 2);
c.print ("Debit or Credit: ");
count1 = c.readInt ();
if (count1 >= open - count1)
c.println ("Error!Please eneter a valid amount!");
c.print ("Balance: ");
c.println (open + count + count1);
c.println ();
}
while (entry == 'Y' || entry == 'y');
}
}Last edited by Dark Flare Knight; Mar 22nd, 2005 at 8:09 PM. |
|
|
|
|
|
|
#2 |
|
Professional Programmer
|
What is the code doing? I don't really have time to run it myself, but if you could post a run of the program it'll make it easier for us to help you debug it.
Dizz PS, this is a great post, has a lot of code, and a detailed description of the problem, props to you DFK! |
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
Attemptes it and had some problems with my compiler :/ so i gave up sorry can you post your errors i might be able to suggest routes to go down that way.
|
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 318
Rep Power: 4
![]() |
You've actually got a few problems here; I haven't tried to run the code or anything, so this is just off the top of my head and I don't know if this is everything. Below is your code, with comments added by me to say what I think is up.
import java.awt.*;
import hsa.Console;
public class cbook
{
static Console c = new Console ();
static public void main (String[] args)
{
double open, dorc, count, count1;
char entry;
c.clear ();
c.print ("Opening Balance: ");
open = c.readDouble ();
do
{
c.print ("Debit or Credit: ");
count = c.readInt (); // should be c.readDouble() again (1)
if (count >= open - count)
c.println ("Error!Please eneter a valid amount!");
}
while (count >= open - count);
c.print ("Balance: ");
c.println (open + count);
c.println ();
do
{
c.print ("Another Entry? ");
entry = c.readChar ();
if (entry == 'Y' || entry == 'y')
c.println (); // only this statement is conditional (2)
c.print ("Current Balance: ");
c.println (count + open, 2, 2);
c.print ("Debit or Credit: ");
count1 = c.readInt (); // again, this should be c.readDouble() (3)
if (count1 >= open - count1)
c.println ("Error!Please eneter a valid amount!"); // typo!
c.print ("Balance: ");
c.println (open + count + count1); // count1 may not be valid (4)
c.println ();
}
while (entry == 'Y' || entry == 'y');
}
}If your values are doubles, you can't read them with c.readInt(); anyway, you wouldn't want to because they aren't going to be integers according to your specification in your post (1 and 3). In point 2, I think you wanted more statements to be executed conditionally depending on the test in your if statement; you need to use braces { } around the block to make this happen. Finally, at point 4, you output balance taking into account the value of count1 even when your program has complained than an invalid value was given; probably not a good idea! You could set the value of count1 to zero where it's invalid to fix this. Also, because you read the count/count1 values each time, your program is forgetting earlier 'transactions' rather than adding them up; e.g. if I enter -5.00 then -5.00 again, the balance the second time will not have gone down by 10.00, but just by 5.00. You could correct this by reading into a value other than count/count1 and then adding the value (it's as easy as adding because adding a negative is subtracting) so that count/count1 contain running totals. Be careful only to add the newly read value in when it's valid though. Hope this helps! Like I say, I don't know if it's everything. |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
Thanks for your help. I think i got it now.
Last edited by Dark Flare Knight; Mar 23rd, 2005 at 4:00 PM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|