我的代码有什么问题?为什么不能正确计算圆的面积?
What's wrong with my code ? Why it is not calculating the area of circle correctly?
What's wrong with my code ? Why it is not calculating the area of
cirlce correctly in case of circle as a shape? Every time it is
calculating the area of circle equals to 0.Rest of the code is working
fine.
package constructor;
import java.util.Scanner;
class input{
float c_area;
int s_area,r_area;
input(int side1,String type)
{
if(type.equals("circle"))
{
c_area=3.14f*side1*side1;
}
else{
s_area=side1*side1;
}
}
input(int l,int b){
r_area=l*b;
}
void area(String select){
if(select.equals("cirlce"))
{
System.out.println("Area is: "+c_area);
}
else if(select.equals("square")){
System.out.println("Area is: "+s_area);
}
else
{
System.out.println("Area is:"+r_area);
}
}
};
public class shape {
public static void main(String[] args) {
String name;
char ch;
Scanner obj=new Scanner(System.in);
do{
System.out.print("ENTER THE SHAPE TYPE:");
name=obj.next();
if(name.equals("circle"))
{
int radius;
System.out.print("Enter the radius: ");
radius=obj.nextInt();
input a=new input(radius,name);
a.area(name);
}
else if(name.equals("rectangle"))
{
int len,bre;
System.out.print("Enter LENGTH & BREADTH: ");
len=obj.nextInt();
bre=obj.nextInt();
input x=new input(len,bre);
x.area(name);
}
else if(name.equals("square"))
{
int side;
System.out.print("Enter side: ");
side=obj.nextInt();
input x=new input(side,name);
x.area(name);
}
System.out.println("continue: Y or N:");
ch=obj.next().charAt(0);
}while(ch=='y' || ch=='Y');
}
}
What's wrong with my code ? Why it is not calculating the area of
cirlce correctly in case of circle as a shape? Every time it is
calculating the area of circle equals to 0.Rest of the code is working
fine.
问题出在您输入的这段代码 class:
if(type.equals("circle"))
{float c_area;
c_area=3.14f*side1*side1;
}
如果将其设为局部变量而不是实例变量,则在内部声明 c_area,请改为尝试此
if(type.equals("circle"))
{
this.c_area=3.14f*side1*side1;
}
希望对您有所帮助..
看看这两行:
float c_area;
c_area=3.14f*side1*side1;
您在构造函数中声明了一个名为 c_area
的变量,而您在 class 作用域声明中已经有一个以相同方式命名的变量。所以编译器忽略 class 级别变量,而是计算构造函数级别变量。当然,class level 变量不会改变,默认情况下保持为零。只需从构造函数中删除 float c_area;
就可以了
注意:如果您是 Java 的新手,您应该知道它遵循某些代码约定。您应该使用 UpperCase 样式命名您的 classes,并且您的变量名称应该是 camelCased
最重要的是,您最好使用 "Math.PI" 而不是只取 3.14f,它更准确;
在我的 Oppinoin 中,您应该使用静态方法来计算 "c_area"(使用参数(半径,类型)和即时 return。
我看不到需要创建一个对象,所以你可以删除你的构造函数(因为以后不需要它来处理这个对象。
如果这是你的全部代码,我个人会创建一个方法来计算面积..并且不要使用另一个 class
他的程序非常好,没有编码错误。
唯一的问题是空白区有错字(String select){ } 方法
if(select.equals("cirlce"))
应该是 if(select.equals("circle"))
圆圈拼写错误
What's wrong with my code ? Why it is not calculating the area of cirlce correctly in case of circle as a shape? Every time it is calculating the area of circle equals to 0.Rest of the code is working fine.
package constructor;
import java.util.Scanner;
class input{
float c_area;
int s_area,r_area;
input(int side1,String type)
{
if(type.equals("circle"))
{
c_area=3.14f*side1*side1;
}
else{
s_area=side1*side1;
}
}
input(int l,int b){
r_area=l*b;
}
void area(String select){
if(select.equals("cirlce"))
{
System.out.println("Area is: "+c_area);
}
else if(select.equals("square")){
System.out.println("Area is: "+s_area);
}
else
{
System.out.println("Area is:"+r_area);
}
}
};
public class shape {
public static void main(String[] args) {
String name;
char ch;
Scanner obj=new Scanner(System.in);
do{
System.out.print("ENTER THE SHAPE TYPE:");
name=obj.next();
if(name.equals("circle"))
{
int radius;
System.out.print("Enter the radius: ");
radius=obj.nextInt();
input a=new input(radius,name);
a.area(name);
}
else if(name.equals("rectangle"))
{
int len,bre;
System.out.print("Enter LENGTH & BREADTH: ");
len=obj.nextInt();
bre=obj.nextInt();
input x=new input(len,bre);
x.area(name);
}
else if(name.equals("square"))
{
int side;
System.out.print("Enter side: ");
side=obj.nextInt();
input x=new input(side,name);
x.area(name);
}
System.out.println("continue: Y or N:");
ch=obj.next().charAt(0);
}while(ch=='y' || ch=='Y');
}
}
What's wrong with my code ? Why it is not calculating the area of cirlce correctly in case of circle as a shape? Every time it is calculating the area of circle equals to 0.Rest of the code is working fine.
问题出在您输入的这段代码 class:
if(type.equals("circle"))
{float c_area;
c_area=3.14f*side1*side1;
}
如果将其设为局部变量而不是实例变量,则在内部声明 c_area,请改为尝试此
if(type.equals("circle"))
{
this.c_area=3.14f*side1*side1;
}
希望对您有所帮助..
看看这两行:
float c_area;
c_area=3.14f*side1*side1;
您在构造函数中声明了一个名为 c_area
的变量,而您在 class 作用域声明中已经有一个以相同方式命名的变量。所以编译器忽略 class 级别变量,而是计算构造函数级别变量。当然,class level 变量不会改变,默认情况下保持为零。只需从构造函数中删除 float c_area;
就可以了
注意:如果您是 Java 的新手,您应该知道它遵循某些代码约定。您应该使用 UpperCase 样式命名您的 classes,并且您的变量名称应该是 camelCased
最重要的是,您最好使用 "Math.PI" 而不是只取 3.14f,它更准确;
在我的 Oppinoin 中,您应该使用静态方法来计算 "c_area"(使用参数(半径,类型)和即时 return。 我看不到需要创建一个对象,所以你可以删除你的构造函数(因为以后不需要它来处理这个对象。
如果这是你的全部代码,我个人会创建一个方法来计算面积..并且不要使用另一个 class
他的程序非常好,没有编码错误。
唯一的问题是空白区有错字(String select){ } 方法
if(select.equals("cirlce"))
应该是 if(select.equals("circle"))
圆圈拼写错误