[英]Calculate the distance between two points
I tried the following code.but it gives two errors.I want calculate the distance between two points by formula line and display the result in the textView1 .我尝试了以下代码。但它给出了两个错误。我想通过公式线计算两点之间的距离并将结果显示在textView1 中。 I do not know Where did I make the mistake in the code?我不知道我在代码中哪里出错了?
Cal.java卡尔.java
import android.view.View;
import android.content.Context;
import java.lang.Math;
public class Cal extends View {
Cal(Context context){
super(context);
}
public double result;
double parameter = ((10-80)^2) + ((15-90)^2);
public void cal(){
result = Math.sqrt(parameter);
}
}
MainActivity.java主活动.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity{
Cal cal;
TextView textView;
public void onCreate(Bundle s){
super.onCreate(s);
setContentView(R.id.textView1);
cal = new Cal(this);
textView.setText(cal).;
}
}
Errors:错误:
Gradle: FAILURE: Build failed with an exception. Gradle:失败:构建失败,出现异常。
What went wrong: Execution failed for task ':Www:compileDebug'.出了什么问题:任务 ':Www:compileDebug' 执行失败。
Compilation failed;编译失败; see the compiler error output for details.有关详细信息,请参阅编译器错误输出。
Try: Run with --stacktrace option to get the stack trace.尝试:使用 --stacktrace 选项运行以获取堆栈跟踪。 Run with --info or --debug option to get more log output.使用 --info 或 --debug 选项运行以获得更多日志输出。
and和
Could not execute build using Gradle distribution ' http://services.gradle.org/distributions/gradle-1.6-bin.zip '.无法使用 Gradle 分发版“ http://services.gradle.org/distributions/gradle-1.6-bin.zip ”执行构建。
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity{
Cal cal;
TextView textView;
public void onCreate(Bundle s){
super.onCreate(s);
setContentView(R.layout.<your layout name>); // You can not set id of any view here
cal = new Cal(this); // This is a object
cal.cal();
textView.setText(""+ cal.result); // set the value instead of view object
}
}
Make the cal() method return double value:使 cal() 方法返回双精度值:
import android.view.View;
import android.content.Context;
import java.lang.Math;
public class Cal extends View {
Cal(Context context){
super(context);
}
public double result;
double parameter = ((10-80)^2) + ((15-90)^2);
public double cal(){
result = Math.sqrt(parameter);
return result;
}
}
Call the cal() method and display the result.调用 cal() 方法并显示结果。
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity{
Cal cal;
TextView textView;
public void onCreate(Bundle s){
super.onCreate(s);
setContentView(R.layout.<put your layout name here>);
cal = new Cal(this);
double res = cal.cal();
textView.setText(""+res);
}
}
The textview will be set to " NaN " as your parameter variable is not a number variable. textview 将设置为“ NaN ”,因为您的参数变量不是数字变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.