日期:2014-05-20  浏览次数:20876 次

一个莫名奇妙的错误
class Person{
private String name;
private int age;
public Person(String name,int age){
this.name=name;
this.age=age;
}
  public boolean compare(Person per){
  Person p1=this;
  Person p2=per;
  if(p1==p2){
  return true;
  }
  if(p1.name.equals(p2.name)&&p1.age==p2.age){
  return true;
  }else{
  return false;
  }
}
  public class Demo{
  public static void main(String []args){
  Person p1=new Person("sunxiaolong",35);
  Person p2=new Person("nixiaohuan",26);
  if(p1.compare(p2)){
  System.out.println("想等");
  }
  else{
  System.out.println("不相等");
  }
  }
  }
   
}总是提示没有main函数,能解决此问题给最高分

------解决方案--------------------
总是提示没有main函数,能解决此问题给最高分
你如何写的命令?应该是
java Demo吧,你写的java Person?或者java Demo.class ?
------解决方案--------------------
+1
探讨
总是提示没有main函数,能解决此问题给最高分
你如何写的命令?应该是
java Demo吧,你写的java Person?或者java Demo.class ?

------解决方案--------------------
改完了。自己看吧

Java code

class Person{
    private String name;
    private int age;
    public Person(String name,int age){
        this.name=name;    
        this.age=age;
    }
    public boolean compare(Person per){
          Person p1=this;
          Person p2=per;
          if(p1==p2){
              return true;
            }
           if(p1.name.equals(p2.name)&&p1.age==p2.age){
            return true;
           }else{
              return false;
            }
    }

    
}
public class Demo{
  public static void main(String []args){
      Person p1=new Person("sunxiaolong",35);
      Person p2=new Person("nixiaohuan",26);
      if(p1.compare(p2)){
          System.out.println("想等");
      }else{
          System.out.println("不相等");
      }
  }
}

------解决方案--------------------
javac Demo.java
java Demo
结果是不相等
------解决方案--------------------
//基本语法错误,
Java code

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public boolean compare(Person per) {
        Person p1 = this;
        Person p2 = per;
        if (p1 == p2) {
            return true;
        }
        if (p1.name.equals(p2.name) && p1.age == p2.age) {
            return true;
        } else {
            return false;
        }
    }

}
public class Demo {
    public static void main(String[] args) {
        Person p1 = new Person("sunxiaolong", 35);
        Person p2 = new Person("nixiaohuan", 26);
        if (p1.compare(p2)) {
            System.out.println("想等");
        } else {
            System.out.println("不相等");
        }
    }
}

------解决方案--------------------
public static void main(String []args)
public static void main(String[] args)
自己看看。
------解决方案--------------------
public class Demo前面少了一个“}”。
------解决方案--------------------
Java code

class Person{
private String name;
private int age;
public Person(String name,int age){
this.name=name;
this.age=age;
}
  public boolean compare(Person per){
  Person p1=this;
  Person p2=per;
  if(p1==p2){
  return true;
  }
  if(p1.name.equals(p2.name)&&p1.age==p2.age){
  return true;
  }else{
  return false;
  }
}}//括号加在这里 最后的去掉
  public class Demo{
  public static void main(String[] args){
  Person p1=new Person("sunxiaolong",35);
  Person p2=new Person("nixiaohuan",26);
  if(p1.compare(p2)){
  System.out.println("想等");
  }
  else{
  System.out.println("不相等");
  }
  }
  }