我正在尝试建立一个单位为1到1000或以上的结算应用程序,有一定的条件来计算要支付的总金额。如果客户消费1到20个单位,他将收取卢比。 30(最低费用)+卢比。每单位3,意味着如果他消耗12个单位他必须支付Rs(30 + 12 * 3)= 66卢比,如果他消耗超过20个单位还有另一个条件,最低费用将是卢比。 110和每单位费用将是7卢比。现在,如果消费者消费25个单位,那么他必须支付卢比(110+(25-20)* 7)=卢比145.我完全搞不清楚从哪里开始。我已经制作了一个布局文件和java文件。如果有人帮助我,我将感激不尽。
activity_main.xml中
package com.example.android.nepalelectricity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText input;
Button calculate;
Button clear;
Button credits;
Button exit;
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input=new EditText(this);
calculate = new Button(this);
result=new TextView(this);
clear=new Button(this);
credits = new Button(this);
exit = new Button(this);
input=(EditText)findViewById(R.id.input);
credits = (Button) findViewById(R.id.credits);
clear = (Button) findViewById(R.id.clear);
exit = (Button) findViewById(R.id.exit);
calculate = (Button) findViewById(R.id.calculate);
result=(TextView)findViewById(R.id.result);
calculate.setOnClickListener(new View.OnClickListener() {
// Perform action on Calculate Button click
@Override
public void onClick(View v) {
// i am confused in this section
/* if (input.getText().length()==0){
result.setText(R.string.err_msg);
}
else {
result.setText("You are great");
//change the integer value into string
result.setText("");
int input1 = Integer.parseInt(input.getText().toString());
} */
//Till here
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
input.setText("");
result.setText("");
}
});
credits.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Credits.class));
}
});
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Thank you for using app!", Toast.LENGTH_SHORT).show();
finish();
}
});
}
}
MainActivity.java
Header set Access-Control-Allow-Origin: http://www.somedomain.com
答案 0 :(得分:1)
使用如下逻辑:
calculate.setOnClickListener(new View.OnClickListener() {
// Perform action on Calculate Button click
@Override
public void onClick(View v) {
String value = input.getText().toString(); // if error occur here try this : String.valueOf(input.getText().toString());
if(TextUtils.isEmpty(value)){
//make toast saying value is null/empty
}
else{
final int inputValue = Integer.parseInt(value);
if(inputValue > 1 && inputValue <= 20){
result.setText(Integer.toString((inputValue*3)+30)); //try avoiding brackets in calculation if error occur, computer does not understand BODMAS calculation format.
}
else if(inputvalue > 20){
result.setText(Integer.toString(((inputValue-20)*7)+110)); //try avoiding brackets in calculation if error occur, computer does not understand BODMAS calculation format.
}
else{
// make toast saying value not in range
}
}
}
});