Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
This is my code:
if(weight<=5)
cost=(2.8*weight);
if(weight<=20)
cost=(2.8*5)+(5.2*(weight-5));
if(weight<=50)
cost=(2.8*5)+(5.2*15)+(7*(weight-20));
if(weight>50)
cost=(2.8*5)+(5.2*15)+(7*30)+(8.6*(weight-50));
when weight is lesser than 6.9, why is the answer all negative?
when you write an IF nested statement, you must use the ELSE statement to prevent your scenario.
The right code:
if(weight<=5) {
cost=(2.8*weight);
} else if(weight<=20) {
cost=(2.8*5)+(5.2*(weight-5));
} else if(weight<=50) {
cost=(2.8*5)+(5.2*15)+(7*(weight-20));
} else if(weight>50) {
cost=(2.8*5)+(5.2*15)+(7*30)+(8.6*(weight-50));
}
In alternative, you must defined better your ranges, as follow:
if(weight<=5) {
cost=(2.8*weight);
} if(weight > 5 && weight<=20) {
cost=(2.8*5)+(5.2*(weight-5));
} if(weight > 20 && weight<=50) {
cost=(2.8*5)+(5.2*15)+(7*(weight-20));
} if(weight>50) {
cost=(2.8*5)+(5.2*15)+(7*30)+(8.6*(weight-50));
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
static final int WIN = 3;
static final int TIE = 1;
static final int LOSS = 0;
public static int berekenWedstrijdPunten(int[] mpUserTeamPunten, int[] mpTegenstanderTeamPunten){
if (mpUserTeamPunten > mpTegenstanderTeamPunten){
return WIN;
}
if (mpUserTeamPunten == mpTegenstanderTeamPunten){
return TIE;
}
if (mpUserTeamPunten < mpTegenstanderTeamPunten) {
return LOSS;
}
}
getting error lines under the if statements with > & <, trying to get a return of the win/tie/loss point into a print statement.
You can't compare two arrays with logical operators.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Why I try to use the loop to display the numbers I had put into the ArrayList, it says Unreachable code what am I doing wrong.
private void nOther1() {
ArrayList<Integer> multiples = new ArrayList<Integer>();
int n = 1;
while (1 <= 100) {
multiples.add(n);
n++;
}
// UNREACHABLE CODE
for (int num : multiples) {
System.out.println(num);
}
}
Because
while (1 <= 100)
will never terminate since 1 is always <= 100, and the compiler knows that.
See Chapter 14.21. Unreachable Statements for detailed explanation.
This condition while(1 <= 100){ is always returning true
it would be the same as you do while(true){ so all behind that point is code that NEVER will be executed..
I think you wanted trying instead:
while(n <= 100){
multiples.add(n);
n++;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
https://projecteuler.net/problem=4
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
Here is my code and I find the answer as 580085 but it is not the correct answer:
public class asdas {
public static void main(String[] args) {
int a=100,b=100,answer=0;
while(a<=999)
{
b=100;
while(b<=999)
{
int product=a*b;
int reverse=0;
while(product>0)
{
int lastDigit=product%10;
reverse=(reverse*10)+lastDigit;
product=product/10;
}
product=a*b;
if(product==reverse)
{
answer=product;
}
b++;
}
a++;
}
System.out.println(answer);
}
}
The problem is here:
if(product==reverse)
{
answer=product;
}
You're assuming that if you find a palindrome, it's the largest. That's not true though.
For example, 101*100 is calculated after 100*102, even though the latter is larger. In fact, right after calculating 998*999 (997,002), you calculate 999*100 (99,900), which is nearly 10 times smaller!
You should add another check to make sure the new product is greater:
if(product==reverse && product>answer) {
answer = product;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Eclipse is giving me the error "The left-hand side must be a variable" at this part of my code:
else
{ for(int i=0; i>=cellphoneArr.length; i++)
{if (cell_1.equals2(cellphoneArr[i]))
System.out.println(cellphoneArr[i]);
else
(cell_1.equals3(cellphoneArr[i])); ---> this is the error
System.out.println(cellphoneArr[i]);
}
The method equals3 is the following:
public boolean equals3(Cellphone phone)
{ if (brand.equals(phone));
}
I've been trying to figure this one out, but the way I invoked my two other methods equals 1 and 2 both worked with the object cell_1.
Try it as:
else
{ for(int i=0; i>=cellphoneArr.length; i++)
{if (cell_1.equals2(cellphoneArr[i]))
System.out.println(cellphoneArr[i]);
else if(cell_1.equals3(cellphoneArr[i]))
System.out.println(cellphoneArr[i]);
}
and the method equals3 must return a boolean values as:
public boolean equals3(Cellphone phone)
{ if (brand.equals(phone))
return true;
else
return false;
}
Remove ; at the end of else, Just like you did for if.
else(cell_1.equals3(cellphoneArr[i]));
^
Return a Boolean value from you function, instead of if
public boolean equals3(Cellphone phone)
{
return (brand.equals(phone));
}
You need to add an if:
else if (cell_1.equals3(cellphoneArr[i]))
You need an if following the else
EDIT
The code should be
if (cell_1.equals2(cellphoneArr[i])) {
System.out.println(cellphoneArr[i]);
} else if (cell_1.equals3(cellphoneArr[i])) {
System.out.println(cellphoneArr[i]);
}
Formatting allows you to pin point these kind of errors easily. If you are using Eclipse, do ctrl + shift + f
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
if(check !=0){
if (wee[i]>wee[i+1])
{
System.out.println("False");
check = 0;
else()
System.out.println("True!");
}
}
The Code snippet contains a nested if loop. The second set keeps returning "else without if" and i don't understand why. I've tried it with and without the parenthesis.
to be more clear: Why is the compiler returning an "else" without "if" error.
Change what you have to this:
if(check !=0){
if (wee[i]>wee[i+1])
{
System.out.println("False");
check = 0;
}
else
{
System.out.println("True!");
}
}
You were missing the closing paranthesis for the inner if statement.
Try:
if(check !=0)
{
if (wee[i]>wee[i+1])
{
System.out.println("False");
check = 0;
}
else
{
System.out.println("True!");
}
}
Or for fun:
System.out.println(wee[i] > wee[i+1] ? "False" : "True");