我的代码没有错误,也没有结果

时间:2016-02-20 20:20:33

标签: java methods runtime-error

我是一个非常新的编码器,我遇到了一些困难。我通过输入四个值(x,y和x,y)输入两个点后,编写一个应该打印出区域和圆的半径的代码。没有错误,但是当我运行程序时,我没有得到任何关于半径和我要求的区域的结果。请看一看并告诉我你的想法。谢谢!

import java.util.Scanner;

/**
 *
 * @author Dominique
 */
public class AreaCircle {

    public static double distance(double x1, double y1, double x2, double y2) {
       double dx = x2 - x1;
       double dy = y2 - y1;
       double dsquared = dx * dx + dy * dy;
       double radius = Math.sqrt(dsquared);
       System.out.println("The radius of your circle is " + dsquared);
       return radius;
    }

    public static void areaCircle(double radius) {
       double circleArea = (double) Math.pow(radius, 2) * (double) Math.PI;
       System.out.println("The area of your circle is " + circleArea);
       return;
    }

    public static double distance(int x1, int x2, int y1, int y2) {
       distance(0.5, 0.6, 0.7, 0.8);
       areaCircle(0.8);
       return;
    }

    public static void main(String[] args) {
       System.out.println("Enter in the x of one of your points");
       Scanner kbdln = new Scanner(System.in);
       double pickOne = kbdln.nextDouble();
       System.out.println("Enter in the y of one of your points");
       double pickTwo = kbdln.nextDouble();
       System.out.println("Enter in the x of your second point");
       double pickThree = kbdln.nextDouble();
       System.out.println("Great! Lastly, enter in the y of your second point");
       double pickFour = kbdln.nextDouble();
    }
}

2 个答案:

答案 0 :(得分:1)

在您的方法中,没有其他调用,例如距离(..) areaCircle(..)

尝试将以下代码添加到您的主要内容

                int numTrans;
                char tempChar[1000];
                double tempHolder;
                string **tempDataHolder;

                cout<<"Input number of transactions to add: ";
                cin>>numTrans;


                tempDataHolder = new string*[numTrans]; //pointer array 
                for(i=0;i<numTrans;i++)
                tempDataHolder[i] = new string[col];

                cout<<"\nPlease input the following data as necessary: \n";
                for(ir=0; ir<numTrans;ir++)
                {
                    cout<<"Date Requested: "; //This may seem unnecessary but some companies require paper 
                                          //documentation aside from a system to approve petty cash
                                          //and sometimes the accounting staff has too much to do to 
                                          //perform data entry jobs in realtime such as this
                    getline(cin,tempDataHolder[ir][col]);
                    cout<<"Person Requesting Funds: ";
                    getline(cin,tempDataHolder[ir][col+1]);
                    cout<<"Person who approved request: ";
                    getline(cin,tempDataHolder[ir][col+2]);
                    cout<<"Amount Requested: Php. ";
                    cin>>tempHolder; //temp double number to properly format money
                    ostringstream convert; 
                    convert<<tempHolder;
                    tempDataHolder[ir][col+3] = convert.str();
                    cout<<"Particulars: ";
                    getline(cin,tempDataHolder[ir][col+4]);
                    tempDataHolder[ir][col+5] = " "; //initialized to empty space because 
                    tempDataHolder[ir][col+6] = " "; //data has not yet been retrieved 
                    tempDataHolder[ir][col+7] = " "; //through liquidation
                    tempDataHolder[ir][col+8] = " ";
                    tempDataHolder[ir][col+9] = "false";                    
                }
                tableSize = deterSize(curFileName) + (numTrans*col); //this will ensure the next table will house all new elements as well
                readCurFile(curFileName, tableSize);
                displayCurTable(tableSize);


                delete tempDataHolder;

计算结果并打印出来。

答案 1 :(得分:0)

在主块中,您没有编写任何代码来计算面积和半径。 在&#34;之后的Main(string [] args){}块中,双pickFour = kbdln.nextDouble();&#34; 写下代码:

double radius = distance(pickOne, pickTwo ,pickThree, pickFour);
double area = areaCircle(radius);

所以你的代码看起来像这样:

package areacircle;

import java.util.Scanner;

/**
 *
 * @author Dominique
 */
public class AreaCircle {

    public static double distance
 (double x1, double y1, double x2, double y2) {
 double dx = x2 - x1;
 double dy = y2 - y1;
 double dsquared = dx*dx + dy*dy;
 double radius = Math.sqrt (dsquared);
 System.out.println ("The radius of your circle is " + dsquared);
 return radius;
 } 
 public static void areaCircle(double radius){
 double circleArea=(double)Math.pow(radius,2)*(double) Math.PI;
 System.out.println("The area of your circle is "+circleArea);
return;
}
 public static double distance
         (int x1, int x2, int y1, int y2){


          distance(0.5, 0.6, 0.7, 0.8);
          areaCircle(0.8);
        return ;
         }


    public static void main(String[] args) {
       System.out.println("Enter in the x of one of your points");
         Scanner kbdln=new Scanner(System.in);
         double pickOne=kbdln.nextDouble();
         System.out.println("Enter in the y of one of your points");
         double pickTwo=kbdln.nextDouble();
         System.out.println("Enter in the x of your second point");
         double pickThree=kbdln.nextDouble();
         System.out.println("Great! Lastly, enter in the y of your second point");
         double pickFour=kbdln.nextDouble();
         //The added part
         double radius = distance(pickOne, pickTwo ,pickThree, pickFour);
         double area = areaCircle(radius);
    }

}
相关问题