code for calculations in android
I'm working with an application right now for our project in school. My application is about Resistor Color Code calculation. My codes are working, but in displaying the values, I used the value as string. My problem is I want to make my result value as 1.2K ohms, 1.5M ohm or 5.4M ohms, just like that. Because in my codes the result will display 1200 ohms, 1500K ohms or 5400K ohms. Help me Please. Thanks in advance for the help.
This is my code for a, b, c, d and value is the dis开发者_C百科play in EditText
.
calcu is a button.
calcu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//for first band
if (a=="Black")
a = " ";
if (a=="Brown")
a = "1";
if (a=="Red")
a = "2";
if (a=="Orange")
a = "3";
if (a=="Yellow")
a = "4";
if (a=="Green")
a = "5";
if (a=="Blue")
a = "6";
if (a=="Violet")
a = "7";
if (a=="Gray")
a = "8";
if (a=="White")
a = "9";
//for second band
if (b=="Black")
b = "0";
if (b=="Brown")
b = "1";
if (b=="Red")
b = "2";
if (b=="Orange")
b = "3";
if (b=="Yellow")
b = "4";
if (b=="Green")
b = "5";
if (b=="Blue")
b = "6";
if (b=="Violet")
b = "7";
if (b=="Gray")
b = "8";
if (b=="White")
b = "9";
//for multiplier
if (c=="Black")
c = " ";
if (c=="Brown")
c = "0";
if (c=="Red")
c = "00";
if (c=="Orange")
c = "000";
if (c=="Yellow")
c = "0000";
if (c=="Green")
c = "00000";
if (c=="Blue")
c = "000000";
if (c=="Violet")
c = "0000000";
if (c=="Gray")
c = "00000000";
if (c=="White")
c = "000000000";
//for Tolerance
if (d=="Brown")
d = "1";
if (d=="Red")
d = "2";
if (d=="Green")
d = "0.5";
if (d=="Blue")
d = "0.25";
if (d=="Violet")
d = "0.1";
if (d=="Gray")
d = "0.05";
if (d=="Gold")
d = "5";
if (d=="Silver")
d = "10";
Value.setText(a + b + c + "\u2126" + " " + "\u00B1" + d + "%" + " Tolerance");
int result = getTheResult();
String Result = "";
if(result > 0 && result < 1000) Result = "" + result + " Ohms";
else if(result >= 1000 && result < 1000000) Result = "" + (result / 1000) + "K Ohms";
else if (result >= 1000000) Result = "" + (result / 1000000) + "M Ohms";
else Result = "Invalid Value";
import javax.swing.JOptionPane;
public class Resistance {
String digit_band1_color;
String digit_band2_color;
String multiplier_band3_color;
String tolerance_band4_color;
int temp1,temp2,temp3;
double temp4;
double result;
public Resistance(String a,String b,String c,String d){
digit_band1_color=a;
digit_band2_color=b;
multiplier_band3_color=c;
tolerance_band4_color=d;
switch (digit_band1_color){
case "Black":
temp1=0;
break;
case "Brown":
temp1=1;
break;
case "Red":
temp1=2;
break;
case "Orange":
temp1=3;
break;
case "Yellow":
temp1=4;
break;
case "Green":
temp1=5;
break;
case "Blue":
temp1=6;
break;
case "Voilet":
temp1=7;
break;
case "Grey":
temp1=8;
break;
case "White":
temp1=9;
break;
}
switch (digit_band2_color){
case "Black":
temp2=0;
break;
case "Brown":
temp2=1;
break;
case "Red":
temp2=2;
break;
case "Orange":
temp2=3;
break;
case "Yellow":
temp2=4;
break;
case "Green":
temp2=5;
break;
case "Blue":
temp2=6;
break;
case "Voilet":
temp2=7;
break;
case "Grey":
temp2=8;
break;
case "White":
temp2=9;
break;
}
switch (multiplier_band3_color){
case "Black":
temp3=0;
break;
case "Brown":
temp3=1;
break;
case "Red":
temp3=2;
break;
case "Orange":
temp3=3;
break;
case "Yellow":
temp3=4;
break;
case "Green":
temp3=5;
break;
case "Blue":
temp3=6;
break;
case "Voilet":
temp3=7;
break;
case "Grey":
temp3=8;
break;
case "White":
temp3=9;
break;
}
switch (tolerance_band4_color){
case "Brown":
temp4=1;
break;
case "Red":
temp3=2;
break;
case "Orange":
temp4=0.05;
break;
case "Yellow":
temp4=0.02;
break;
case "Green":
temp4=0.5;
break;
case "Blue":
temp4=0.25;
break;
case "Voilet":
temp4=0.1;
break;
case "Grey":
temp4=0.01;
break;
case "Gold":
temp4=5;
break;
case "Silver":
temp4=10;
break;
}
result=Math.pow(10,temp3);
System.out.println("Resistance = "+temp1+temp2+result+"+-"+temp4+"%");
}
public static void main(String[] args) {
String a=JOptionPane.showInputDialog(null,"Please Enter Color in Proper Format like this Black,Brown etc");
String b=JOptionPane.showInputDialog(null,"Please Enter Color in Proper Format like this Black,Brown etc");
String c=JOptionPane.showInputDialog(null,"Please Enter Color in Proper Format like this Black,Brown etc");
String d=JOptionPane.showInputDialog(null,"Please Enter Color in Proper Format like this Black,Brown etc");
Resistance calculator=new Resistance(a,b,c,d);
}
}
精彩评论