• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sooo Close (Arrays & exit loop w/ 0 )  RSS feed

 
Stacey Johnson
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone;
First off I'd just like to say how great this website is. I've gotten lots of great advise and I appreciate it.
Well I need more advice. Below I have written a program that asks the user to input integers up to an array of 30 and then the output finds the smallest integer inputed. I've got all that working fine, what I don't have is the statement that says if the input is "0" it will jump to the result. I've tried working it a few different ways and I know I'm close but I can't seem to get it. Does anyone have any advise?? Please
public class Array5_3 {
public static void main(String [] args) {

int[] integers = new int[4];
for (int x = 0; x < integers.length; x++){

String input = JOptionPane.showInputDialog(null,
"Please enter a number, \nto return the smallest number enter 0.",
"Programming",JOptionPane.QUESTION_MESSAGE);

integers[x] = Integer.parseInt(input);
}
int small = integers[0];
for (int x = 0; x < integers.length; x++) {
if (small > integers[x])
small = integers[x];
}
JOptionPane.showMessageDialog(null, "The smallest integer inputed was "+ small,
"Programming", JOptionPane.PLAIN_MESSAGE);

System.exit(0);
}
}
Thanks in advance
Stacey
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also, in your code where you look for the smallest, you will need to exclude zero from being the smallest; otherwise, the answer will always be zero.
Alternatively, you could introduce an index variable and make it more than just a loop variable:

[ January 21, 2004: Message edited by: Joel McNary ]
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easiest way to get out of the loop is to use the keyword break. Something like this:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!