Results 1 to 16 of 16
- 11-29-2007, 02:12 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 4
- Rep Power
- 0
- 11-29-2007, 03:21 PM #2
At a quick glance it looks like you have it pretty much figured out. I would add a switch statement in your for loop to account for the different operations.
What specific problems are you having?
- 11-29-2007, 05:43 PM #3
Member
- Join Date
- Nov 2007
- Posts
- 4
- Rep Power
- 0
IM not getting it to compile - not even that what I have posted above.
As to Switch Statments do they some what go like this
?? man im just lost from hereJava Code:static void calculation() { int total = 1; int number; int lines; int i; switch() { case a: System.out.println("Multiplication Table"); for(i = 1; i < lines; i++) total = number * i; System.out.println(number + "*" + i + "=" + total); } }
- 11-29-2007, 06:08 PM #4
Maybe this will help a little more. Make sure to import the Keyboard class at the top. I wasn't able to test it since I don't have to Keyboard class.
I put the for loops inside the switch cases because I thought it would be a little more efficient since it won't be evaluating the switch statement every time. I could be wrong though.
Java Code://Need to import Keyboard class here. //Did it come from your book or something? import.... class Mathtab{ public static void main(String[]args) { input(); } //////////////////////////////////////// // Gathering Data from user /////////////////////////////////////// static void input() { int type, lines; char table; //Enter the table eg 5 times table 9 Addition table etc System.out.print("What maths Table would you like? : "); type = Keyboard.readInt(); System.out.print("\nHow many lines of the table would you like? :"); lines = Keyboard.readInt(); System.out.print("\nSelect a Table Type (a)Multiplication (b)Division (c)Addition (d)Subtraction: "); Keyboard.skipLine(); table = Keyboard.readChar(); table = Character.toUpperCase(table); calculation(type, lines, table); } //////////////////////////////////////// // Calculation of Data Gathered /////////////////////////////////////// static void calculation(int maths, int lines, char type) { int total; System.out.println("Multiplication Table"); int i; switch(type){ case 'a': for(i = 1; i <= lines; i++){ total = maths * i; System.out.println(maths + "*" + i + "=" + total); } break; case 'b': for(i = 1; i <= lines; i++){ total = maths / i; System.out.println(maths + "/" + i + "=" + total); } break; case 'c': for(i = 1; i <= lines; i++){ total = maths + i; System.out.println(maths + "+" + i + "=" + total); } break; case 'd': for(i = 1; i <= lines; i++){ total = maths - i; System.out.println(maths + "-" + i + "=" + total); } } } }
- 11-29-2007, 07:09 PM #5
Member
- Join Date
- Nov 2007
- Posts
- 4
- Rep Power
- 0
I have a
Keyboard.class in the same directory
but still not getting an noutput for that method
I run the java and I can Input 1st line Input 2nd line and input 'a' 'b' 'c' or 'd'
but i just get a return of
Multiplication Table
press any key to continue......Last edited by ad0m; 12-02-2007 at 01:31 AM.
- 11-29-2007, 07:12 PM #6
I didn't put the calculate method in the main class because it needs the input from the user to work. Take a look at the code.
I moved the calculation method back out to main. So imagine that the input() method just ran and you are about to run the calculation() method. The main method doesn't know anything about the what the user entered because nothing was returned from the input method. There is no way to let the calculation know how many lines, what kind of operation to perform and what not.Java Code://Need to import Keyboard class here. //Did it come from your book or something? import.... class Mathtab{ public static void main(String[]args) { input(); calculation(); } //////////////////////////////////////// // Gathering Data from user /////////////////////////////////////// static void input() { int type, lines; char table; //Enter the table eg 5 times table 9 Addition table etc System.out.print("What maths Table would you like? : "); type = Keyboard.readInt(); System.out.print("\nHow many lines of the table would you like? :"); lines = Keyboard.readInt(); System.out.print("\nSelect a Table Type (a)Multiplication (b)Division (c)Addition (d)Subtraction: "); Keyboard.skipLine(); table = Keyboard.readChar(); table = Character.toUpperCase(table); } //////////////////////////////////////// // Calculation of Data Gathered /////////////////////////////////////// static void calculation(int maths, int lines, char type) { int total; System.out.println("Multiplication Table"); int i; switch(type){ case 'a': for(i = 1; i <= lines; i++){ total = maths * i; System.out.println(maths + "*" + i + "=" + total); } break; case 'b': for(i = 1; i <= lines; i++){ total = maths / i; System.out.println(maths + "/" + i + "=" + total); } break; case 'c': for(i = 1; i <= lines; i++){ total = maths + i; System.out.println(maths + "+" + i + "=" + total); } break; case 'd': for(i = 1; i <= lines; i++){ total = maths - i; System.out.println(maths + "-" + i + "=" + total); } } } }
To fix this, you have 2 options. First would be to return the user input from the input() method back to main. This is possible but complicated due to the fact that a) you have multiple variables to return and b) the variables are of different types. Java only allows one return value. To return multiple values, you would need to return an Array. But arrays can only handle data of on type. To handle the two types that you have (ints and a char) you would need to use a List, Vector.....something along those lines. Now these collections store objects as exactly that....generic Objects. So when you pull them out, you have to type cast them which can cause exceptions and blah blah blah....see what I mean about complicated?
Your second option would be to call the calculation method from inside the input method like I wrote. Look at the code again.
Look at the end of the input() method just before calculation is called(). You just obtained all the input from the user and it is right there in that method. So why not just call the calculation() method and pass it that data directly? This saves a bunch of work.Java Code://Need to import Keyboard class here. //Did it come from your book or something? import.... class Mathtab{ public static void main(String[]args) { input(); } //////////////////////////////////////// // Gathering Data from user /////////////////////////////////////// static void input() { int type, lines; char table; //Enter the table eg 5 times table 9 Addition table etc System.out.print("What maths Table would you like? : "); type = Keyboard.readInt(); System.out.print("\nHow many lines of the table would you like? :"); lines = Keyboard.readInt(); System.out.print("\nSelect a Table Type (a)Multiplication (b)Division (c)Addition (d)Subtraction: "); Keyboard.skipLine(); table = Keyboard.readChar(); table = Character.toUpperCase(table); calculation(type, lines, table); } //////////////////////////////////////// // Calculation of Data Gathered /////////////////////////////////////// static void calculation(int maths, int lines, char type) { int total; System.out.println("Multiplication Table"); int i; switch(type){ case 'a': for(i = 1; i <= lines; i++){ total = maths * i; System.out.println(maths + "*" + i + "=" + total); } break; case 'b': for(i = 1; i <= lines; i++){ total = maths / i; System.out.println(maths + "/" + i + "=" + total); } break; case 'c': for(i = 1; i <= lines; i++){ total = maths + i; System.out.println(maths + "+" + i + "=" + total); } break; case 'd': for(i = 1; i <= lines; i++){ total = maths - i; System.out.println(maths + "-" + i + "=" + total); } } } }
Ok, now that I think about it there is a third option that is also easier than the first. You could set up local fields in the Mathtab class that correspond to your input. The input class would then set these and then the calculation method could use them. That would look something like this:
Again, I didn't test any of these but the concepts are accurate.Java Code://Need to import Keyboard class here. //Did it come from your book or something? import.... class Mathtab{ int type; int lines; char table; //Need a constructor public Mathtab(){ } public static void main(String[]args) { Mathtabl foo = new Mathtab(); foo.input(); foo.calculation(); } //////////////////////////////////////// // Gathering Data from user /////////////////////////////////////// static void input() { Don't need these anymore. //int type, lines; //char table; //Enter the table eg 5 times table 9 Addition table etc System.out.print("What this.type Table would you like? : "); this.type = Keyboard.readInt(); System.out.print("\nHow many lines of the table would you like? :"); this.lines = Keyboard.readInt(); //this sets the filed declared above for this particular object. System.out.print("\nSelect a Table Type (a)Multiplication (b)Division (c)Addition (d)Subtraction: "); Keyboard.skipLine(); this.table = Keyboard.readChar(); this.table = Character.toUpperCase(table); } //////////////////////////////////////// // Calculation of Data Gathered /////////////////////////////////////// static void calculation(int this.type, int lines, char type) { int total; System.out.println("Multiplication Table"); int i; switch(this.table){ case 'a': for(i = 1; i <= this.lines; i++){ total = this.type * i; System.out.println(this.type + "*" + i + "=" + total); } break; case 'b': for(i = 1; i <= this.lines; i++){ total = this.type / i; System.out.println(this.type + "/" + i + "=" + total); } break; case 'c': for(i = 1; i <= this.lines; i++){ total = this.type + i; System.out.println(this.type + "+" + i + "=" + total); } break; case 'd': for(i = 1; i <= this.lines; i++){ total = this.type - i; System.out.println(this.type + "-" + i + "=" + total); } } } }
As far as not getting output, are you sure you moved the calculation method? I think it might be because you are changing the char to upper case. I didn't notice that before. Try changing to case('x') statements to case('X').
Good luck dude.
-Shoe
Originally Posted by ad0m
- 11-29-2007, 07:12 PM #7
Oh yeah. Get rid of that System.out.println("Multiplication Table");. You don't need it.
- 11-29-2007, 07:34 PM #8
Member
- Join Date
- Nov 2007
- Posts
- 4
- Rep Power
- 0
Dude your a tru champion!!!!
I I get stuck again I will update this thred and keep u posted!!
Fantastic stuff
!!! :)
- 11-29-2007, 07:45 PM #9
Member
- Join Date
- Nov 2007
- Location
- Dublin, Ireland
- Posts
- 12
- Rep Power
- 0
AWH Shoe,
SAVIOR, I was trying to get the same program to work. True legend.
Cheers! :)
Mark
- 11-29-2007, 07:47 PM #10
lol, are you guys in the same class or something?
I hope you learn something from it.
- 11-29-2007, 08:30 PM #11
Member
- Join Date
- Nov 2007
- Location
- Dublin, Ireland
- Posts
- 12
- Rep Power
- 0
- 11-30-2007, 01:14 AM #12
Member
- Join Date
- Nov 2007
- Posts
- 3
- Rep Power
- 0
nice
Take this code from me
i change "keyboard" method to "Scanner method"
i use "switch" and "for" loops
i didn't use a method but a direct code
my regards
Java Code:import java.util.Scanner; public class Mathtab { public static void main(String[]args) { ///////////// // gather the data from user ///////////////////////////////////////////////// Scanner input = new Scanner(System.in); System.out.print("What maths Table would you like? : "); int table = input.nextInt(); System.out.print("\nHow many lines of the table would you like? :"); int line = input.nextInt(); System.out.print("\nSelect a Table Type (1)Multiplication (2)Division (3)Addition (4)Subtraction: "); int type = input.nextInt(); System.out.print("\n-------------------\n"); /////////////////////////////////////////////////// ///////////////// // calculate the data //////////////////////// switch (type) { case 1: for (int i=1;i<=line;i++) System.out.println(table+"*"+i+"="+(table*i)); break; case 2: for (int i=1;i<=line;i++) System.out.println(table+"/"+i+"="+(table/i)); break; case 3: for (int i=1;i<=line;i++) System.out.println(table+"+"+i+"="+(table+i)); break; case 4: for (int i=1;i<=line;i++) System.out.println(table+"-"+i+"="+(table-i)); break; } System.out.print("\n-------------------"); } }Last edited by eljarrah; 11-30-2007 at 01:16 AM.
- 11-30-2007, 01:20 AM #13
Member
- Join Date
- Nov 2007
- Posts
- 3
- Rep Power
- 0
keyboard ??:confused: :confused:
Scanner is the best . ;)
- 11-30-2007, 11:45 AM #14
Member
- Join Date
- Nov 2007
- Location
- Dublin, Ireland
- Posts
- 12
- Rep Power
- 0
Hey,
Haven't used scanner before. Only have a Keyboard.class file.
What i want to know though is how would you get the program to run in a do/while loop, so that at the end the user would be prompted with
Process another (y/n):
so y would repeat and n be the termination of the Program
I have tried the loop in this and several other positions..Java Code:class maths{ public static void main(String[]args) { input(); } /* Data Gathering */ static void input() do{ { int type, lines; char table; char again; System.out.print("Which maths Table would you like(1-12)?: "); type = Keyboard.readInt(); System.out.print("\nHow many lines of the table would you like? :"); lines = Keyboard.readInt(); System.out.print("\nSelect a Table Type (a)Multiplication (b)Division (c)Addition (d)Subtraction: "); Keyboard.skipLine(); table = Keyboard.readChar(); table = Character.toUpperCase(table); calculation(type, lines, table); System.out.print("\nProcess Another ('y'/'n'): "); again = Keyboard.readChar(); again = Character.toUpperCase(again); } }while(again == 'Y'); /* Calculation of Data Gathered */ static void calculation(int maths, int lines, char type) { int total; int i; switch(type){ case 'A': for(i = 1; i <= lines; i++){ total = maths * i; System.out.println(maths + "*" + i + "=" + total); } break; case 'B': for(i = 1; i <= lines; i++){ total = maths / i; System.out.println(maths + "/" + i + "=" + total); } break; case 'C': for(i = 1; i <= lines; i++){ total = maths + i; System.out.println(maths + "+" + i + "=" + total); } break; case 'D': for(i = 1; i <= lines; i++){ total = maths - i; System.out.println(maths + "-" + i + "=" + total); } } } }
HELP PLEASE
Thanks
Mark
- 11-30-2007, 01:41 PM #15
It looks like you have it except that you need use the .equals method of the char class instead of the boolean operator '=='.
Aren't you using the Keyboard class because your book is making you? I agree that Scanner would be better but I was under the impression that it was not an option.Java Code:(while again.equals('Y'))Last edited by ShoeNinja; 11-30-2007 at 06:51 PM.
- 11-30-2007, 05:19 PM #16
Member
- Join Date
- Nov 2007
- Location
- Dublin, Ireland
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
I am Doing Something Wrong But Don't Know What?
By BHCluster in forum New To JavaReplies: 3Last Post: 04-16-2008, 01:16 PM -
what is wrong with this code
By masaka in forum New To JavaReplies: 5Last Post: 04-16-2008, 08:27 AM -
Cannot understand whats wrong
By Lehane_9 in forum New To JavaReplies: 1Last Post: 03-06-2008, 07:57 PM -
What's wrong with this code?
By Wizard wusa in forum New To JavaReplies: 14Last Post: 01-22-2008, 11:55 PM -
Generates random maths questions
By cachi in forum New To JavaReplies: 4Last Post: 08-01-2007, 04:48 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks