Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can someone fix me?

 
Carl Headley
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is my code. What I've been trying to do for the past six days is design a loop that reads five integers, sorts them using a while loop, and print out the smallest number entered. I can easily accomplish this with the for loop, but have no clue as to how this is done using a while loop. Can someoone please fix me?



import java.text.*; //this package supports number formatting class
import java.util.Arrays; //this package supports Arrays


public class Whileloop
{
public static void main(String[] args)
{

int[] number = new int[5];

int index=0;
int sum;
System.out.println("Enter 5 whole numbers: ");

sum = 0;
do
{

number [index] = SavitchIn.readLineInt( );
index=index+1;
sum=sum+1;
}while (sum < 5);
/*This part sorts*/

while (number[0]<number[1]&&number[2]&&number[3]&&number[4]);
{

do{
if (number[0]!<number[1]&&number[2]&&number[3]&&number[4])
System.out.print("searching.");
else {System.out.print("the smallest number is"+number[0]);}
} while (number[1]<number[0]&&number[2]&&number[3]&&number[4]);

do{if (number[1]!<number[0]&&number[2]&&number[3]&&number[4]);
System.out.print("...");
else System.out.print("the smallest number is"+number[1]);
} while (number[2]<number[0]&&number[1]&&number[3]&&number[4]);

do{if (number[2]!<number[1]&&number[0]&&number[3]&&number[4]);
System.out.print("...");
else System.out.print("the smallest number is"+number[2]);

} while (number[3]<number[0]&&number[1]&&number[2]&&number[4]);

do{if (number[3]!<number[1]&&number[2]&&number[0]&&number[4]);
System.out.print("...");
else System.out.print("the smallest number is"+number[3]);

} while (number[4]<number[0]&&number[1]&&number[3]&&number[2]);

do{if (number[4]!<number[1]&&number[2]&&number[3]&&number[0]);
System.out.print("...");
else System.out.print("the smallest number is"+number[4]);


}
System.out.print(/n"Thank"+number[0]);


}}


Any corners that can be cut in the while loops would be greatly appreciated.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can easily accomplish this with the for loop, but have no clue as to how this is done using a while loop.


They're basically the same

for(int x = 0; x < 10 ; x++)
{
//do something
}

int x = 0;
while(x < 10)
{
//do something
x++
}
 
Lisa D'Aniello
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Carl,

Holy while loops! To cut down on the loops, you could use 2 loops instead of 5.

You will need 2 temporary variables, 2 counters. Here is the example from my textbook that helped me a lot when I was first learning C++ (slightly modified to fit java syntax)

 
Carl Headley
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the quick response, but could you explain how I could swap the numbers for the sort?
 
Carl Headley
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks so so much everybody.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic