java.util.scanner - java scanner output strange if else logic errors -
hey people greetings m new coder wanted know problem in code kindly tell logic unable print else statement there after when entering age high 20 output still same of if statement
code
import java.util.*; public class shivam { int age; void function() { if (age<=10) { system.out.println("chutiye chota h tu"); } else { system.out.println("bada ho gya saale"); } } public static void main(string[] args) { scanner sc= new scanner(system.in); int age=sc.nextint(); shivam s1=new shivam(); s1.function(); sc.close(); } }
the reason create instance of custom class function() method.
shivam s1=new shivam(); the int age; sets value of age default primitive i.e 0.
now when call
s1.function(); the condition
if (age<=10) //evaluates true now, fix should accept age parameter function() instead of being field of class as:
public static void function(int age) { if (age<=10) { ... } else { ... } } and call same method
s1.function(age); // inside main method
Comments
Post a Comment