The Artima Developer Community
Sponsored Link

Java Answers Forum
apcs kid in need of helpxor

1 reply on 1 page. Most recent reply: Mar 21, 2005 12:56 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
James

Posts: 1
Nickname: newb
Registered: Mar, 2005

apcs kid in need of helpxor Posted: Mar 18, 2005 8:53 AM
Reply to this message Reply
Advertisement
public class Rotate
{
	
	int a[] = {1,2,3,4,5,6};
    
    EasyReader console = new EasyReader();
    
    int d = console.readInt();
    
    
 
public static void rotate(int a[], int d)
{
	
	System.out.println("Enter an integer: ");
    
    int temp, i;	
    	
    while (d < 0)
	{
	    temp = a[a.length - 1];
	    for (i = a.length - 1;   i > 0;   i--)
  	    {
  		    a[i] = a[i - 1];
  		}
  	    a[0] = temp;
  	    d--;
  	
  	}
  	while (d > 0)
  	{
  		 temp = a[0];
  		  		 
  		 for (i = 0;   i < a.length - 1;   i++)
  		 {
  		 	a[i] = a[i + 1];
  		 }
  		 a[a.length - 1] = temp;
  		 d++;
  	}
  	
  System.out.print(a);
}
}




the applet starts... and starts... and starts.... and never really opens, just loops over and over again. compile aand see for your self. help?


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: apcs kid in need of helpxor Posted: Mar 21, 2005 12:56 AM
Reply to this message Reply
First of all a hint:
You seem to use non-static variables for a static method, wich works if you create an instance of the class and call the static method through the instance. But then it would not make sense using a static method, would it?


Ok, now to your problem:

public someMethod (int d) {
  while (d < 0) { //1*
     d--; //will never become bigger or equal to 0 => endless loop
  }
  while (d > 0) { //2*
     d++; //will never become smaller or equal to 0 => endless loop
  }
}


See?
If d is bigger than 0 you will remain in loop 2.
If d is smaller than 0 you will remain in loop 1.

Only if d is equal to 0 from the beginning the program will not enter an endless loop.

Flat View: This topic has 1 reply on 1 page
Topic: Hash Map Previous Topic   Next Topic Topic: compiling error

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use