如果语句创建语法错误 - android

时间:2014-05-13 10:33:20

标签: android if-statement syntax-error

这是一个简单的问题,但我只是不明白什么是错的。我正在做一个Android应用程序,我希望屏幕在int命中一定数量时更改。

这是我的If语句

if (end=5)
{

         Intent i = new Intent(Quiz.this, Explain1.class);
         startActivity(i);
         finish();
}

应用程序的其余部分工作正常,但此语句在其之前和之后创建语法错误。

这是全班。目前的答案还没有解决这个问题

public class Quiz extends Activity  
{
MediaPlayer mysound;
boolean loaded=false;
private SoundPool soundPool,soundPool2;
int soundID, soundID2;
String Ans;
TextView ans1,ans2,ans3,ans4,ans5,ans6,ans7,ans8;
TextView Q1,Q2,Q3,Q4,Q5;
int q1=0, q2=0, q3=0, q4=0, q5=0;
int end = q1+q2+q3+q4+q5;
if (end.equals(5))
{

         Intent i = new Intent(Quiz.this, Explain1.class);
         startActivity(i);
         finish();
};
@Override
protected void onCreate(Bundle savedInstanceState) 
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz);
    ans1 = (TextView) findViewById(R.id.two4);
    ans2 = (TextView) findViewById(R.id.one6);
    ans3 = (TextView) findViewById(R.id.three2);
    ans4 = (TextView) findViewById(R.id.four0);
    ans5 = (TextView) findViewById(R.id.four8);
    ans6 = (TextView) findViewById(R.id.six4);
    ans7 = (TextView) findViewById(R.id.seven2);
    ans8 = (TextView) findViewById(R.id.eight);
    Q1 =(TextView) findViewById(R.id.ans1);
    Q2 =(TextView) findViewById(R.id.ans4);
    Q3 =(TextView) findViewById(R.id.ans3);
    Q4 =(TextView) findViewById(R.id.ans2);
    Q5 =(TextView) findViewById(R.id.ans5);
    Q1.setOnDragListener(dragListener);
    Q2.setOnDragListener(dragListener2);
    Q3.setOnDragListener(dragListener3);
    Q4.setOnDragListener(dragListener4);
    Q5.setOnDragListener(dragListener5);
    ans1.setOnTouchListener(listen);
    ans2.setOnTouchListener(listen);
    ans3.setOnTouchListener(listen);
    ans4.setOnTouchListener(listen);
    ans5.setOnTouchListener(listen);
    ans6.setOnTouchListener(listen);
    ans7.setOnTouchListener(listen);
    ans8.setOnTouchListener(listen);
    soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);

    soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
           @Override
           public void onLoadComplete(SoundPool soundPool, int sampleId,
                   int status) {
               loaded = true;
           }
       });
       soundID = soundPool.load(this, R.raw.correct, 1);

       soundPool2 = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);

       soundPool2.setOnLoadCompleteListener(new OnLoadCompleteListener() {
              @Override
              public void onLoadComplete(SoundPool soundPool, int sampleId,
                      int status) {
                  loaded = true;
              }
          });

          soundID2 = soundPool2.load(this, R.raw.error, 1);
 }


OnTouchListener listen =(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent arg1) {
        TextView button = (TextView) v;         
        DragShadow dragShadow = new DragShadow(v);

        ClipData data = ClipData.newPlainText("", "");
        v.startDrag(data, dragShadow, button, 0);

        return false;
    }
});

OnLongClickListener longlisten = new OnLongClickListener()
{       
    @Override
    public boolean onLongClick(View v)
    {
        TextView button = (TextView) v;         
        DragShadow dragShadow = new DragShadow(v);

        ClipData data = ClipData.newPlainText("", "");
        v.startDrag(data, dragShadow, button, 0);

        return false;
    }

};

public void cont(View v) 
{
    TextView button = (TextView) v;         
    DragShadow dragShadow = new DragShadow(v);

    ClipData data = ClipData.newPlainText("", "");
    v.startDrag(data, dragShadow, button, 0);
}


private class DragShadow extends View.DragShadowBuilder
{
    private ColorDrawable grey;


    public DragShadow(View view) {
        super(view);
        grey = new ColorDrawable(Color.LTGRAY);
    }
    @Override
    public void onDrawShadow(Canvas canvas) {
        grey.draw(canvas);
    }

    @Override
    public void onProvideShadowMetrics(Point shadowSize,Point shadowTouchPoint) 
    {
        View v = getView();
        int height =(int) v.getHeight();
        int width = (int) v.getWidth();
        grey.setBounds(0,0, width, height);
         shadowSize.set(width, height);
         shadowTouchPoint.set((int)width/2, (int)height/2);

    }
};

OnDragListener dragListener = new OnDragListener()
{
    @Override
    public boolean onDrag(View v, DragEvent event)
    {
            int dragEvent = event.getAction();
            TextView dropText = (TextView) v;

            switch(dragEvent)
            {
                case DragEvent.ACTION_DRAG_ENTERED:

                break;

                case DragEvent.ACTION_DRAG_EXITED:
                    break;

                case DragEvent.ACTION_DROP:
                    TextView draggedText = (TextView)event.getLocalState();
                    Ans = (String) (draggedText.getText());
                    dropText.setText(draggedText.getText());
                    if ( Ans.equals("16"))
                    {

                            AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
                            float actualVolume = (float) audioManager
                                      .getStreamVolume(AudioManager.STREAM_MUSIC);
                            float maxVolume = (float) audioManager
                                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                            float volume = actualVolume / maxVolume;

                                soundPool.play(soundID, volume, volume, 1, 0, 1f);
                                Log.e("Test", "Played sound");
                                q1=1;

                    }
                     else
                          {
                              AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
                                float actualVolume = (float) audioManager
                                              .getStreamVolume(AudioManager.STREAM_MUSIC);
                                float maxVolume = (float) audioManager
                                .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                                float volume = actualVolume / maxVolume;

                                    soundPool2.play(soundID2, volume, volume, 1, 0, 1f);
                                    Log.e("Test", "Played sound");
                                    q1=0;
                          }
                    break;
            }

            return true;
    }
};
OnDragListener dragListener2 = new OnDragListener()
{
    @Override
    public boolean onDrag(View v, DragEvent event)
    {
            int dragEvent = event.getAction();
            TextView dropText = (TextView) v;

            switch(dragEvent)
            {
                case DragEvent.ACTION_DRAG_ENTERED:

                break;

                case DragEvent.ACTION_DRAG_EXITED:
                    break;

                case DragEvent.ACTION_DROP:
                    TextView draggedText = (TextView)event.getLocalState();
                    Ans = (String) (draggedText.getText());
                    dropText.setText(draggedText.getText());
                    if ( Ans.equals("24"))
                    {
                            AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
                            float actualVolume = (float) audioManager
                                      .getStreamVolume(AudioManager.STREAM_MUSIC);
                            float maxVolume = (float) audioManager
                                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                            float volume = actualVolume / maxVolume;

                                soundPool.play(soundID, volume, volume, 1, 0, 1f);
                                Log.e("Test", "Played sound");
                                q2=1;
                    }
                     else
                          {
                              AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
                                float actualVolume = (float) audioManager
                                              .getStreamVolume(AudioManager.STREAM_MUSIC);
                                float maxVolume = (float) audioManager
                                .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                                float volume = actualVolume / maxVolume;

                                    soundPool2.play(soundID2, volume, volume, 1, 0, 1f);
                                    Log.e("Test", "Played sound");
                                    q2=2;
                          }
                    break;
            }

            return true;
    }
};
OnDragListener dragListener3 = new OnDragListener()
{
    @Override
    public boolean onDrag(View v, DragEvent event)
    {
            int dragEvent = event.getAction();
            TextView dropText = (TextView) v;

            switch(dragEvent)
            {
                case DragEvent.ACTION_DRAG_ENTERED:

                break;

                case DragEvent.ACTION_DRAG_EXITED:
                    break;

                case DragEvent.ACTION_DROP:
                    TextView draggedText = (TextView)event.getLocalState();
                    Ans = (String) (draggedText.getText());
                    dropText.setText(draggedText.getText());
                    if ( Ans.equals("32"))
                    {

                            AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
                            float actualVolume = (float) audioManager
                                      .getStreamVolume(AudioManager.STREAM_MUSIC);
                            float maxVolume = (float) audioManager
                                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                            float volume = actualVolume / maxVolume;

                                soundPool.play(soundID, volume, volume, 1, 0, 1f);
                                Log.e("Test", "Played sound");
                                q3=1;

                    }
                     else
                          {
                              AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
                                float actualVolume = (float) audioManager
                                              .getStreamVolume(AudioManager.STREAM_MUSIC);
                                float maxVolume = (float) audioManager
                                .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                                float volume = actualVolume / maxVolume;

                                    soundPool2.play(soundID2, volume, volume, 1, 0, 1f);
                                    Log.e("Test", "Played sound");
                                    q3=0;
                          }
                    break;
            }

            return true;
    }
};
OnDragListener dragListener4 = new OnDragListener()
{
    @Override
    public boolean onDrag(View v, DragEvent event)
    {
            int dragEvent = event.getAction();
            TextView dropText = (TextView) v;

            switch(dragEvent)
            {
                case DragEvent.ACTION_DRAG_ENTERED:

                break;

                case DragEvent.ACTION_DRAG_EXITED:
                    break;

                case DragEvent.ACTION_DROP:
                    TextView draggedText = (TextView)event.getLocalState();
                    Ans = (String) (draggedText.getText());
                    dropText.setText(draggedText.getText());
                    if ( Ans.equals("40"))
                    {

                            AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
                            float actualVolume = (float) audioManager
                                      .getStreamVolume(AudioManager.STREAM_MUSIC);
                            float maxVolume = (float) audioManager
                                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                            float volume = actualVolume / maxVolume;

                                soundPool.play(soundID, volume, volume, 1, 0, 1f);
                                Log.e("Test", "Played sound");
                                q4=1;

                    }
                     else
                          {
                              AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
                                float actualVolume = (float) audioManager
                                              .getStreamVolume(AudioManager.STREAM_MUSIC);
                                float maxVolume = (float) audioManager
                                .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                                float volume = actualVolume / maxVolume;

                                    soundPool2.play(soundID2, volume, volume, 1, 0, 1f);
                                    Log.e("Test", "Played sound");
                                    q4=0;
                          }
                    break;
            }

            return true;
    }
};
OnDragListener dragListener5 = new OnDragListener()
{
    @Override
    public boolean onDrag(View v, DragEvent event)
    {
            int dragEvent = event.getAction();
            TextView dropText = (TextView) v;

            switch(dragEvent)
            {
                case DragEvent.ACTION_DRAG_ENTERED:

                break;

                case DragEvent.ACTION_DRAG_EXITED:
                    break;

                case DragEvent.ACTION_DROP:
                    TextView draggedText = (TextView)event.getLocalState();
                    Ans = (String) (draggedText.getText());
                    dropText.setText(draggedText.getText());
                    if ( Ans.equals("48"))
                    {

                            AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
                            float actualVolume = (float) audioManager
                                      .getStreamVolume(AudioManager.STREAM_MUSIC);
                            float maxVolume = (float) audioManager
                                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                            float volume = actualVolume / maxVolume;

                                soundPool.play(soundID, volume, volume, 1, 0, 1f);
                                Log.e("Test", "Played sound");
                                q5=1;

                    }
                     else
                          {
                              AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
                                float actualVolume = (float) audioManager
                                              .getStreamVolume(AudioManager.STREAM_MUSIC);
                                float maxVolume = (float) audioManager
                                .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                                float volume = actualVolume / maxVolume;

                                    soundPool2.play(soundID2, volume, volume, 1, 0, 1f);
                                    Log.e("Test", "Played sound");
                                    q5=0;
                          }
                    break;
            }

            return true;
    }
};

}

5 个答案:

答案 0 :(得分:1)

尝试以下代码: -

if (end==5)

见以下链接: -

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

Simple Assignment Operator

=       Simple assignment operator

Arithmetic Operators

+       Additive operator (also used
        for String concatenation)
-       Subtraction operator
*       Multiplication operator
/       Division operator
%       Remainder operator

Unary Operators

+       Unary plus operator; indicates
        positive value (numbers are 
        positive without this, however)
-       Unary minus operator; negates
        an expression
++      Increment operator; increments
        a value by 1
--      Decrement operator; decrements
        a value by 1
!       Logical complement operator;
        inverts the value of a boolean

Equality and Relational Operators

==      Equal to
!=      Not equal to
>       Greater than
>=      Greater than or equal to
<       Less than
<=      Less than or equal to

Conditional Operators

&&      Conditional-AND
||      Conditional-OR
?:      Ternary (shorthand for 
        if-then-else statement)

Type Comparison Operator

instanceof      Compares an object to 
                a specified type 

Bitwise and Bit Shift Operators

~       Unary bitwise complement
<<      Signed left shift
>>      Signed right shift
>>>     Unsigned right shift
&       Bitwise AND
^       Bitwise exclusive OR
|       Bitwise inclusive OR

答案 1 :(得分:0)

if (end=5)

应该是

if (end==5)

如果您想比较==

,则必须使用int

答案 2 :(得分:0)

如果你想检查结束等于5则不应该是

if(end==5)
{
}

答案 3 :(得分:0)

If语句应该在任何函数或方法中......你不能将if语句直接写入你的类....

您的if声明不包含任何正文...... 因此,将其带入解决syntex错误的任何函数或方法。

答案 4 :(得分:-1)

您应该尝试使用此代码。我有类似的问题,这解决了它:

    if(end.equals(5)){
       //Your code
    }