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");
                        }

	}
	
	
}
}

So, what is your problem?

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

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");
}
}

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!

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.