-1

所以我试图创建一个程序来读取一个单词,然后设置 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

对不起,我知道缩进不好。

4

1 回答 1

0

您需要一次性向用户询问整个问题(即 {cmd} {x} {y} {width} {height}),然后解析此结果。

例如...

cmd = input.nextLine(); // Where cmd is a String
// example input...
// area 1 2 3 4
// perimeter 1 2 3 4
// quite

获得用户输入后,您需要解析此文本。现在您可以使用正则表达式来执行此操作,但最好保持简单;)

相反,您可以使用您已经知道的...

Scanner parser = new Scanner(cmd);
cmd = parser.next();
// Example output...
// area
// perimeter 
// quite

现在。一旦你有了cmd(或用户想要做什么),你需要开始做出关于......的决定

// First, we need to know if we can handle the question...
if ("area".equalsIgnoreCase(cmd) || "perimeter".equalsIgnoreCase(cmd)) {

然后就可以开始提取参数了...

int x = parser.nextInt();
int y = parser.nextInt();
int width = parser.nextInt();
int height = parser.nextInt();

现在,我可能还会parser.hasInt()用来检查参数是否存在,但这只是我......

然后,你需要做的就是把它粘在一个循环里......

do {
    // Get user input
    // Parse user input
    // Make some decisions...
} while (!"quite".equalsIgnoreCase(cmd));
于 2013-08-21T00:04:48.867 回答