package com.karanvir.tip;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import static com.karanvir.tip.R.drawable.p;
public class MainActivity extends AppCompatActivity {
public EditText people;
public EditText amount;
public EditText percentage;
public EditText finalAmount;
public String finalAmountz;
public RatingBar rating;
public TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
people=(EditText) findViewById(R.id.editText);
amount=(EditText) findViewById(R.id.editText2);
percentage=(EditText) findViewById(R.id.editText3);
finalAmount=(EditText) findViewById(R.id.editText6);
rating=(RatingBar) findViewById(R.id.ratingBar);
//ratingBar.setRating(3);
txt=(TextView) findViewById(R.id.textView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void calc(View view){
try{
double pep= Double.parseDouble(people.getText().toString());
double a=Double.parseDouble(amount.getText().toString());
double perc=Double.parseDouble(percentage.getText().toString());
double percc=Double.parseDouble(percentage.getText().toString());
if(pep>1||pep==1){
if(perc>1.0) {
double aa=a;
perc = perc / 100;
a = (a * perc);
double newamount=a+aa;
double placeholder = newamount / pep;
double rounder = Math.round(placeholder);
finalAmountz = "$" + String.valueOf(rounder);
finalAmount.setText(finalAmountz.toString());
if (perc>0.12) {
rating.setRating(5);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
else if (perc>0.10&&perc<0.12||perc==0.10) {
rating.setRating(4);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
else if (perc>0.7&&perc<0.10||perc==0.07) {
rating.setRating(3);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
else if (perc>0.05&&perc<0.07||perc==0.05) {
rating.setRating(2);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
if (perc<0.05) {
rating.setRating(1);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
}
}
else if (pep<1||pep==1){
Toast.makeText(MainActivity.this, "Please Enter 1 person minimum", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
}
&#13;
请不要在这里撕裂我。但我是Android开发的新手。我现在正在制作小费计算器应用程序,一切都按照我想要的方式运行。但是,我的星星计算已关闭。它一直显示1星?请看第72-117行。有人能帮我吗?对于noob问题我很抱歉,我还在学习。
package com.karanvir.tip;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import static com.karanvir.tip.R.drawable.p;
public class MainActivity extends AppCompatActivity {
public EditText people;
public EditText amount;
public EditText percentage;
public EditText finalAmount;
public String finalAmountz;
public RatingBar rating;
public TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
people=(EditText) findViewById(R.id.editText);
amount=(EditText) findViewById(R.id.editText2);
percentage=(EditText) findViewById(R.id.editText3);
finalAmount=(EditText) findViewById(R.id.editText6);
rating=(RatingBar) findViewById(R.id.ratingBar);
//ratingBar.setRating(3);
txt=(TextView) findViewById(R.id.textView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void calc(View view){
try{
double pep= Double.parseDouble(people.getText().toString());
double a=Double.parseDouble(amount.getText().toString());
double perc=Double.parseDouble(percentage.getText().toString());
if(pep>1||pep==1){
if(perc>1.0) {
double aa=a;
perc = perc / 100;
a = (a * perc);
double newamount=a+aa;
double placeholder = newamount / pep;
double rounder = Math.round(placeholder);
finalAmountz = "$" + String.valueOf(rounder);
finalAmount.setText(finalAmountz.toString());
if (perc>12) {
rating.setRating(5);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
if (perc>10&&perc<12||perc==10) {
rating.setRating(4);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
if (perc>7&&perc<10||perc==7) {
rating.setRating(3);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
if (perc>5&&perc<7||perc==5) {
rating.setRating(2);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
if (perc<5) {
rating.setRating(1);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
}
else if(perc<=1.0){
double aa=a;
a = (a * perc);
double newamount=a+aa;
double placeholder = newamount / pep;
double rounder = Math.round(placeholder);
finalAmountz = "$" + String.valueOf(rounder);
finalAmount.setText(finalAmountz.toString());
finalAmountz = "$" + String.valueOf(rounder);
finalAmount.setText(finalAmountz.toString());
if (perc>12)
rating.setRating(5);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
if (perc>10&&perc<12||perc==10)
rating.setRating(4);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
if (perc>7&&perc<10||perc==7)
rating.setRating(3);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
if (perc>5&&perc<7||perc==5)
rating.setRating(2);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
if (perc<5)
rating.setRating(1);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
}
else if (pep>1){
Toast.makeText(MainActivity.this, "Please Enter 1 person minamum", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
}
&#13;
答案 0 :(得分:1)
您将perc除以100
if(pep>1||pep==1){
if(perc>1.0) {
double aa=a;
perc = perc / 100;
....
除非您输入的值优于 500 ,否则其值将始终低于5(您的最后一个条件),因此总是给出1星