Unreachable Code

itsame123

Mitglied
Hi,

ich hab keine Ahnung warum Eclipse bei

if(x_pos > appletsize_x -radius) {

x_speed = -1;

}

else if(x_pos < radius) {
x_speed = +1;

}

einen Unreachable Code Fehler ausgibt .

Danke im Voraus 🙂.


Java:
import java.applet.*;
import java.awt.*;
import javax.swing.*;


public class FirstApplet extends Applet {
	
	int x_pos = 10;
	int y_pos = 100;
	int x_speed =1;
	int radius = 20;
	private Image dbimage;
	private Graphics dbg;
	int appletsize_x = 300;
	

	
	
	public void init() {
		
		
	}
	
	public void start() {
		
		
	}
	
	public void stop() {
		
		
	}
	
	public void destroy() {
		
		
	}
	

	
	public void run() {
		
		Thread th = new Thread();
		th.start();
		
		
		
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY); //hier

		while(true) {
			repaint();
			
			x_pos++;
			
			try {
				Thread.sleep(20); 
				
			}
			catch(InterruptedException ex) {
				
				
			}
			
			Thread.currentThread().setPriority(Thread.MAX_PRIORITY); 		}
		
	

		if(x_pos > appletsize_x -radius) {
			
			x_speed = -1;
			
		}
		
		else if(x_pos < radius) {
			x_speed = +1;
			
		}
		
		x_pos += x_speed;

		
	}
	
	public void paint (Graphics g) {
		g.setColor(Color.red);
		g.fillOval(x_pos-radius, y_pos-radius, 2*radius, 2*radius);
		
	}
	
	public void upadte(Graphics g) {
		
		if(dbimage == null) {
			
			dbimage = createImage(this.getSize().width, this.getSize().height);
			dbg = dbimage.getGraphics();
			
		}
		
		dbg.setColor(getBackground());
		dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
		
		dbg.setColor(getForeground());
		paint(dbg);
		
		g.drawImage(dbimage, 0, 0, this);
					
			
		}
	
}
 
Nun du hast eine Endlosschleife
Java:
while (true) {
}

Also werden Anweisungen danach nie erreicht.
 
Du solltest deinen code mal besser einrücken.

Java:
while(true) {
            repaint();
           
            x_pos++;
           
            try {
                Thread.sleep(20);
            }
            catch(InterruptedException ex) {
  
            }
           
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);        
}

Du hast da eine Endlosschleife ohne Abbruchbedingung, der code danach kann niemals ausgeführt werden.
 

Zurück
Oben