Results 1 to 7 of 7
- 02-17-2012, 05:43 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
hellow im new here..can somebody help me with this prog i think its easy for you->
for you guys to understand i am 1st yr college. begginer in java pls help me with this guys i just need the prog that will stop if you top a negative number for exmple this prog cant figure it out.
import java.util.Scanner;
public class ReverseOrder
{
public static void main (String[] args)
{
Scanner scan = new Scanner (System.in);
double[] numbers = new double[10];
System.out.println ("The size of the array: " + numbers.length);
for (int index = 0; index < numbers.length; index++)
{
System.out.print ("Enter number " + (index+1) + ": ");
numbers[index] = scan.nextDouble();
}
System.out.println ("The numbersyou entered:");
for (int index = numbers.length-1; index >= 0; index--)
System.out.print (numbers[index] + " ");
}
}
help me pls when you type - number the prog will stop and display the numbers you entered..thank you asap
output
enter number 1: 3
enter number 2: 3
enter number 3: 3
enter number 4: 3
enter number 10: 4
the number you entered :
3.0 3.0 3.0 3.0.3.0 4.0
- 02-17-2012, 06:11 AM #2
Member
- Join Date
- Nov 2011
- Posts
- 56
- Rep Power
- 0
Re: hellow im new here..can somebody help me with this prog i think its easy for you-
Basically, you'll have to check for it inside your first for loop.
You can do so by using an if else statement.
In this case, check if the number[index] if it is less than 0.
***DO WHAT YOU WANT***
to break out from the loop, use a
break;
With your current code, after you enter a negative number at say Number 3, it will stop and print something like
The size of the array: 10
Enter number 1: 1
Enter number 2: 3
Enter number 3: -3
The numbersyou entered:
0.0 0.0 0.0 0.0 0.0 0.0 0.0 -3.0 3.0 1.0
if that is what you want.Last edited by rhexis; 02-17-2012 at 06:35 AM.
- 02-17-2012, 06:58 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
Re: hellow im new here..can somebody help me with this prog i think its easy for you-
thank you i cant still figure it out i tried to use if else break;
maybe i can use another variable maybe if use this if (index <0)
break;
stil working on it sorry im not good in english
thanks for replz u gve me an idea :)
- 02-17-2012, 07:03 AM #4
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
Re: hellow im new here..can somebody help me with this prog i think its easy for you-
size of the array 10 you entered 10 number but if type -neagtive the prog stop in print the num you type
enter num 1: 90
enter num 2: -56
the number you entered :
90.0 -56.0
like this
- 02-17-2012, 07:07 AM #5
Member
- Join Date
- Nov 2011
- Posts
- 56
- Rep Power
- 0
Re: hellow im new here..can somebody help me with this prog i think its easy for you-
Can the user type 0 as a number?
If the user can enter 0 as a number, I don't think you should use an Array as it will store 0.0 if no values are entered, and would therefore print out all the values 1-10 in reverse order.
You can change your Array to an ArrayList. An ArrayList grows dynamically with no fixed size.
if you assume that the user won't enter 0 as a number, all you have to do is add another if else statement at the 2nd for loop to check that if (numbers[index] != 0), print that number. The downside is of course, if the user enters a number 0, it won't print.
An example of what I mean
The size of the array: 10
Enter number 1: 3
Enter number 2: 0
Enter number 3: -3
The numbersyou entered:
-3.0 3.0Last edited by rhexis; 02-17-2012 at 07:13 AM.
- 02-17-2012, 09:16 AM #6
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
Re: hellow im new here..can somebody help me with this prog i think its easy for you-
import java.util.Scanner;
public class Hirap
{
public static void main (String[] args)
{
Scanner scan = new Scanner (System.in);
int[] numbers = new int[10];
System.out.println ("The size of the array: " + numbers.length);
for (int index = 0; index <numbers.length; index++){
System.out.print ("Enter number " + (index+1) + ": ");
numbers[index] = scan.nextInt();
if(numbers[index]<0){
break;
}
}
System.out.println ("The numbers you entered:");
for (int index = numbers.length-1; index>= 0; index--)
System.out.print (numbers[index] + " ");
}
}
ihave a little prob when i entered a -number the prog wil stop and print the ouput like this
size of array 10:
enter num 1: 15
enter num 2: 16
but when you entered - numbers the prog wil stop but print the 10 array size:((
enter num 3 : -19
the num you entered:
15 16 -19 0 0 0 0 0 0 0 0
huhuh haha itsto hard for a begginer zzzz
- 02-17-2012, 11:33 AM #7
Member
- Join Date
- Nov 2011
- Posts
- 56
- Rep Power
- 0
Re: hellow im new here..can somebody help me with this prog i think its easy for you-
Like I have mentioned, the problem is because you are using an Array that you have already declared a size too, which in this case is 10
In this case, any unfilled slots in an Array will be filled with the int 0.
I recommend you use an ArrayList. Just google on how to use an ArrayList, it's very simple.
An ArrayList has no size limit, and allows you to dynamically add elements into it.
Similar Threads
-
prog
By mamtha in forum New To JavaReplies: 2Last Post: 01-11-2012, 04:59 PM -
Running my prog.
By arcticM in forum AWT / SwingReplies: 3Last Post: 08-24-2009, 03:42 AM -
What I need to do more in this prog??
By eliCanzee in forum AWT / SwingReplies: 2Last Post: 06-01-2009, 03:47 AM -
hellow you guys...
By babyarle20 in forum IntroductionsReplies: 1Last Post: 08-19-2008, 11:51 AM -
want menu driven prog. who to take user i/p in the middle of the prog.
By Shyam Singh in forum New To JavaReplies: 1Last Post: 07-13-2008, 03:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks