【发布时间】:2013-08-20 23:48:41
【问题描述】:
所以我试图创建一个程序来读取一个单词,然后设置 2 个指定矩形的数字(w,h)。当单词是“面积”时,程序应该计算并打印面积。当单词是“周长”时,计算并打印周长。当单词为“退出”时,退出程序,不读取数字。
输出应如下所示:
java Rectangle1
? area 1 2 3 4
Area of (1.0, 2.0, 3.0, 4.0) = 12.0
? perimeter 1 2 3 4
Perimeter of (1.0, 2.0, 3.0, 4.0) = 14.0
? area 2 3 4 5
Area of (2.0, 3.0, 4.0, 5.0) = 20.0
? quit
$
我可以让程序先存储宽度和高度,然后读取面积或周长,所以它看起来像这样:
$ java Rectangle1
To find the area or perimeter of a rectangle
Enter the Length from 1 to 20 ( defaults to 1 ) :
1
Enter the Width from 1 to 20 (defaults to 1 ) :
2
Enter 1 to find Area
Enter 2 to find Perimeter
Enter 3 to quit
Choice:1
Area: 2.000000
Choice:2
Perimeter: 6.000000
Choice:3
但我不知道如何让它读取面积或周长,然后在同一行输入宽度和高度,然后打印出周长的任一面积作为答案,直到输入退出
这是我的代码:
import java.util.Scanner;
//I just put everything in one class, the Rectangle keeps its properties
//main was just added
//feel free to seperate as needbe
public class Rectangle1{
// length and width default to 1
private static double length;
private static double width;
public static double perimeter;
public static double area;
private static int choice;
//empty constructor
public Rectangle1(){
setLength(1);
setWidth(1);
}
// constructor with length and width supplied
public Rectangle1( double theLength, double theWidth) {
setLength(theLength );
setWidth( theWidth );
} // end Rectangle two-argument constructor
// validate and set length
public void setLength(double theLength){
length = ( theLength > 0.0 && theLength < 20.0 ? theLength : 1.0 );
} // end method setLength
// validate and set width
public void setWidth(double theWidth){
width = ( theWidth > 0.0 && theWidth <20.0 ? theWidth : 1.0);
}//end method setWidth
// get value of length
public double getLength(){
return length;
}//end method getLength
// get value of width
public double getWidth(){
return width;
}// end method getWidth
// calculate rectangle's perimeter
public static double calcPerimeter () {
perimeter = (length * 2) + (width * 2);//calculates area
System.out.printf("Perimeter: %f\n", perimeter);//output
return perimeter;
}
// calculate rectangle's area
public static double calcArea(){
area = (length * width);//calculates area
System.out.printf("Area: %f\n", area);//output
return area;
}
// convert to String
public String toString(){
return String.format("%s02d %02d", length, width);
}///end method toPerimeter String
public static void main( String args[] )
{
Scanner input = new Scanner (System.in);
Rectangle1 myRectangle = new Rectangle1(); //this is an object instance
int choice;
double width;
double length;
System.out.println("To find the area or perimeter of a rectangle" );
System.out.println ( "Enter the Length from 1 to 20 ( defaults to 1 ) : " );
length = input.nextInt();
System.out.println ( "Enter the Width from 1 to 20 (defaults to 1 ) : " );
width = input.nextInt();
//We need to now set these values to our rectangle object
myRectangle.setWidth(width);
myRectangle.setLength(length);
System.out.println ( "Enter 1 to find Area" );
System.out.println ( "Enter 2 to find Perimeter" );
System.out.println ( "Enter 3 to quit" );
System.out.printf ( "Choice:" );
choice = input.nextInt();
while ( choice != 3 ){
if (choice == 1){
System.out.printf( "", myRectangle.calcArea()); //call the method of our created object instance, NOT the class name
}
else if ( choice == 2){
System.out.printf( "", myRectangle.calcPerimeter());//call the method of our created object instance, NOT the class name
}
System.out.printf ( "Choice:" );
choice = input.nextInt();
}
}//end method main
} // end class Rectangle
对不起,我知道缩进不好。
【问题讨论】:
-
抱歉,我对您的输入内容有点困惑。你能给出一个样本输入,然后是一个样本输出吗?谢谢。 - 即您有一些标记为“这是输出:”的文本,在所有标记为“这是输出:”的文本中,用户输入了什么?
-
您是否尝试过了解您的程序并查看 Scanner 类的 API? docs.oracle.com/javase/7/docs/api/java/util/Scanner.html 您的程序(我假设您从某处复制)已经通过
nextInt()读取宽度和长度以读取数字。如果你查看Scanner的API,你会看到还有其他方法可以获取其他类型,例如next()获取一个String,然后你可以检查它是否包含area。 -
所以基本上输入应该是面积 1 2 3 4 输出应该是 12 或周长 1 2 3 4 输出应该是 14