public class OblongTester
{
public static void main(String [] args)
{
// declare variables
Oblong myOblong = new Oblong();
// use methods to set instance variables
myOblong.setHeight(10);
myOblong.setWidth(25);
// use methods to retrieve values of instance variables
System.out.println("Width: " + myOblong.getWidth());
System.out.println("Height: " + myOblong.getHeight());
System.out.println("Area: " + myOblong.calculateArea());
}
}
我需要的不是为setHeight和setWidth设置固定值而是希望用户能够输入高度和宽度,然后让区域获取高度和宽度的值将它们相乘并生成输出区域例如
输入高度:10 输入宽度:5 面积:50.0
希望这清楚我要问的是什么。上次我因为没有具体而对指关节进行了包装。
此致
标记
答案 0 :(得分:2)
使用Scanner
:
public class OblongTester
{
public static void main(String [] args)
{
// declare variables
Oblong myOblong = new Oblong();
// use methods to set instance variables
Scanner sc = new Scanner(System.in); //create a scanner
System.out.println("Enter the height: ");
int height = sc.nextInt(); //get height
System.out.println("Enter the width: ");
int width = sc.nextInt(); //get width
myOblong.setHeight(height);
myOblong.setWidth(width);
System.out.println();
// use methods to retrieve values of instance variables
System.out.println("Width: " + myOblong.getWidth());
System.out.println("Height: " + myOblong.getHeight());
System.out.println("Area: " + myOblong.calculateArea());
}
}