我不明白怎么了。肯定是的。我只是菜鸟。我的想法是输入所有这些参数,然后在TestMyInteger类上运行它。我在线程“主要” java.lang.NoClassDefFoundError中遇到主要错误异常:MyInteger 在TestMyInteger.main(TestMyInteger.java:3) 原因:java.lang.ClassNotFoundException:MyInteger
我不知道我的代码出了什么问题
public class MyInteger {
private int value;
public MyInteger (int value) {
this.value = value;
} //myInteger
public int getValue() {
return value;
}//getValue
public boolean isEven() {
return (value % 2 == 0);
}//isEven(not static)
public boolean isOdd() {
return (value % 2 != 0);
}//isOdd(not static)
public boolean isPrime() {
int i = 0;
for(i = 2; i <= value / 2; i++){
if(value % i != 0)
return true;
}
return false;
}//isPrime (not static)
public static boolean isEven (int n) {
return (n % 2 == 0);
}//isEven(static)
public static boolean isOdd(int n) {
return (n % 2 != 0);
}//isOdd(static)
public static boolean isPrime(int n) {
int i = 0;
for(i = 2; i <= n / 2; i++){
if(n % i != 0)
return true;
}
return false;
}//isPrime(static)
public static boolean isEven(MyInteger n) {
return n.isEven();
}// myInteger isEven
public static boolean isPrime(MyInteger o) {
int i = 0;
for(i = 2; i <= o / 2; i++){
if(o % i != 0)
return true;
}
return false;
} //myInteger prime
public static boolean isOdd(MyInteger o) {
return (o % 2 != 0);
}//MyInteger isOdd
public boolean equals(int anotherNum) {
if (n1 == n2)
return true;
return false;
} //what value is this (equals)
public boolean equals(MyInteger o) {
if (n1 == n2)
return true;
return false;
`enter code here` }//MyInteger equals
}
所有这些都将在此TestMyInteger类上运行
public class TestMyInteger {
public static void main(String[] args) {
MyInteger n1 = new MyInteger(5);
System.out.println("n1 is even? " + n1.isEven());
System.out.println("n1 is prime? " + n1.isPrime());
System.out.println("15 is prime? " + MyInteger.isPrime(15));
System.out.println("15 is even? " + MyInteger.isEven(15));
MyInteger n2 = new MyInteger(24);
System.out.println("n2 is odd? " + n2.isOdd());
System.out.println("45 is odd? " + MyInteger.isOdd(45));
System.out.println("n1 is equal to n2? " + n1.equals(n2));
System.out.println("n1 is equal to 5? " + n1.equals(5));
System.out.println("n2 is even? " + MyInteger.isEven(n2));
System.out.println("n2 is odd? " + MyInteger.isOdd(n2));
System.out.println("n2 is prime? " + MyInteger.isPrime(n2));
}
}//TestMyInteger
答案 0 :(得分:0)
委托,请不要一遍又一遍地重新实施相同的方法。为了从value
实例访问MyInteger
,您需要使用instance.value
。您的isPrime
逻辑似乎是相反的。不要尝试更改Object.equals(Object)
的签名。如果您打算覆盖equals
,则也应该覆盖hashCode
和toString
。喜欢,
public class MyInteger {
private int value;
public MyInteger(int value) {
this.value = value;
} // myInteger
public int getValue() {
return value;
}// getValue
public boolean isEven() {
return isEven(value);
}// isEven(not static)
public boolean isOdd() {
return isOdd(value);
}// isOdd(not static)
public boolean isPrime() {
return isPrime(value);
}// isPrime (not static)
public static boolean isEven(int n) {
return (n % 2 == 0);
}// isEven(static)
public static boolean isOdd(int n) {
return !isEven(n);
}// isOdd(static)
public static boolean isPrime(int n) {
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}// isPrime(static)
public static boolean isEven(MyInteger n) {
return isEven(n.value);
}// myInteger isEven
public static boolean isPrime(MyInteger o) {
return isPrime(o.value);
} // myInteger prime
public static boolean isOdd(MyInteger o) {
return isOdd(o.value);
}// MyInteger isOdd
public boolean equals(int anotherNum) {
return value == anotherNum;
} // what value is this (equals)
@Override
public boolean equals(Object o) {
if (o instanceof MyInteger) {
return equals(((MyInteger) o).value);
}
return false;
}// MyInteger equals
@Override
public int hashCode() {
return Integer.hashCode(value);
}
@Override
public String toString() {
return String.valueOf(value);
}
}
除非可以编译MyInteger
,否则不能使用MyInteger
。电脑不知道怎么办。
答案 1 :(得分:0)
public final class MyInteger {
private final int value;
public MyInteger(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public boolean isEven() {
return isEven(value);
}
public boolean isOdd() {
return isOdd(value);
}
public boolean isPrime() {
return isPrime(value);
}
public static boolean isEven(int value) {
return value % 2 == 0;
}
public static boolean isOdd(int value) {
return !isEven(value);
}
public static boolean isPrime(int value) {
if (Math.abs(value) < 2)
return false;
for (int i = 2, max = (int)Math.sqrt(value); i <= max; i++)
if (value % i == 0)
return false;
return true;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
return obj instanceof MyInteger && value == ((MyInteger)obj).value;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public String toString() {
return String.valueOf(value);
}
}