2015-11-05 62 views
-20

該代碼與第2行第12行有問題。請幫助。 代碼在圖像中。Java和printf

import java.util.Scanner; 
public class first{ 
    public static void main(String[] args){ 
     Scanner b = new Scanner(System.in); 
     pro nc = new pro(); 
     System.out.println("Enter the name of your first true crush"); 
     String temp = b.nextLine(); 
     nc.setname(temp); 
     nc.etc(); 
    } 
} 

public class pro{ 
    private String gname; 
    public String getName() { 
     return gname; 
    } 
    public void setName(String name) { 
     this.gname = name; 
    } 
    public void etc(){ 
     System.out.printf("The name of your true crush was %s",getName()); 
    }   
} 
+1

請編輯您的問題發佈代碼,而不是截圖。 – Arc676

+1

此外,「有一些問題」並沒有清楚地描述問題。請在問題中提供錯誤的完整說明。 –

+1

可能的重複[爲什麼我得到一個簡單的printf編譯錯誤?](http://stackoverflow.com/questions/7774428/why-am-i-getting-a-compilation-errors-with-a-simple -printf) – thegauravmahawar

回答

2

沒有與此

I AM USING NETBEANS IDE. 



/* 

    /// 

public class pro { 
private String gname; 

    public String getname() { 
     return gname; 
    } 

    public void setname(String name) { 
     gname = name; 
    } 
    public void etc(){ 
     System.out.printf("Tht name is %s",getname()); 
    } 
} 

///////// 

    import java.util.Scanner; 

     public class first { 
     public static void main(String[] args) { 
     Scanner b= new Scanner(System.in); 
     pro nc = new pro(); 
     System.out.println("enter the name"); 
     String temp = b.nextLine(); 
     nc.setname(temp); 
     nc.etc(); 
    } 
} 



///////// 

*/ 

複覈這個沒有問題。

我沒有改變你的代碼,只是鍵入它。
是的,我同意Arc676應該發佈代碼。

+0

其實我已經嘗試直接發佈代碼......但它只是每次報告某種問題 –

0

netbeans沒有問題。 您可以嘗試使用System.out.format而不是System.out.printf

import java.util.Scanner; 
public class first{ 
    public static void main(String[] args){ 
     Scanner b = new Scanner(System.in); 
     pro nc = new pro(); 
     System.out.println("Enter the name of your first true crush"); 
     String temp = b.nextLine(); 
     nc.setname(temp); 
     nc.etc(); 
    } 
} 
public static class pro{ 
    private String gname; 
    public String getName() { 
     return gname; 
    } 
    public void setName(String name) { 
     this.gname = name; 
    } 
    public void etc(){ 
     System.out.format("The name of your true crush was %s", getName()); 
    }   
} 
3

在類2中,您尚未在System.out.printf()中指定該函數的位置。你應該使用這個關鍵字,並重新寫作

public class pro { 
    private String name; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
    this.name = name; 
    } 
    public void etc(){ 
     System.out.printf("The carp is %s",this.getName()); 
    } 

}