Results 1 to 4 of 4
Thread: Looping
- 09-08-2010, 06:34 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 18
- Rep Power
- 0
Looping
public static void main(String[] args){
int oddNo;
oddNo = getOdd();
}
public static int getOdd(){
int oddNo = 0;
Scanner sc = new Scanner(System.in);
while (oddNo % 2 == 0){
System.out.print("Enter an odd number : ");
oddNo = sc.nextInt();
System.out.println("Wrong input ! ");
}
if (oddNo % 2 !=0);
System.out.print("The odd number is : " + oddNo);
return oddNo;
}
however, the output:
Enter an odd number : 10
Wrong input !
Enter an odd number : 8
Wrong input !
Enter an odd number : 9
Wrong input ! (how to remove this?)
The odd number is : 9
- 09-08-2010, 07:13 AM #2
You'll want an if statement attached to this line:
...to verify whether or not the new input is odd/even.Java Code:System.out.println("Wrong input ! ");
PS: Adding [code] tags around your code is helpful to those trying to read it.
- 09-08-2010, 07:20 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 18
- Rep Power
- 0
i've got thanks and btw how i go about solving the following:
Write the main() method to call getNum () in order to get a series of positive integers between 1 and 9999. It will stop calling when the user enters 9999. The program will compute and display the following:
a) product of all the numbers,
b) average of all the numbers,
c) difference between the biggest and the smallest number
- 09-08-2010, 02:01 PM #4
You've got the code for reading in number from a user. Now you need to add some logic to the loop where you read these numbers to do the computations as per the program requirements.
Start with a program with a loop that prompts for the next number and exits the loop when the number is the end number value.
When that works, move to the next part of the problem.
Similar Threads
-
Help with While and For Looping
By gmoney8316 in forum New To JavaReplies: 2Last Post: 03-03-2010, 10:54 PM -
looping and filtering
By javafanatic in forum New To JavaReplies: 14Last Post: 02-09-2010, 09:48 AM -
Looping Help Please
By JonnySnip3r in forum New To JavaReplies: 5Last Post: 01-31-2010, 05:57 AM -
looping problems
By Blakester in forum New To JavaReplies: 4Last Post: 10-05-2009, 08:26 PM -
Looping if statements
By rice in forum New To JavaReplies: 10Last Post: 10-02-2009, 02:50 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks