所以即時嘗試創建一個程序,讀取一個單詞,然後設置2個數字,指定矩形,(W,H)。當單詞是「區域」時,程序應計算並打印該區域。當單詞是「周長」時,計算並打印周長。當單詞「退出」時,退出程序,不讀取數字。如何讀取一個詞,然後在java中輸入?
輸出應該是這樣的:
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
對不起,我知道縮進不好。
對不起,我對你的輸入有點困惑。你可以給一個樣本輸入,然後輸出樣本嗎?謝謝。 - 即所有標記爲「這是輸出:」的文本標有「這是輸出:」,用戶輸入了什麼? – user2570465
你甚至試過了解你的程序,看看Scanner類的API嗎? http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html您的程序(我認爲您從某處複製)已經通過'nextInt()'讀取寬度和長度,以閱讀數字。如果你檢查掃描器的API,你會發現還有其他方法可以獲得其他類型,比如'next()'獲得一個String,然後你可以檢查它是否包含'area'。 – jbx
所以基本上輸入應該是 區1 2 3 4 和輸出應該是或者 周長1 2 3 4 和輸出應該是 14 – user2681595