2014-02-05 5 views
0

Как мне назвать переменную длины и ширины в методе getArea без создания частной переменной в классе, так как я делаю это, вызывает метод снова запустить после того, как он уже запускался один раз. Я действительно не нравится этот путь, но вот так, как профессор хочет его сделать, чтобы имитировать раз, прежде чем «объектно-ориентированное программирование»Я пытаюсь вызвать двойную переменную из одного метода в другой

/*This program allows the user to enter the length and widtch and receive the area 
of the rectangle*/ 

import java.util.Scanner; 

public class theRectangleCompany 
{ 
    public static void main(String[] args) 
    { 
    System.out.print("This program will find an area of a Rectangle "); 
    getLength(); 
    getWidth(); 
    getArea(); 
    } 

    public static double getLength() 
    { 
    double length; 
    Scanner keyboard = new Scanner(System.in); 
    System.out.print("Please enter the length "); 
    length = keyboard.nextDouble(); 
    return length; 
    } 

    public static double getWidth() 
    { 
    double width; 
    Scanner keyboard = new Scanner(System.in); 
    System.out.print("Please enter the width "); 
    width = keyboard.nextDouble(); 
    return width; 
    } 

    public static void getArea() 
    { 
     double length = getLength(); 
     double width = getWidth(); 
     double area = width * length; 
     System.out.println("The area of the Rectangle is: " +area); 

    } 


} 
+0

Вы говорите, что вы хотите чтобы избежать использования частной переменной в классе, но как насчет переменной в основном методе? – StephenTG

+0

stephen, она хочет только методы в основном, кроме сообщения, которое я запрашиваю у пользователя, – user3276851

+0

Если методы нужно только вызывать в основном, мой ответ определенно является решением. – developerwjk

ответ

5

Почему вы называете GetLength() и GetWidth() от основного метода , Просто позвоните GetArea()

public static void main(String[] args) 
    { 
    System.out.print("This program will find an area of a Rectangle "); 
    getArea(); 
    } 
+0

вот почему профессор хочет, чтобы это было сделано по какой-то причине, я согласен! он работает лучше, если я так делаю – user3276851

+0

@ user3276851, пожалуйста, примите ответ – Ashish

1

Вы можете сделать параметры сфотографировать функции GetArea и использовать вызовы функций двух других функций как параметры:

getArea(getLength(), getWidth()); 

public static void getArea(double length, double width) { ... } 
0

изменения здесь:

/*This program allows the user to enter the length and widtch and receive the area 
of the rectangle*/ 

import java.util.Scanner; 

public class theRectangleCompany 
{ 
    public static void main(String[] args) 
    { 
    System.out.print("This program will find an area of a Rectangle ");  
    getArea(); 
    } 

    public static double getLength() 
    { 
    double length; 
    Scanner keyboard = new Scanner(System.in); 
    System.out.print("Please enter the length "); 
    length = keyboard.nextDouble(); 
    return length; 
    } 

    public static double getWidth() 
    { 
    double width; 
    Scanner keyboard = new Scanner(System.in); 
    System.out.print("Please enter the width "); 
    width = keyboard.nextDouble(); 
    return width; 
    } 

    public static void getArea() 
    { 
     double length = getLength(); 
     double width = getWidth(); 
     double area = (width * length); 
     System.out.println("The area of the Rectangle is: " +area); 

    } 


} 
0

Не уверен, если это то, что вы хотите:

public static void getArea() 
{ 
    System.out.println("The area of the Rectangle is: " + (getLength() * getWidth())); 
} 

Вы бы также нужно изменить основной метод, чтобы исключить GetLength() и GetWidth():

public static void main(String[] args) 
{ 
    System.out.print("This program will find an area of a Rectangle "); 
    getArea(); 
} 

вариант выше что-то вроде

public static void main(String[] args) 
{ 
    System.out.print("This program will find an area of a Rectangle "); 
    getArea(getLength(),getWidth()); 
} 

public static void getArea(double length, double width) 
{ 
    System.out.println("The area of the Rectangle is: " + (length * width)); 
} 
Смежные вопросы