i have fixed it now, but it still shows me 0 after i input my first numbers. it shows me correct on vikt but not in pris. :S
package Brev;
import static javax.swing.JOptionPane.*;
public class Uppgift1 {
public static void main(String[] arg) {
String indata = showInputDialog("Hur mycket väger ditt brev i gram?");
int vikt = Integer.parseInt(indata);
int pris = 0;
do {
indata = showInputDialog("Ditt porto kostar " + pris + " kr med vikten " + vikt + " Gram." +
"\nSkriv in en följande vikt för att addera på ditt nuvarande porto.");
vikt = vikt + Integer.parseInt(indata);
pris = pris + pris;
if (vikt < 1) {
showMessageDialog(null, "Error");
}
else if (vikt <= 50){
showMessageDialog(null, "Portot blir "+ (pris + 7) + "Kr. med " + vikt + " Gram.");
}
else if (vikt <= 100){
showMessageDialog(null, "Portot blir "+ (pris + 14) + "Kr. med " + vikt + " Gram.");
}
else if (vikt <= 250){
showMessageDialog(null, "Portot blir "+ (pris + 28) + "Kr. med " + vikt + " Gram.");
}
else if (vikt <= 500){
showMessageDialog(null, "Portot blir "+ (pris + 42) + "Kr. med " + vikt + " Gram.");
}
else if (vikt <= 1000){
showMessageDialog(null, "Portot blir "+ (pris + 56) + "Kr. med " + vikt + " Gram.");
}
else if (vikt <= 2000){
showMessageDialog(null, "Portot blir "+ (pris + 70) + "Kr. med " + vikt + " Gram.");
}
else if (vikt > 2000)
showMessageDialog(null, "Maximalvikten är 2000GRAM / 2KG");
} while (vikt <= 2000);
}
}
You're writing code that looks ambiguous:
"aString" + anInt + anotherInt + "anotherString"
Java is interpreting this to mean the equivalent of:
("aString" + anInt) + anotherInt + "anotherString"
so that each int
is converted to a String
before being added, but you wanted to do the equivalent of:
"aString" + (anInt + anotherInt) + "anotherString"
So, just write it in this last, unambiguous way. Put parentheses around your math formulas if they're part of a string addition.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.