不知道理解的意思对不对,另外
public static int parseInt(char[] c){
……
}
老是出错,顺便请高手分析一下,到底错在哪里?
顺便把运行结果,以及抛出的异常也贴上来:
mInt.get()=3
nInt.get()=4
mInt.value不是偶数!
mInt.value是奇数!
mInt.value是素数!
4是偶数!
4不是奇数!
4不是素数!
nInt.value是偶数!
nInt.value不是奇数!
nInt.value不是素数!
3!=4
4=4
3!=4
java.lang.NumberFormatException: For input string: "???"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.valueOf(Unknown Source)
at test.MyInteger.parseInt(TestMyInteger.java:120)
at test.TestMyInteger.main(TestMyInteger.java:211)
c=0,s=12345
下面是原代码:
package test;
class MyInteger{
int value;
public MyInteger(int n){
this.value=n;
}
public int get(){
return value;
}
//*******************
public boolean isEven(){
if(this.value%2==0){
return true;
}
return false;
}
public boolean isOdd(){
if(this.value%2==1){
return true;
}
return false;
}
public boolean isPrime(){
for(int i=2;i<=(int)Math.sqrt(this.value);i++){
if(this.value%i==0){
return false;
}
}
return true;
}
//********************
public boolean isEven(int n){
if(n%2==0){
return true;
}
return false;
}
public boolean isOdd(int n){
if(n%2==1){
return true;
}
return false;
}
public boolean isPrime(int n){
for(int i=2;i<=(int)Math.sqrt(n);i++){
if(n%i==0){
return false;
}
}
return true;
}
//*********************
public boolean isEven(MyInteger m){
if(m.get()%2==0){
return true;
}
return false;
}
public boolean isOdd(MyInteger m){
if(m.get()%2==1){
return true;
}
return false;
}
public boolean isPrime(MyInteger m){
for(int i=2;i<=(int)Math.sqrt(m.get());i++){
if(m.get()%i==0){
return false;
}
}
return true;
}
public boolean equals(int n){
if(this.value==n){
return true;
}
return false;
}
public boolean equals(MyInteger m){
if(this.value==m.value){
return true;
}
return false;
}
public static int parseInt(char[] c){
try{
String str=String.valueOf(c);
int num=Integer.valueOf(str);
return num;
}catch(Exception e){
e.printStackTrace();
return 0;
}
}
public static int parseInt(String str){
try{
int num=Integer.valueOf(str);
return num;
}catch(Exception e){
e.printStackTrace();
return 0;
}
}
}
public class TestMyInteger {
public static void main(String[] args){
MyInteger mInt=new MyInteger(3);
MyInteger nInt=new MyInteger(4);
int num=4;
char[] c={1,2,3};
String s="12345";
System.out.println("mInt.get()="+mInt.get());
System.out.println("nInt.get()="+nInt.get());
if(mInt.isEven())
System.out.println("mInt.value是偶数!");
else
System.out.println("mInt.value不是偶数!");
if(mInt.isOdd())
System.out.println("mInt.value是奇数!");
else
System.out.println("mInt.value不是奇数!");
if(mInt.isPrime())
System.out.println("mInt.value是素数!");
else
System.out.println("mInt.value不是素数!");
//********************
if(mInt.isEven(num))
System.out.println(num+"是偶数!");
else
System.out.println(num+"不是偶数!");
if(mInt.isOdd(num))
System.out.println(num+"是奇数!");
else
System.out.println(num+"不是奇数!");
if(mInt.isPrime(num))
System.out.println(num+"是素数!");
else
System.out.println(num+"不是素数!");
//************************
if(nInt.isEven(nInt))
System.out.println("nInt.value是偶数!");
else
System.out.println("nInt.value不是偶数!");
if(nInt.isOdd(nInt))
System.out.println("nInt.value是奇数!");
else
System.out.println("nInt.value不是奇数!");
if(nInt.isPrime(nInt))
System.out.println("nInt.value是素数!");
else
System.out.println("nInt.value不是素数!");
if(mInt.equals(num))
System.out.println(mInt.value+"="+num);
else
System.out.println(mInt.value+"!="+num);
if(nInt.equals(num))
System.out.println(nInt.value+"="+num);
else
System.out.println(nInt.value+"!="+num);
if(mInt.equals(nInt))
System.out.println(mInt.value+"="+nInt.value);
else
System.out.println(mInt.value+"!="+nInt.value);
int n1=mInt.parseInt(c);
int n2=mInt.parseInt(s);
System.out.println("c="+n1+",s="+n2);
}
}
[
本帖最后由 琢石成器 于 2013-11-16 15:58 编辑 ]