2015-10-03 67 views
0

我有代碼一個非常基本的程序比當我輸入System.out.println時,我得到的錯誤:「引用println模糊」,隨機它使用工作,並沒有有一個錯誤,但現在它確實發生了這種錯誤,所以我如何解決這個問題,比你。對println的引用是模棱兩可的錯誤

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, my name is Slash"); 
    System.out.println("What is your name?"); 
    name = scan.nextLine(); 
    System.out.println("Hello " + name); 
    Thread.sleep(1000); 
    System.out.println("How old are 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 to calculate any thing?"); 
    a = scan.nextLine(); 
    if(a==("yes")){ 
     System.out.println("What would u like to do?(multiply,or add)");  
     a = 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; 

     } 


    } 

} 
    } 
+0

它工作在食,但不是在NetBeans – idk0namz

回答

0

首先,nextInt()歲以後不消耗新線(所以你nextLine然後空)。您將Object的值與致電.equals()的值進行比較,因爲==測試參考身份(並使用String我會使用String.equalsIgnoreCase(String))。最後,我會在Java中聲明變量的最小可見性;儘可能接近他們的第一次使用。喜歡的東西,

public static void main(String[] args) throws InterruptedException { 
    Scanner scan = new Scanner(System.in); 

    System.out.println("hello, my name is Slash"); 
    System.out.println("What is your name?"); 
    String name = scan.nextLine(); 
    System.out.println("Hello " + name); 
    Thread.sleep(1000); 
    System.out.println("How old are 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 to calculate any thing?"); 
    scan.nextLine(); // <-- nextInt leaves a trailing new line. 
    String a = scan.nextLine(); 
    if (a.equalsIgnoreCase("yes")) { // <-- compare string for equality in 
             // Java 
     System.out.println("What would u like to do?(multiply,or add)"); 
     a = scan.nextLine(); 
     if (a.equalsIgnoreCase("add")) {// <-- compare string for 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 
     } 
    } 
} 
+0

之後,它仍然心不是固定 – idk0namz

+0

以何種方式是不固定的嗎?你重新定義了「系統」嗎? –

+0

我認爲是這樣的,錯誤仍然是uo的SOP錯誤線程「main」java.lang.RuntimeException異常:不可編譯的源代碼 - 對println的引用不明確 java.io中的方法println(java.lang.String) .PrintStream和java.io.PrintStream中的println(Object)匹配 \t at blue.light.BlueLight.main(BlueLight.java:19) – idk0namz

相關問題