-1

そこで、単語を読み取り、次に長方形 (w, h) を指定する 2 つの数値のセットを読み取るプログラムを作成しようとしています。単語が「面積」の場合、プログラムは面積を計算して出力する必要があります。単語が「周囲」の場合は、周囲を計算して出力します。単語が「quit」の場合、数字を読まずにプログラムを終了します。

出力は次のようになります。

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 に答える