2013-08-20 20 views
-1

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

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

+0

对不起,我对你的输入有点困惑。你可以给一个样本输入,然后输出样本吗?谢谢。 - 即所有标记为“这是输出:”的文本标有“这是输出:”,用户输入了什么? – user2570465

+0

你甚至试过了解你的程序,看看Scanner类的API吗? http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html您的程序(我认为您从某处复制)已经通过'nextInt()'读取宽度和长度,以阅读数字。如果你检查扫描器的API,你会发现还有其他方法可以获得其他类型,比如'next()'获得一个String,然后你可以检查它是否包含'area'。 – jbx

+0

所以基本上输入应该是 区1 2 3 4 和输出应该是或者 周长1 2 3 4 和输出应该是 14 – user2681595

回答

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)); 
相关问题