2017-06-03 205 views
1

用戶需要從提供選項中選擇一個選項。如果用戶選擇三角形,程序會提示用戶輸入頂點的座標並計算三角形的周長。我無法解決錯誤。使用方法計算三角形周長和圓周長

import java.util.*; 
public class Menu 
{ 
    public static void main (String[] args) 
    { 
     int userOption =0; 
     userOption = myMenu(); 
     System.out.println("User selected Option"+userOption); 

    if (userOption==1) 
    { 
     System.out.println("The perimetre of your triangle is" + getPerimeter(trianglePrompt())); 
    }  
    //else if (userOption==2) 
    //{ 
     //System.out.println("The circumference of your circle is" + circle(circlePrompt())); 
    //} 

    } 

    public static int myMenu(){ 
    int userOption; 
    Scanner myInput=new Scanner(System.in); 
    do { 
     System.out.println("Select one of the following options:"); 
     System.out.println(" 1. Triangle"); 
     System.out.println(" 2. Circle"); 
     System.out.println(" 3. Exit"); 
     userOption= myInput.nextInt(); 
    // To read a number of type float, use : myInput.nextFloat(); 
    // To read a character use : (myInput.next()).charAt(0); 
     if (userOption==3){ 
     System.out.println("Bye"); 
     System.exit(0); 
     } 

    } while (userOption !=1 && userOption !=2); 
     return userOption; 
    } 

//METHOD TO SCAN RADIUS FROM USER 
    public static double circlePrompt() 
    { 
     double radius; 
     Scanner myRadius= new Scanner(System.in); 
     do 
     { 
     System.out.println("Input a radius of a circle: "); 
     radius= myRadius.nextDouble(); 
     if(radius<0) 
      System.out.println("You must input a positive radius"); 
     } 
     while (radius<0); 
     return radius; 
    } 

//METHOD TO CALCULATE THE CIRCUMFERENCE 
    public static double circle(double radius) 
    {  
     return (2*radius*Math.PI); 
    } 
// METHOD TO SCAN 6 COORDINATES OF TRIANGLE 
    private Point p1, p2,p3; 
    public int trianglePrompt(int x1, int y1, int x2, int y2, int x3, int y3) 
    { 
    //double x1; 
    Scanner scanObject = new Scanner(System.in); 
    System.out.println("Input x1 of a Triangle : "); 
    x1= scanObject.nextInt(); 

System.out.println("Input x1 of a Triangle : "); 
    x2= scanObject.nextInt(); 

System.out.println("Input x1 of a Triangle : "); 
    x3= scanObject.nextInt(); 

System.out.println("Input x1 of a Triangle : "); 
    y1= scanObject.nextInt(); 

System.out.println("Input x1 of a Triangle : "); 
    y2= scanObject.nextInt(); 

System.out.println("Input x1 of a Triangle : "); 
    y3= scanObject.nextInt(); 
    //return x1; 

} 
    p1 = new Point(x1,y1); 
    p2 = new Point(x2,y2); 
    p3 = new Point(x3,y3); 
    public double getSideA() 
    { 
     double length = p1.distance(p2); 
     return length; 
    } 

    public double getSideB() 
    { 
    double length = p2.distance(p3); 
    return length; 
    } 

    public double getSideC() 
     { 
     double length = p3.distance(p1); 
     return length; 
     } 

//METHOD TO CALCULATE PERIMETRE OF TRIANGLE BY ADDING ALL THREE SIDES 
    public int getPerimeter() 
    { 
    int as = getSideA(), bs = getSideB(), cs = getSideC(); 
    return (as + bs + cs); 
    } 
} 

錯誤:

Menu.java:108: error: <identifier> expected 
p1 = new Point(x1,y1); 
^
Menu.java:109: error: <identifier> expected 
p2 = new Point(x2,y2); 
^
Menu.java:110: error: <identifier> expected 
p3 = new Point(x3,y3); 
^
3 errors 

回答

-1

P1 =新點(X1,Y1);

應該是

Point p1 = new Point(x1,y1);

+0

這將只替換當前的編譯錯誤。而且,如果你修復了第二個編譯錯誤,程序仍然無法工作。所以這個答案不是特別有用,因此我的downvote。 –

0

您已經初始化了任何方法之外的p1,p2p3。可以這樣做,但前提是你在聲明它們的同一個語句中初始化它們。 所以,你可以寫的

private Point p1 = new Point(x1, y1); 
private Point p2 = new Point(x2, y2); 
private Point p3 = new Point(x3, y3); 

代替

private Point p1, p2, p3; 

,然後刪除三行的錯誤。這將修復你的編譯錯誤,但它不會讓你的程序工作,因爲這意味着這些行將在值x1, y1, x2, y2, x3, y3具有用戶輸入的值之前運行。

你真正想要做的是移動線

p1 = new Point(x1, y1); 
p2 = new Point(x2, y2); 
p3 = new Point(x3, y3); 

等向上,以前}字符之前。這樣,他們將會在x1, y1, x2, y2, x3, y3設置的方法中,並且會在這些變量被賦予其值之後運行。