Jump to content

while loop errors

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
farhanyun91

farhanyun91

    Learning Programmer

  • Members
  • PipPipPip
  • 53 posts
please help me out with these errors.i am freaking out and pissed off!!!
i got no idea how to resolve the errors

import javax.swing.JOptionPane;

public class TouchLight

{

    String str

    String person;

    int PersonA=1;

    int PersonB=2;

    int personC=5;

    int personD=10;

    int duration;

    int Attempt;


    Attempt=1

    str=JOptionPane.showInputDialog("Please choose your person.");

    person=String.parseString(str);


    while(Attempt<5)

        {

            if(person=PersonA && person=PersonB)

                {

                    JOptionPane.showMessageDialog(null,"Duration is"+PersonB");

                    if(duration<17)

                    {

                        {

                            JOptionPane.showMessageDialog(null,"Congratulation!!!");

                        }

                        else

                        {

                            JOptionPane.showMessageDialog(null,"Try Again!!!");

                        }

                    }

                    if(person=PersonB && person=PersonC)

                        {

                            JOptionPane.showMessageDialog(null,"Duration is"+PersonC);

                            {

                                if(duration<17)

                                {

                                    JOptionPane.showMessageDialog(null,"Congratulation");

                                }

                                else

                                {

                                    JOptionPane.showMessageDialog(null,"Try Again!!!");

                                }

                            }

                        }

                            if(person=PersonC && person=PersonD)

                                {

                                        JOptionPane.showMessageDialog(null,"Duration is"+PersonD);

                                    {

                                        if(duration<17)

                                        {

                                            JOptionPane.showMessageDialog(null,"Congratulation!!!!");

                                        }

                                        else

                                        {

                                            JOptionPane.showMessageDialog(null,"Try Again!!!!");

                                        }

                                    }

                                }


                }

                Attempt++;

                str=JOptionPane.showInputDialog("Please choose your person.");

                person=String.parseString(str);

        }






#2
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
Well, I see some problems in the code...

  • You cannot initialize object properties in the class declaration unless they are global and declared as static
  • You must write all your code inside one or more methods of the class. You wrote it directly inside the class declaration
  • Comparison operator is == (= is assignment operator). This will be problematic in conditional tests
  • Attempt=1 needs a semicolon after it
Also there are some strange things in your code, because this condition:

if ((person==PersonA) && (person == PersonB))
(after changing = with == and placing parenthesis to be more clear)

Will never be true because person will never be equal simultaneously to two different values.