| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1122 人关注过本帖
标题:狂郁闷!请赐教
只看楼主 加入收藏
无铭剑客
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2005-10-31
收藏
 问题点数:0 回复次数:10 
狂郁闷!请赐教

编了一下午java程序,竟然调试出错,看了半天也没检查出错误。

也没人能请教,真是有点郁闷。

无奈!
请朋友帮忙指正!




创建一个Rectangle类,该类拥有属性length和width,每个属性的默认值均为1。该类拥有方法perimeter和area,分别用于计算矩形的周长和面积。该类还有设置和读取属性length和width的方法。设置方法应检查length和width的属性值是否是大于0.0小于20.0的浮点数。编写一个程序测试Rectangle类。
自己编写的源程序:
import javax.swing.JOptionPane;
public class Rectangle{
private float length=1,width=1;
public void perimeter(){
float a;
a=2*(length+width);
System.out.println("矩形的周长是: "+a);
}
public void area(){
float a;
a=length*width;
System.out.println("矩形的面积是: "+a);
}
public void reset(){
String str=JOptionPane.showInputDialog("请输入length值:");
length=Float.parseFloat(str);
String str=JOptionPane.showInputDialog("请输入width值:");
width=Float.parseFloat(str);
}
public void test(){
if(length>0&&length<20)
System.out.println("length是大于0.0小于20.0的浮点数");
else
System.out.println("length不是大于0.0小于20.0的浮点数");
if(width>0&&width<20)
System.out.println("width是大于0.0小于20.0的浮点数");
else
System.out.println("width不是大于0.0小于20.0的浮点数");

public void read(){
System.out.println("length的值是 "+length);
System.out.println("width的值是 "+width);
}
public static void main(String args[]){
int a;
System.out.println("1:计算矩形的周长");
System.out.println("2:计算矩形的面积");
System.out.println("3:重新设置矩形的长和宽");
System.out.println("4:读取矩形的长和宽");
System.out.println("5:检测长、宽值得范围");
String str=JOptionPane.showInputDialog("请选择上述中的一种操作:");
a=Integer.parseInt(str);
switch(a){
case 1:perimeter();break;
case 2:area();break;
case 3:reset();break;
case 4:read();break;
case 5:test();break;
default:System.out.println("Wrong input!");
}
}
}








搜索更多相关主题的帖子: length Rectangle 属性 该类 
2006-05-09 18:36
★王者至尊★
Rank: 1
等 级:新手上路
帖 子:528
专家分:0
注 册:2006-3-28
收藏
得分:0 
括号的匹配有问题

------Java 爱好者,论坛小混混,学习中------
2006-05-09 18:49
★王者至尊★
Rank: 1
等 级:新手上路
帖 子:528
专家分:0
注 册:2006-3-28
收藏
得分:0 

这是我改过的:
--------------------------------------------------------

import javax.swing.JOptionPane;
public class Rectangle{
private float length=1,width=1;
public void perimeter(){
float a;
a=2*(length+width);
System.out.println("矩形的周长是: "+a);
}
public void area(){
float a;
a=length*width;
System.out.println("矩形的面积是: "+a);
}
public void reset(){
String str1=JOptionPane.showInputDialog("请输入length值:");
length=Float.parseFloat(str1);
String str2=JOptionPane.showInputDialog("请输入width值:");
width=Float.parseFloat(str2);
}
public void read(){
System.out.println("length的值是 "+length);
System.out.println("width的值是 "+width);
}

public void test(){
if(length>0&&length<20)
System.out.println("length是大于0.0小于20.0的浮点数");
else
System.out.println("length不是大于0.0小于20.0的浮点数");
if(width>0&&width<20)
System.out.println("width是大于0.0小于20.0的浮点数");
else
System.out.println("width不是大于0.0小于20.0的浮点数");

}
public static void main(String args[]){
int a;
System.out.println("1:计算矩形的周长");
System.out.println("2:计算矩形的面积");
System.out.println("3:重新设置矩形的长和宽");
System.out.println("4:读取矩形的长和宽");
System.out.println("5:检测长、宽值得范围");
String str=JOptionPane.showInputDialog("请选择上述中的一种操作:");
a=Integer.parseInt(str);
switch(a){
case 1:new Rectangle().perimeter();break;
case 2:new Rectangle().area();break;
case 3:new Rectangle().reset();break;
case 4:new Rectangle().read();break;
case 5:new Rectangle().test();break;
default:System.out.println("Wrong input!");
}
}
}


-------------------------------------------------------------------


------Java 爱好者,论坛小混混,学习中------
2006-05-09 18:52
zhouxin
Rank: 2
等 级:新手上路
威 望:4
帖 子:76
专家分:0
注 册:2006-4-28
收藏
得分:0 
这个程序不好,怎么完成一个任务就自动关闭了。

好好编程好好学习
2006-05-10 16:43
zhouxin
Rank: 2
等 级:新手上路
威 望:4
帖 子:76
专家分:0
注 册:2006-4-28
收藏
得分:0 

import javax.swing.JOptionPane;
public class Rectangle{
private float length=1,width=1;
public void perimeter(){
float a;
a=2*(length+width);
System.out.println("矩形的周长是: "+a);
}
public void area(){
float a;
a=length*width;
System.out.println("矩形的面积是: "+a);
}
public void reset(){
String str1=JOptionPane.showInputDialog("请输入length值:");
length=Float.parseFloat(str1);
String str2=JOptionPane.showInputDialog("请输入width值:");
width=Float.parseFloat(str2);
}
public void read(){
System.out.println("length的值是 "+length);
System.out.println("width的值是 "+width);
}

public void test(){
if(length>0&&length<20)
System.out.println("length是大于0.0小于20.0的浮点数");
else
System.out.println("length不是大于0.0小于20.0的浮点数");
if(width>0&&width<20)
System.out.println("width是大于0.0小于20.0的浮点数");
else
System.out.println("width不是大于0.0小于20.0的浮点数");

}
public static void main(String args[]){
int a;
System.out.println("1:计算矩形的周长");
System.out.println("2:计算矩形的面积");
System.out.println("3:重新设置矩形的长和宽");
System.out.println("4:读取矩形的长和宽");
System.out.println("5:检测长、宽值得范围");
System.out.println("0:Exit.");
while (true)
{
String str=JOptionPane.showInputDialog("请选择上述中的一种操作:");
a=Integer.parseInt(str);
switch(a){
case 1:new Rectangle().perimeter();break;
case 2:new Rectangle().area();break;
case 3:new Rectangle().reset();break;
case 4:new Rectangle().read();break;
case 5:new Rectangle().test();break;
case 0:System.exit(0);
default:System.out.println("Wrong input!");
}
}
}
}


好好编程好好学习
2006-05-10 16:48
zhouxin
Rank: 2
等 级:新手上路
威 望:4
帖 子:76
专家分:0
注 册:2006-4-28
收藏
得分:0 
还有一点,建议这种题目可以全部用swing来实现,可以试试。

好好编程好好学习
2006-05-10 16:52
无铭剑客
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2005-10-31
收藏
得分:0 
谢谢楼上的朋友,在问一下.二楼朋友改后能编译但不能运行,是怎么回事,难道是我编译器的问题??
2006-05-11 00:26
无铭剑客
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2005-10-31
收藏
得分:0 
提示出错信息为"Exception in thread "main" java.lang.NoClassDefFoundError:Rectangle"
2006-05-11 00:29
水水水
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2006-5-11
收藏
得分:0 

崩溃啊!
我发的贴怎么没人看啊,看楼主好像瞒行的
帮我看一下一个简单的程序,帮忙改一下,行不?谢了!
import java.lang.Math;
public abstract class Area {
public abstract double area();
}
public class RectArea extends Area{
double x,y;
public double area() {
double s1;
s1=x*y;
System.out.println("s1="+s1);
return s1;
}
}
public class RoundArea extends Area{
double z;
public double area(){
double s2;
s2=Math.PI*z*z;
System.out.println("s2="+s2);
return s2;
}
}
public class ImpleArea{
public static void main(String args[]){
RectArea f1=new RectArea();
RoundArea f2=new RoundArea();
try{
System.in.read(double x,double y,double z);
}
catch(Exception e){
System.out.println("error:"+e.toString());
}
f1.area();
f2.area();
}
}

2006-05-11 00:31
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
System.in.read(double x,double y,double z);
你这是什么意思?

可惜不是你,陪我到最后
2006-05-11 10:11
快速回复:狂郁闷!请赐教
数据加载中...
 
   



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

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