Fresh.587
2021-11-13 23:29
采纳率: 80%
浏览 19
已结题

java里创建的类对象如何输入参数?


package javaapplication22;
import java.util.Scanner;
public class JavaApplication22 {   
     public static void main(String[] args) {
              char x;    
   Scanner reader=new Scanner(System.in); 
      System.out.println("请输入想要计算的图形:");   
    System.out.println("y:圆形\tj:矩形\ts:三角形\t");  
     x=reader.next().charAt(0);    
   if(x=='y'){     
   double r;    
   Shape yuanxing=new Circle("圆形",r);  
     System.out.println("请输入圆形的半径:");  
     r=reader.nextDouble();    
   System.out.print("周长="+yuanxing.getLength());  
     }    
   else if(x=='j'){   
        double c,k;    
   Shape juxing=new Rectangle("矩形",c,k);  
     System.out.println("请输入矩形的长和宽");
       c=reader.nextDouble();    
   k=reader.nextDouble();   
    System.out.print("周长="+juxing.getLength());    
   }     
  else if(x=='s')       {    
   double a1,a2,a3;   
    Shape sanjiaoxing=new Triangle("三角形",a1,a2,a3);  
     System.out.println("请输入三角形的三边:"); 
      a1=reader.nextDouble(); 
      a2=reader.nextDouble();     
  a3=reader.nextDouble();  
     System.out.print("周长="+sanjiaoxing.getLength());    
          }      
                          }  
  }
abstract class Shape{  
  protected String name; 
   public Shape(String xm)    {   
     name=xm;   
     System.out.println("名称"+name);  
  }   
 abstract public double getLength();
}
class Circle extends Shape{   
 private final double PI=3.14; 
   private double radius; 
   public Circle(String shapeName,double r)    
{      
  super(shapeName);  
      radius=r; 
   }   
 public double getLength()    {   
   return 2*PI*radius;  
    }   
 }
class Rectangle extends Shape{  
  private double width; 
   private double height; 
   public Rectangle(String shapeName,double width,double height) 
   {    
    super(shapeName);    
    this.width=width;   
     this.height=height;  
  } 
   public double getLength() 
   {     
   return 2*(width+height);  
  }
}
class Triangle extends Shape{   
 private double a1,a2,a3; 
   public Triangle(String shapeName,double a1,double a2,double a3)  
  {      
  super(shapeName);    
    this.a1=a1;    
    this.a2=a2;   
     this.a3=a3;    
        }    
public double getLength(){   
     return a1+a2+a3; 
   }}

img


怎样才能输入r,c,k,a1,a2,a3这三个变量?还是只能在创建对象是输入固定值?求指点!

图片转代码服务由CSDN问答提供 功能建议

                                                                    Dublely
                  可能尚木初始化变量(
2 nt(”周长=”+yuanxing.getLengthO)
12 可能尚未初始化变量k
                  (AIt-Entershowshints)
 25
                                  Shape juxing=newRectangle(”年形”,c,k);
                                  System.out.print1n(请输入矩形的长和宽”);
                                   c=reader.nextDoubleO
                                   k=reader.nextDoubleO
                                   System.out.print(”周长=”+juxing.getLengthO)
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • 大鹏cool 2021-11-14 09:35
    已采纳

    变量需要先赋值后使用,定义变量的时候可以直接使用 Scanner 读取值并赋给变量

    评论
    解决 1 无用
    打赏 举报

相关推荐 更多相似问题