当前位置: 代码迷 >> J2SE >> 非法的表达式开始解决办法
  详细解决方案

非法的表达式开始解决办法

热度:112   发布时间:2016-04-23 19:52:46.0
非法的表达式开始
用toString方法输出字符串,然后用CountChar计算字符串中某字符的个数,谢了如下程序,编译结果如图,大家帮忙看看哪里有错,需要改进的。




import java.util.Scanner;
public class Employee
{
 public String toString(){
public String name;
public int age;
return "我的名字叫"+name+",年龄为:"+age;  
}
  public int CountChar(String src,char c){
  //public String src;
  //public char c;
  int count=0;
  for(int i=0;i<src.lengtg;i++){
      if(src.charAt(i)==c){
         count++;
  }
  }
          return count;
  }

public static void main(String[] args){
            Scanner input=new Scanner(System.in);
Employee em=new Employee();           //实例化对象
            //输入
System.out.print("请输入您的姓名:");
em.name=input.next();                 //主函数修改子函数的属性值使用这个方法
System.out.print("请输入您的年龄:");
em.age=input.nextInt();
            //调用方法输出
            System.out.println(em.toString());
            System.out.println("*************");
//输入
            System.out.print("请输入字符串:");
String src=input.next();
            System.out.print("请输入字符:");
char c=input.nextChar();
//调用方法输出
System.out.println("此字符串中"+c+"出现的次数是:"+em.CountChar(src,c)+"次");
   
}
}

------解决思路----------------------
public只能修饰类成员,不能修饰局部变量
  相关解决方案