0

Hello everyone at Daniweb! This is my first post here, sorry if i do something wrong.

Ok, so just a quick background on me and where I'm at in java. This year I'm taking my first Comp Sci class as CSUN (Cal State University Northridge). I am a complete beginner when it comes to programming, you have been warned haha.

Anyway, I have to write a program that prints 10 random numbers between 1-100, then prints how many are even and how many are odd.

This is what I have so far.

import java.util.Scanner;
public class problem3{
public static void main(String[] args){

	for(int i=1;i<=10;i++){
		int randomNumber = (int)(99 * Math.random())+1;
		System.out.println(randomNumber);
		
		
		
		while(randomNumber % 2 == 0){
			int k = 0 
			k++;
			break;
			System.out.println("you have " + z + " odd numbers");
                        }

		while(randomNumber % 2 != 0){
			int z = 0 ;
			z++;
			break;
			System.out.println("you have " + k + " even numbers");
                        }

	}
	
	
}
}
4
Contributors
6
Replies
8
Views
5 Years
Discussion Span
Last Post by leodash
0

ahh sorry! ok so when i run the program is gives me.

74
you have 1 even numbers
38
you have 1 even numbers
99
you have 1 odd numbers
76
you have 1 even numbers
86
you have 1 even numbers
33
you have 1 odd numbers
94
you have 1 even numbers
53
you have 1 odd numbers
54
you have 1 even numbers
61
you have 1 odd numbers


rather than

61
56
23
56
13
34
you have 3 even numbers
you have 3 odd numbers

0

So what the problem was you need to declare the z int and k int outside the for loop to have a starting point if that makes sense. then every time you run the loop and you get odd number, z will increment and the same for k with even numbers.

import java.util.Scanner;
public class problem3{
public static void main(String[] args){
int z = 0;
int k = 0;
for(int i=1;i<=10;i++){
int randomNumber = (int)(99 * Math.random())+1;
System.out.println(randomNumber);

while(randomNumber % 2 == 0){
k++;
break;

}

while(randomNumber % 2 != 0){
z++;
break;

}

}

System.out.println("you have " + z + " odd numbers");
System.out.println("you have " + k + " even numbers");
}
}

0

Ooooo, I see. I had tried something very similar to that, except I ended up with an endless loop!

Thank you so much for your time!

0

but why the wile loops? they're not really used, since they will never add up more than 1.

just read the code and rethink what it does.

0

Hello everyone at Daniweb! This is my first post here, sorry if i do something wrong.

Ok, so just a quick background on me and where I'm at in java. This year I'm taking my first Comp Sci class as CSUN (Cal State University Northridge). I am a complete beginner when it comes to programming, you have been warned haha.

Anyway, I have to write a program that prints 10 random numbers between 1-100, then prints how many are even and how many are odd.

This is what I have so far.

import java.util.Scanner;
public class problem3{
public static void main(String[] args){

	for(int i=1;i<=10;i++){
		int randomNumber = (int)(99 * Math.random())+1;
		System.out.println(randomNumber);
		
		
		
		while(randomNumber % 2 == 0){
			int k = 0 
			k++;
			break;
			System.out.println("you have " + z + " odd numbers");
                        }

		while(randomNumber % 2 != 0){
			int z = 0 ;
			z++;
			break;
			System.out.println("you have " + k + " even numbers");
                        }

	}
	
	
}
}

You can search the net about decision making in programming. That could give you some hint.

Edited by leodash: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.