| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 418 人关注过本帖
标题:构造函数出现问题!
只看楼主 加入收藏
yynn
Rank: 1
等 级:新手上路
帖 子:279
专家分:0
注 册:2005-11-4
收藏
 问题点数:0 回复次数:2 
构造函数出现问题!
import java.util.*;
public class Gnome{
public String name;
public int age;
public Gnome gnome_buddy;
private boolean magical= false;
public double height = 2.6;
public static final int MAX_HEIGHT = 3;
Gnome(String nm , int ag , Gnome bud, double hgt){
name = nm;
age = ag;
gnome_buddy = bud;
height = hgt;
}
Gnome(){
name ="Runmple";
age =204;
gnome_buddy = null;
height = 2.1;
}
public static void makeKing(Gnome h){
h.name = "King" + h.getRealName();
// h.magical = ture;
}
public void makeMeKing(){
name = "King" +getRealName();
// magical= ture;
}
public boolean isMagical(){
return magical;
}
public void setHeight(){
// height= newHeight;

}
public String getName(){
return "I won't tell!";
}
public String getRealName(){
return name;
}
public void renameGnome(String s ){
name = s;
}

}
class Test{
public static void main(String args[]){
Scanner i = new Scanner(System.in);
Gnome g = new Gnome();
System.out.println("Enter the name");
g.age = i.nextInt();
System.out.println(g.age);
}

}

只有注释掉一下东西才能运行,但是本来代码是有的,但是为什么会错呢!
搜索更多相关主题的帖子: 函数 构造 
2007-10-22 19:27
yynn
Rank: 1
等 级:新手上路
帖 子:279
专家分:0
注 册:2005-11-4
收藏
得分:0 
public class CreditCard{
private String number;
private String name;
private String bank;
private double balance;
private int limit;
CreditCard(String no ,String nm,String bk,double bal , int lim){
number = no;
name = nm;
bank = bk;
balance = bal;
limit=lim;

}
public String getNumber(){
return number;
}
public String getName(){
return name ;

}
public String getBank(){
return bank;
}
public double getBalance(){
return balance;

}
public int getLimit(){
return limit;
}
public boolean chargelt(double price){
if (price + balance >(double)limit)
return false;
balance += price;
return true;
}
public void makePayment(double payment){
balance -=payment;
}
public static void printCard(CreditCard c){
System.out.println("Number="+c.number);
System.out.println("Name="+c.name);
System.out.println("Bank="+c.bank);
System.out.println("Balance="+c.balance);
System.out.println("Limit="+c.limit);

}
}
class Test{
public static void main(String args[]){
CreditCard wallet[] = new CreditCard[10];
wallet[0] = new CreditCard("5392 9083 8932 9034","Tao Qin","Ireland",0.0,1);
wallet[1] = new CreditCard("4839 4393 3290 9098","Le Jie","Ireland",0.0,1);
for (int i= 1 ; i<4 ;i++){
wallet[0].chargelt((double)i);
wallet[1].chargelt(2.0*i);
}
System.out.println("Card payment:");
for(int i = 0; i<2 ; i++){
CreditCard.printCard(wallet[i]);
while(wallet[i].getBalance()>100.0){
wallet[i].makePayment(100.0);
System.out.println("New balance = " +wallet[i].getBalance());
}
System.out.println();
}

}
}
程序正常,但是当我余额超过limit的时候,没有返回falselimit直接赋值给balance

2007-10-22 20:28
fengyingxzx
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2007-8-28
收藏
得分:0 

上面那个:前两个是不是因为true写成了ture了?第三个,你没定义newHeight啊!


2007-10-22 22:47
快速回复:构造函数出现问题!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012173 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved