Log in

View Full Version : Error


junaidahmd
Mar 3, 2013, 05:31 AM
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;
}
}
}

rameshspry
Jul 4, 2013, 10:51 PM
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);
}

}