Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   Error (https://www.askmehelpdesk.com/showthread.php?t=737114)

  • Mar 3, 2013, 05:31 AM
    junaidahmd
    error
    what's the error in the prog...




    import java.util.Scanner;
    public class g {


    public static void main(String[] args) {
    Scanner s=new Scanner(System.in);
    int d,x,y;

    System.out.println("Enter the first number");
    x=s.nextInt();
    System.out.println("Enter the second number");
    y=s.nextInt();
    public void add(){
    d= x+y;
    System.out.println(d);
    }
    public void sub(){
    d= x-y;
    System.out.println(d);
    }

    int a;
    System.out.println("Enter ur choice");
    a=s.nextInt();
    switch(a)
    { case 1:add();
    break;
    case 2:sub();
    break;
    default :System.out.println("invalid choice");
    break;
    }
    }
    }
  • Jul 4, 2013, 10:51 PM
    rameshspry
    you have to write the add and sub methods out side of the main method

    Like this...

    import java.util.Scanner;
    public class g {


    public static void main(String[] args) {
    Scanner s=new Scanner(System.in);
    int d,x,y;

    System.out.println("Enter the first number");
    x=s.nextInt();
    System.out.println("Enter the second number");
    y=s.nextInt();


    int a;
    System.out.println("Enter ur choice");
    a=s.nextInt();
    switch(a)
    {
    case 1:add(x,y);
    break;
    case 2:sub(x,y);
    break;
    default :System.out.println("invalid choice");
    }
    }
    public static void add(int x, int y){
    int d= x+y;
    System.out.println(d);
    }
    public static void sub(int x, int y){
    int d= x-y;
    System.out.println(d);
    }

    }

  • All times are GMT -7. The time now is 09:13 PM.