I am trying to figure out why I get the Error in Title when adding if-Statements to my program. Without the if-statements everything works just fine. I hope someone can help me out :D Greetings
String? btmRechneranzeige(double? menge, double? prozent, String? btmittel,) {
// Add your function code here!
var m = menge;
var p = prozent;
double mengebtm = 0;
if (m != 0 && p != 0) {
mengebtm = m! * (p! / 100);
}
var result = mengebtm.toString();
String stoff = "";
if(btmittel == null){
return "...";
}
if (btmittel == "Amphetamin" && mengebtm >= 10) {
stoff = "Amphetaminbase übersteigt die nicht-geringe Menge";
} else {
if (btmittel == "Amphetamin" && mengebtm < 10) {
stoff = "Amphetaminbase übersteigt die nicht-geringe Menge nicht";
} else {return null;}
String output =
"Bei einer Menge von $result gramm handelt es sich um eine geringe Menge!";
//String result = nonono;
return output;
}
CodePudding user response:
The brackets don't match up right, making paths that don't return anything. I suspect you want to change the line that says
} else {
to
} else
Although other things seem wrong as well. Like you don't even use the result of stoff
CodePudding user response:
String? btmRechneranzeige1(double? menge, double? prozent, String? btmittel) {
var m = menge;
var p = prozent;
String stoff = "";
double mengebtm = 0;
if (m != 0 && p != 0) {
mengebtm = m! * (p! / 100);
}
var result = mengebtm.toString();
if (btmittel == null) {
return "...";
} else if (btmittel == "Amphetamin" && mengebtm >= 10) {
stoff = "Amphetaminbase übersteigt die nicht-geringe Menge";
} else if (btmittel == "Amphetamin" && mengebtm < 10) {
stoff = "Amphetaminbase übersteigt die nicht-geringe Menge nicht";
String output = "Bei einer Menge von $result gramm handelt es sich um eine geringe Menge!";
return output;
}
return null; // this line optional
}
CodePudding user response:
You are missing a return at this if-statement "btmittel == "Amphetamin" && mengebtm >= 10"
So this would work:
String? btmRechneranzeige(double? menge, double? prozent, String? btmittel,) {
// Add your function code here!
var m = menge;
var p = prozent;
double mengebtm = 0;
if (m != 0 && p != 0) {
mengebtm = m! * (p! / 100);
}
var result = mengebtm.toString();
String stoff = "";
if(btmittel == null){
return "...";
}
if (btmittel == "Amphetamin" && mengebtm >= 10) {
stoff = "Amphetaminbase übersteigt die nicht-geringe Menge";
return "some text here..." // -> This is the added line!!
} else {
if (btmittel == "Amphetamin" && mengebtm < 10) {
stoff = "Amphetaminbase übersteigt die nicht-geringe Menge nicht";
} else {return null;}
String output =
"Bei einer Menge von $result gramm handelt es sich um eine geringe Menge!";
//String result = nonono;
return output;
}
} // You were also missing this bracket :D
When returning an instance of something make sure you add a return value (null is also a value) for each path, so every if (else) branch.