#pls help

29 messages · Page 1 of 1 (latest)

stark helm
#

class Main {
public static void main(String[] args) {
int num = 3;
int num1 = 10;
int res = 0;

    if(num<num1){
         for(num = num; num<=num1; num++){
        res = res + num;
        System.out.print(num);
        if(num!=num1){
            System.out.print("+");
        }
    } else {
         for(num = num; num<=num1; num--){
        res = res + num;
        System.out.print(num);
        if(num!=num1){
            System.out.print("+");
        }
    }

    }
    System.out.print("=");
    System.out.print(res);

}

}

Whats the error here? You know?

fleet basinBOT
#
class Main {
    public static void main(String[] args) {
        int num = 3;
        int num1 = 10;
        int res = 0;

        if(num<num1){
             for(num = num; num<=num1; num++){
            res = res + num;
            System.out.print(num);
            if(num!=num1){
                System.out.print("+");
            }
        } else {
             for(num = num; num<=num1; num--){
            res = res + num;
            System.out.print(num);
            if(num!=num1){
                System.out.print("+");
            }
        }

        }
        System.out.print("=");
        System.out.print(res);

    }
}

Whats the error here? You know?
kindred ember
#

What happens? What should happen? Why is what happens wrong?

stark helm
#

this is the error

kindred ember
#

check your (curley) brackets

#

every opening bracket needs to have a closing bracket and control structures (like if/else/for) apply to the block surrounded by curley brackets so make sure you are using (the right amount of) brackets at the right positions

stark helm
#

i check it a lot of time and i dont see tny problems?

#

you see where i need to add/delete bracket?

kindred ember
#

Where should the else belong to?

#

oh and using proper indentation can help you spot errors like that as well

stark helm
kindred ember
#

which if?

stark helm
#

the first if

kindred ember
#

ok

#

When you click on a bracket/move your cursor to a bracket, does your IDE show the matching other bracket?

stark helm
#

i think

#

@kindred ember no?

kindred ember
#

ok

#

let me take your code, reformat a bit and remove some stuff

stark helm
#

ok bro

#

i understand

#

class Main {
public static void main(String[] args) {
int num = 10;
int num1 = 3;
int res = 0;

    if(num<num1){
       for(num = num; num<=num1; num++){
        res = res + num;
        System.out.print(num);
        if(num!=num1){System.out.print("+");}}
    }
    
    else {
       for(num = num; num>=num1; num--){
        res = res + num;
        System.out.print(num);
        if(num!=num1){System.out.print("+");}}
        
       

     }
    System.out.print("=");
    System.out.print(res);

}

}

this is my new code and he worked

#

thank you so much!!!!

kindred ember
#
...
if(num<num1){
    for(num=num; num<=num1;num++){
        ...
        if(num!=num1){
            ...
        }
    } else {
        for(num=num; num<=num1; num--){
            ...
            if(num!=num1){
                ...
            }
        }
    }
    ...
kindred ember
stark helm
#

Thx

kindred ember
#

for preventing that in the future, make sure your code is formatted properly

#

your IDE might have autoformatting capabilities