java中怎么输出不了?
importjava.util.Scanner;publicclassTest4{privatestaticdoublewidth;privatestaticdouble...
import java.util.Scanner;
public class Test4{
private static double width;
private static double length;
private String perimeter;
private String area;
//
private Test4(){}
//
private void Test4(double width,double length){
this.width=width;
this.length=length;}
//set get
public void setwidth(double width){
this.width=width;}
public void setlength(double length){
this.length=length;}
public double getwidth(){
return this.width;}
public double getlength(){
return this.length;}
//
public double getperimeter(){
return (width+length)*2;}
public double getarea(){
return width*length;}
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入长方形的长");
length = sc.nextInt();
System.out.println("请输入长方形的宽");
width = sc.nextInt();
System.out.println("长方形的周长是:"+sc.perimeter()+"长方形的面积是:"+sc.area());
}
}
为什么输出不了?难道sc.perimeter()这样的格式不对吗? 展开
public class Test4{
private static double width;
private static double length;
private String perimeter;
private String area;
//
private Test4(){}
//
private void Test4(double width,double length){
this.width=width;
this.length=length;}
//set get
public void setwidth(double width){
this.width=width;}
public void setlength(double length){
this.length=length;}
public double getwidth(){
return this.width;}
public double getlength(){
return this.length;}
//
public double getperimeter(){
return (width+length)*2;}
public double getarea(){
return width*length;}
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入长方形的长");
length = sc.nextInt();
System.out.println("请输入长方形的宽");
width = sc.nextInt();
System.out.println("长方形的周长是:"+sc.perimeter()+"长方形的面积是:"+sc.area());
}
}
为什么输出不了?难道sc.perimeter()这样的格式不对吗? 展开
4个回答
展开全部
函数调用错误,周长,面积可以再main里直接调用,main里面直接调用改成静态方法
注意输入的长和宽只能为整数
import java.util.Scanner;
public class Test4{
private static double width;
private static double length;
private String perimeter;
private String area;
//
private Test4(){}
//
private void Test4(double width,double length){
this.width=width;
this.length=length;}
//set get
public void setwidth(double width){
this.width=width;}
public void setlength(double length){
this.length=length;}
public double getwidth(){
return this.width;}
public double getlength(){
return this.length;}
//
public static double getperimeter(){
return (width+length)*2;}
public static double getarea(){
return width*length;}
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入长方形的长");
length = sc.nextInt();
System.out.println("请输入长方形的宽");
width = sc.nextInt();
System.out.println("长方形的周长是:"+getperimeter()+"长方形的面积是:"+getarea());
// System.out.println("长方形的周长是:"+sc.perimeter()+"长方形的面积是:"+sc.area());
}
}
注意输入的长和宽只能为整数
import java.util.Scanner;
public class Test4{
private static double width;
private static double length;
private String perimeter;
private String area;
//
private Test4(){}
//
private void Test4(double width,double length){
this.width=width;
this.length=length;}
//set get
public void setwidth(double width){
this.width=width;}
public void setlength(double length){
this.length=length;}
public double getwidth(){
return this.width;}
public double getlength(){
return this.length;}
//
public static double getperimeter(){
return (width+length)*2;}
public static double getarea(){
return width*length;}
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入长方形的长");
length = sc.nextInt();
System.out.println("请输入长方形的宽");
width = sc.nextInt();
System.out.println("长方形的周长是:"+getperimeter()+"长方形的面积是:"+getarea());
// System.out.println("长方形的周长是:"+sc.perimeter()+"长方形的面积是:"+sc.area());
}
}
2014-12-08
展开全部
没有sc.perimeter()这种用法的,你这样写编译都通不过的。
System.out.println("长方形的周长是:"+(width+length)*2+"长方形的面积是:"+width*length);可以直接这样写,也可以直接调用你写的计算方法
System.out.println("长方形的周长是:"+(width+length)*2+"长方形的面积是:"+width*length);可以直接这样写,也可以直接调用你写的计算方法
追问
这种方法我会但我不想用这种方法,感觉很麻烦而且很老土
追答
你的main方法里面有点错误,我改了一下,直接用就行了
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入长方形的长");
length = sc.nextDouble();
System.out.println("请输入长方形的宽");
width = sc.nextDouble();
System.out.println("长方形的周长是:"+new Test4().getperimeter()+"长方形的面积是:"+new Test4().getarea());
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
打印错误,自己检查
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询