Results 1 to 16 of 16
  1. #1
    ad0m is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Unhappy Maths table.. what am I doing wrong (probably everything!! :p )

    is this type of switch right ?

    switch(type){
    case 'a': for(i = 1; i <= lines; i++){
    total = maths * i;
    System.out.println(maths + "*" + i + "=" + total);
    }
    Last edited by ad0m; 12-02-2007 at 01:29 AM. Reason: spelling

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    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?

  3. #3
    ad0m is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Default

    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

    Java 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);
            }
    
    }
    ?? man im just lost from here

  4. #4
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    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);
                 }
    }
    
    
    }
    
    }

  5. #5
    ad0m is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Default

    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.

  6. #6
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    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.
    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);
                 }
    }
    
    
    }
    
    }
    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.

    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.

    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);
                 }
    }
    
    
    }
    
    }
    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.

    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:

    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);
                 }
    }
    
    
    }
    
    }
    Again, I didn't test any of these but the concepts are accurate.

    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

    Quote Originally Posted by ad0m
    Hi ShoeNinja

    Thank you so much was very helpful

    I hope it is ok if I can geta few more bits of info off you.

    IE

    The Method 'input();' is in the "public static void manin(String[] args)"

    how come I don't put "calculation" there aswell.. ?



    I have the Keyboard.class file int he same directory hence why I dont have to import it.

    But I am getting No out put after I select the table i want eg Multiplication or Division "a for *" or "b for /" ect ??


    Thanks again so much !
    Kindest regards
    Adam

  7. #7
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    Oh yeah. Get rid of that System.out.println("Multiplication Table");. You don't need it.

  8. #8
    ad0m is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Default

    Dude your a tru champion!!!!

    I I get stuck again I will update this thred and keep u posted!!

    Fantastic stuff

    !!! :)

  9. #9
    Mark1989 is offline Member
    Join Date
    Nov 2007
    Location
    Dublin, Ireland
    Posts
    12
    Rep Power
    0

    Talking

    AWH Shoe,

    SAVIOR, I was trying to get the same program to work. True legend.

    Cheers! :)



    Mark

  10. #10
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    lol, are you guys in the same class or something?

    I hope you learn something from it.

  11. #11
    Mark1989 is offline Member
    Join Date
    Nov 2007
    Location
    Dublin, Ireland
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by ShoeNinja View Post
    lol, are you guys in the same class or something?

    I hope you learn something from it.
    Now what makes you think that :). Crazy talk.

    Yeh learned loads thanks so much.

    Extra kind regards,

    Mark

  12. #12
    eljarrah is offline Member
    Join Date
    Nov 2007
    Posts
    3
    Rep Power
    0

    Default 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.

  13. #13
    eljarrah is offline Member
    Join Date
    Nov 2007
    Posts
    3
    Rep Power
    0

    Default

    keyboard ??:confused: :confused:

    Scanner is the best . ;)

  14. #14
    Mark1989 is offline Member
    Join Date
    Nov 2007
    Location
    Dublin, Ireland
    Posts
    12
    Rep Power
    0

    Default

    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



    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);
                 }
    
    
    }
    
    
    }
    
    }
    I have tried the loop in this and several other positions..

    HELP PLEASE

    Thanks
    Mark

  15. #15
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    It looks like you have it except that you need use the .equals method of the char class instead of the boolean operator '=='.

    Java Code:
    (while again.equals('Y'))
    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.
    Last edited by ShoeNinja; 11-30-2007 at 06:51 PM.

  16. #16
    Mark1989 is offline Member
    Join Date
    Nov 2007
    Location
    Dublin, Ireland
    Posts
    12
    Rep Power
    0

    Default

    Na scanners not an option.

    Thanks that helped, I also had the loop in the wrong spot :).

    But really thanks for the help.

Similar Threads

  1. I am Doing Something Wrong But Don't Know What?
    By BHCluster in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 01:16 PM
  2. what is wrong with this code
    By masaka in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 08:27 AM
  3. Cannot understand whats wrong
    By Lehane_9 in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 07:57 PM
  4. What's wrong with this code?
    By Wizard wusa in forum New To Java
    Replies: 14
    Last Post: 01-22-2008, 11:55 PM
  5. Generates random maths questions
    By cachi in forum New To Java
    Replies: 4
    Last Post: 08-01-2007, 04:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •