java - reference to println is ambiguous error -
i have code basic program when type system.out.println, thes error: "reference println ambiguous" , occured randomly use work , did not have erroe , cant figure error occurs, how fix ,than you.
package blue.light; //name is: slash import java.util.*; public class bluelight { public static void main(string[] args) throws interruptedexception { scanner scan = new scanner(system.in); string name; int age; string a; string yes; string no; double num1; double num2; double ans; system.out.println("hello, name slash"); system.out.println("what name?"); name = scan.nextline(); system.out.println("hello " + name); thread.sleep(1000); system.out.println("how old you?"); age = scan.nextint(); if(age < 18) { system.out.println("ur young af"); } else if (age == 21) { system.out.println("shots, shots, shots"); } else if (age == 69) { system.out.println("ohhh yea"); } system.out.println("you calculate thing?"); = scan.nextline(); if(a==("yes")){ system.out.println("what u do?(multiply,or add)"); = scan.nextline(); if(a == "add"){ system.out.println("enter number 1"); num1 = scan.nextdouble(); system.out.println("enter number 2"); num2 = scan.nextdouble(); ans = num1+num2; } } } }
first, nextint()
doesn't consume new line after age (so nextline
empty). compare object
values call .equals()
because ==
tests reference identity (and string
i'd use string.equalsignorecase(string)
). finally, i'd declare variables minimum visibility in java; close first use possible. like,
public static void main(string[] args) throws interruptedexception { scanner scan = new scanner(system.in); system.out.println("hello, name slash"); system.out.println("what name?"); string name = scan.nextline(); system.out.println("hello " + name); thread.sleep(1000); system.out.println("how old you?"); int age = scan.nextint(); if (age < 18) { system.out.println("ur young af"); } else if (age == 21) { system.out.println("shots, shots, shots"); } else if (age == 69) { system.out.println("ohhh yea"); } system.out.println("you calculate thing?"); scan.nextline(); // <-- nextint leaves trailing new line. string = scan.nextline(); if (a.equalsignorecase("yes")) { // <-- compare string equality in // java system.out.println("what u do?(multiply,or add)"); = scan.nextline(); if (a.equalsignorecase("add")) {// <-- compare string equality // in java system.out.println("enter number 1"); double num1 = scan.nextdouble(); system.out.println("enter number 2"); double num2 = scan.nextdouble(); double ans = num1 + num2; system.out.println(ans); // <-- display answer } } }
Comments
Post a Comment