| |

VerySource

 Forgot password?
 Register
Search
View: 647|Reply: 2

Please help me analyze what is wrong with this code

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-2 15:00:01
| Show all posts |Read mode
class Rectangle
{
int l, w;
int perimeter ()
{
return 2 * (l + w);
}
int area ()
{
return l * w;
}
public static void main (String [] args)
{
Rectangle rect1 = new Rectangle ();
Rectangle rect2 = new Rectangle ();
Ranch
rect1.l = 10;
rect1.w = 5;
System.out.println (rect1.perimeter ());
System.out.println (rect2.area ());
Ranch
rect2.l = 6;
rect2.w = 4;
System.out.println (rect2.perimeter ());
System.out.println (rect2.area ());
}
}

This is a piece of code to find the perimeter and area of ​​a rectangle
The print result is indeed
30
0. This 0 is wrong. It should be 50.
20
twenty four

What is wrong with this result "0", thank you
Reply

Use magic Report

0

Threads

9

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-3-21 11:30:01
| Show all posts
public static void main (String [] args)
    {
        Rectangle rect1 = new Rectangle ();
        Rectangle rect2 = new Rectangle ();

        rect1.l = 10;
        rect1.w = 5;
        System.out.println (rect1.perimeter ());
        System.out.println (rect2.area ()); ===> should be (rect1.area ())

        rect2.l = 6;
        rect2.w = 4;
        System.out.println (rect2.perimeter ());
        System.out.println (rect2.area ());
    }
Write carefully
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-3-22 11:15:02
| Show all posts
Oh oh thank you
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list