c = new Console ();


        int lvl = 1;
        double exp = 0;
        int x = 1;
        double monsterhp;
        int monslvl = 1;
        int a = 1;
        while (a == 1)
        {
            int lvlup = 10 + (lvl * 40);
            if (exp >= lvlup)
            {
                lvl = lvl + 1;
                exp = 0;

            }
            double maxhp = 50 + (lvl * 5);
            double maxmp = 5 + (lvl);
            double hp = maxhp;
            double mp = maxmp;
            hp = Math.rint (hp);
            double hpbar;
            double mpbar;

            a = 0;
            double monsgen = Math.random () * 10;
            monsgen = Math.round (monsgen);
            String battle = ("battle");


            hpbar = maxhp / hp * 100;
            mpbar = maxmp / mp * 100;


            if (monsgen < 6)
            {


                monsterhp = 90 + (lvl * 10);

                while (battle.equals ("battle"))
                {


                    c.clear ();
                    c.setColor (Color.red);
                    c.fillRect (2, 0, hpbar, 2);
                    c.setColor (Color.blue);
                    c.fillRect (5, 0, mpbar, 2);
                    c.println ("Lvl: " + lvl);
                    c.setColor (Color.green);
                    c.fillRect (0, 5, )
                        c.print ("Your hp: ");
                    c.setTextColor (Color.red);
                    c.print (hp);
                    c.setTextColor (Color.black);
                    c.print ("                   Monster hp: ");

The misplaced construct problem occured at the red line. The string is defined and it should run the loop. Any help is appreciated. thanks

Sorry i posted this twice but once the internet wouldn't load the page and I forgot to check. This one is the more detailed one of the two. Thanks for helping.

red is where the problems are green will be the problem

// The "Rpg" class.                cheat
import java.awt.*;
import hsa.Console;

public class Rpg
{
    static Console c;           // The output console

    public static void main (String [] args)
    {
        c = new Console ();


        int lvl = 1;
        double exp = 0;
        int x = 1;
        double monsterhp;
        int monslvl = 1;
        int a = 1;

        while (a == 1)
        {
            int lvlup = 10 + (lvl * 40);
            if (exp >= lvlup)
            {
                lvl = lvl + 1;
                exp = 0;

            }
            double maxhp = 50 + (lvl * 5);
            double maxmp = 5 + (lvl);
            double hp = maxhp;
            double mp = maxmp;
            hp = Math.rint (hp);
            double hpbar;
            double mpbar;

            a = 0;
            double monsgen = Math.random () * 10;
            monsgen = Math.round (monsgen);
            String battle = ("battle");


            hpbar = maxhp / hp * 100;
            mpbar = maxmp / mp * 100;


            if (monsgen < 6)
            {


                monsterhp = 90 + (lvl * 10);

                while (battle.equals ("battle")) // misplaced construct
                {


                    c.clear ();
                    c.setColor (Color.red);
                    c.fillRect (2, 0, hpbar, 2);
                    c.setColor (Color.blue);
                    c.fillRect (5, 0, mpbar, 2);
                    c.println ("Lvl: " + lvl);
                    c.setColor (Color.green);
                    c.fillRect (0, 5, )
                        c.print ("Your hp: ");
                    c.setTextColor (Color.red);
                    c.print (hp);
                    c.setTextColor (Color.black);
                    c.print ("                   Monster hp: ");
                    c.setTextColor (Color.red);
                    c.println (monsterhp);
                    c.setTextColor (Color.black);
                    c.print ("Your mp: ");
                    c.setTextColor (Color.blue);
                    c.println (mp);
                    c.setTextColor (Color.black);
                    c.print (exp + " / " + lvlup);
                    c.println ();
                    c.println ();
                    c.print ("Would you like to attack, defend, use magic, or potion? ");
                    String move = c.readLine ();

                    if (move.equals ("attack"))
                    {
                        // you att monster
                        double att = Math.random () * 10 + (lvl * 2);
                        att = Math.rint (att);
                        monsterhp = monsterhp - att;
                        // monster att you
                        double monsatt = Math.random () * 10;
                        monsatt = Math.rint (monsatt) + (lvl / 2);
                        monsatt = Math.rint (monsatt);
                        hp = hp - monsatt;
                        mp = mp + 1;
                        if (mp > maxmp)
                        {
                            double mpdif = mp - maxmp;
                            mp = mp - mpdif;
                        }



                    }
                    else if (move.equals ("defend"))
                    {
                        double monsatt = Math.random () * 10;
                        monsatt = Math.rint (monsatt);
                        monsatt = (monsatt / 2);
                        monsatt = Math.rint (monsatt) + (lvl / 2);
                        monsatt = Math.rint (monsatt);
                        hp = hp - monsatt;



                    }
                    else if (move.equals ("magic"))
                    { // you attack

                        if (mp > maxmp)
                        {
                            double mpdif = mp - maxmp;
                            mp = mp - mpdif;
                        }
                        else if (move.equals ("magic"))
                        { // you attack

                            if (mp > maxmp)
                            {
                                double mpdif = mp - maxmp;
                                mp = mp - mpdif;
                            }
                            if (mp >= 2)
                            {
                                double att = Math.random () * 15 + (lvl * 2);
                                att = Math.rint (att);
                                monsterhp = monsterhp - att;
                                // monster attacks
                                double monsatt = Math.random () * 10;
                                monsatt = Math.rint (monsatt) + (lvl / 2);
                                monsatt = Math.rint (monsatt);
                                hp = hp - monsatt;
                                mp = mp - 3;
                                mp = mp + 1;
                            }
                        }
                    }
                    else if (move.equals ("potion"))
                    {
                        hp = hp + (maxhp * 0.17);
                        if (hp > maxhp)
                        {
                            hp = maxhp;
                        }
                        double monsatt = Math.random () * 10;
                        monsatt = Math.rint (monsatt) + (lvl / 2);
                        monsatt = Math.rint (monsatt);
                        hp = hp - monsatt;
                        mp = mp + 1;
                        if (mp > maxmp)
                        {
                            double mpdif = mp - maxmp;
                            mp = mp - mpdif;
                        }
                    }
                    else if (move.equals ("cheat"))
                    {
                        monsterhp = monsterhp - monsterhp;
                    }
                    if (monsterhp <= 0)
                    {
                        exp = exp + lvl * 5;
                        battle = ("");
                        a = 1;
                        x = x + 1;
                    }
                    else if (hp <= 0)
                    {
                        c.clear ();
                        c.println ("You have died.");
                        c.print ("press anything to close.");
                        String close = c.readLine ();
                        if (close.equals ("close"))
                        {
                            c.close ();
                        }
                        else
                            c.close ();
                    }
                    else if (move.equals ("lvlupcheat"))
                    {
                        lvl = lvl + 9999;
                        c.clear ();
                        c.println ("Lvl: " + lvl);
                        c.print ("Your hp: ");
                        c.setTextColor (Color.red);
                        c.print (hp);
                        c.setTextColor (Color.black);
                        c.print ("                   Monster hp: ");
                        c.setTextColor (Color.red);
                        c.println (monsterhp);
                        c.setTextColor (Color.black);
                        c.print ("Your mp: ");
                        c.setTextColor (Color.blue);
                        c.println (mp);
                        c.setTextColor (Color.black);
                        c.print (exp + " / " + lvlup);
                    }






                }
                else if (monsgen > 5)
                {

                    if (exp >= lvlup)
                    {
                        lvl = lvl + 1;
                        exp = 0;

                    }

                    monsterhp = 40 + (lvl * 10);
                    while (battle.equals ("battle"))
                    {

                        c.clear ();
                        c.println ("Lvl: " + lvl);
                        c.print ("Your hp: ");
                        c.setTextColor (Color.red);
                        c.print (hp);
                        c.setTextColor (Color.black);
                        c.print ("                   Monster hp: ");
                        c.setTextColor (Color.red);
                        c.println (monsterhp);
                        c.setTextColor (Color.black);
                        c.print ("Your mp: ");
                        c.setTextColor (Color.blue);
                        c.println (mp);
                        c.setTextColor (Color.black);
                        c.print (exp + " / " + lvlup);
                        c.println ();
                        c.println ();
                        c.print ("Would you like to attack, defend, use magic, or potion? ");
                        String move = c.readLine ();

                        if (move.equals ("attack"))
                        {
                            // you att monster
                            double att = Math.random () * 10 + (lvl * 2);
                            att = Math.rint (att);
                            monsterhp = monsterhp - att;
                            // monster att you
                            double monsatt = Math.random () * 10;
                            monsatt = Math.rint (monsatt) + (lvl / 2);
                            monsatt = Math.rint (monsatt);
                            hp = hp - monsatt;
                            mp = mp + 1;
                            if (mp > maxmp)
                            {
                                double mpdif = mp - maxmp;
                                mp = mp - mpdif;
                            }



                        }
                        else if (move.equals ("defend"))
                        {
                            double monsatt = Math.random () * 10;
                            monsatt = Math.rint (monsatt);
                            monsatt = (monsatt / 2);
                            monsatt = Math.rint (monsatt) + (lvl / 2);
                            monsatt = Math.rint (monsatt);
                            hp = hp - monsatt;



                        }
                        else if (move.equals ("magic"))
                        { // you attack
                            double att = Math.random () * 15 + (lvl * 2);
                            att = Math.rint (att);
                            monsterhp = monsterhp - att;
                            // monster attacks
                            double monsatt = Math.random () * 10;
                            monsatt = Math.rint (monsatt) + (lvl / 2);
                            monsatt = Math.rint (monsatt);
                            hp = hp - monsatt;
                            mp = mp - 3;
                            mp = mp + 1;
                            if (mp > maxmp)
                            {
                                double mpdif = mp - maxmp;
                                mp = mp - mpdif;
                            }

                        }
                        else if (move.equals ("cheat"))
                        {
                            monsterhp = monsterhp - monsterhp;
                        }
                        if (monsterhp <= 0)
                        {
                            exp = exp + lvl * 3;
                            battle = ("");
                            a = 1;
                            x = x + 1;
                        }
                        else if (move.equals ("potion"))
                        {
                            hp = hp + (maxhp * 0.17);
                            if (hp > maxhp)
                            {
                                hp = maxhp;
                            }
                            double monsatt = Math.random () * 10;
                            monsatt = Math.rint (monsatt) + (lvl / 2);
                            monsatt = Math.rint (monsatt);
                            hp = hp - monsatt;
                            mp = mp + 1;
                            if (mp > maxmp)
                            {
                                double mpdif = mp - maxmp;
                                mp = mp - mpdif;
                            }
                            else if (hp <= 0)
                            {
                                c.clear ();
                                c.println ("You have died.");
                                c.print ("press anything to close.");
                                String close = c.readLine ();
                                if (close.equals ("close"))
                                {
                                    c.close ();
                                }
                                else
                                    c.close ();
                            }

                        }
                        else if (move.equals ("lvlupcheat"))
                        {
                            lvl = lvl + 9999;
                            c.clear ();
                            c.println ("Lvl: " + lvl);
                            c.print ("Your hp: ");
                            c.setTextColor (Color.red);
                            c.print (hp);
                            c.setTextColor (Color.black);
                            c.print ("                   Monster hp: ");
                            c.setTextColor (Color.red);
                            c.println (monsterhp);
                            c.setTextColor (Color.black);
                            c.print ("Your mp: ");
                            c.setTextColor (Color.blue);
                            c.println (mp);
                            c.setTextColor (Color.black);
                            c.print (exp + " / " + lvlup);
                        }
                    }

                }

            }


















           
        } // main method
    } // Rpg class

thanks for any help

You have a problem with your brackets. After removing some of the code inside the ifs and the whiles, you have something like this:

while () {

} else if () {

}

Which is wrong. Try from the start and this time try to open-close, and then write the code in between.

That kind of worked I changed the

while () {
}
} // put in extra bracket then it gives me an error with the else if
// it dissapears if Put in an if now it looks like this

while (){
}
}
if (mosgen > 5){
}

I still get the while error and I need to change the last } to a ; then I fix one of the two error, but the while still causes an error.

Thanks for the help

You need to look again the whole code: This is wrong, as you know

while () {

}
} // this is an extra bracket

This would work:

if () {
  while () {

  }
}

You need to look the code. Better begin with a new class, and start adding slowly code to it from the previous one, and compile regularly in order to find what you added was wrong.

You need to look again the whole code: This is wrong, as you know

while () {

}
} // this is an extra bracket

This would work:

if () {
  while () {

  }
}

You need to look the code. Better begin with a new class, and start adding slowly code to it from the previous one, and compile regularly in order to find what you added was wrong.

I fixed the error using the new class and copy and paste method. Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.