0
Can do this guys. Showing null value.Plz Help me out.
import java.util.*; class Mobile { static String name; static double cp,sp,d; static int pid; static void input() { Scanner sc = new Scanner(System.in); System.out.println("Enter name of product"); String name = sc.next(); System.out.println("Enter price"); double cp = sc.nextDouble(); System.out.println("Enter pid"); int pid = sc.nextInt(); } static void calculation() { d=(20.0/100)*cp; sp=cp+d; } static void display() { System.out.println("Product name: "+name); System.out.println("Final Price: Rs"+sp); System.out.println("----"+pid+"----"); } public static void main(String args[]) { Mobile ph = new Mobile(); ph.input(); ph.calculation(); ph.display(); } }
1 Réponse
+ 1
You're redefining your variables in the input method, so they are local to the input method itself and destroyed at the end of the method. Remove the type from the redeclaration of each.
name = sc.next()
cp = sc.nextDouble()
pid = sc.nextInt()