如何在Java中进行多个计算并得到一个结果?

huangapple go评论71阅读模式
英文:

How can I do multiple calculations with one result in Java?

问题

我有3个EditBoxes,分别带有以下数值:跑道航向、风向和风速。我需要使用这些数值来计算。首先,我需要计算

侧风分量 = sin(风向 - 跑道航向) * 风速

但我不知道如何实现这个计算...

英文:

I have 3 EditBoxes with the respective values: Runway Heading Wind Direction Wind speed for that I have to use the formula with the respective values. First I have to calculate

Crosswind component = sin(wind direction - runway heading) * wind speed

But I dont know how to implement this...

答案1

得分: 0

EditText windDirectionEditText;
EditText runwayHeadingEditText;
EditText windSpeedEditText;
Button calculate;
TextView crossWindResultTextView;

public onCreate() {
   // 将视图分配给相应的字段
   windDirectionEditText = findViewById(R.id.wind_direction);
   runwayHeadingEditText = findViewById(R.id.runway_heading);
   windSpeedEditText = findViewById(R.id.wind_speed);
   calculate = findViewById(R.id.calculate_stuff);
   crossWindResultTextView = findViewById(R.id.crosswind_text);

   // 将点击监听器分配给按钮,以便在单击时计算结果并将其显示在结果文本视图中
   calculate.setOnClickListener(click -> {
       // 收集数值
       double windDirection = Double.valueOf(windDirectionEditText.getText().toString());
       double windSpeed = Double.valueOf(windSpeedEditText.getText().toString());
       double runwayHeading = Double.valueOf(runwayHeadingEditText.getText().toString());
       // 计算结果
       // 侧风分量 = sin(风向 - 跑道航向) * 风速
       double crosswind = Math.sin(Math.toRadians(windDirection - runwayHeading)) * windSpeed;

       // 将结果分配给文本视图
       crossWindResultTextView.setText(crosswind + " kts");
    });
}

请注意,我在代码中进行了一些修正,以确保计算结果正确。

英文:

Ok, i'm gonna try to help you with something off the bat here. But you'll probably need to adapt it a bit.

EditText windDirectionEditText;
EditText runwayHeadingEditText;
EditText windSpeedEditText;
Button calculate;
TextView crossWindResultTextView;

public onCreate() {
   //assign the views to their correponding fields
   windDirectionEditText = findViewById(R.id.wind_direction);
   runwayHeadingEditText = findViewById(R.id.runway_heading);
   windSpeedEditText = findViewById(R.id.wind_speed);
   calculate = findViewById(R.id.calculate_stuff);
   crossWindResultTextView = findViewById(R.id.crosswind_text);

   // assign the clicklistener to button so that when clicked, it calculates stuff and put it's result in the result text view
   calculate.setOnClickListener( click -> {
       // gather values
       double windDirection = Double.valueOf(windDirectionEditText.getText().toString());
       double windSpeed = Double.valueOf(windSpeedEditText.getText().toString());
       double runwayHeading = Double.valueOf(runwayHeadingEditText.getText().toString());
       // calculate stuff
       // Crosswind component = sin(wind direction - runway heading) * wind speed
       double crosswind = Math.sin(windDirection - runwayHeading) * windSpeed;

       // assign result to textview
       crossWindResultTextView.setText(crosswind + " kts");
    });
}

Now, you'll need a layout, you said you have 3 edit texts for the wind direction, runway heading and wind speed.

Put a textview somewhere in there, and a button.

Change this code so that their identifiers match the ones from the layout.

When you click the "calculate" button, the result textview will say whatever knots for the entered values.

Feel free to work from there and improve it as you need.

huangapple
  • 本文由 发表于 2020年7月29日 18:18:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63151363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定