Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
This is a simple class exercise I'm working on. (I'm very new to programming so if this was a simple 'rookie' mistake I apologize for wasting your time.) I'm not going to lie: I'm finding it difficult to know where to insert certain pieces of code when programming.
import java.util.*;
public class SuperSaveRandallTWyngaardC {
static Scanner console=new Scanner(System.in);
public static void main(String[] args) {
char newCust;
char promo;
int itemNr=0;
int qty=0;
int price=0;
int totalPrice=0;
int custTot=0;
int noOfItems=0;
int grandTot=0;
int custCount=0;
System.out.println(" ");
System.out.println("*******SuperSave - your friendly local store.....*******");
System.out.println(" ");
System.out.print("New customer? (Y/N)>> ");
newCust=console.next().charAt(0);
newCust=Character.toUpperCase(newCust);
while((newCust!='Y')&&(newCust!='N'))
{
System.out.print("Invalid option, please re-enter (Y/N)>> ");
newCust=console.next().charAt(0);
newCust=Character.toUpperCase(newCust);
}
if (newCust == 'N')
{
System.out.println("*******NO SALES THE WHOLE DAY.....*******");
}
else if (newCust == 'Y')
{
System.out.print("Please enter the item number (1000 -> 5000 or zero for none)>> ");
itemNr=console.nextInt();
while ((itemNr<1000)&&(itemNr>5000)||(itemNr!=0))
{
System.out.print("Invalid item number, please re-enter (1000 -> 5000 or zero to stop)>> ");
itemNr=console.nextInt();
}
if (itemNr==0)
{
System.out.println("*******NO ITEMS WERE BOUGHT BY THIS CUSTOMER.....*******");
}
else if ((itemNr>1000)&&(itemNr<5000))
{
System.out.print("Enter quantity>> ");
qty=console.nextInt();
}
}
}
}
Run the program. Sample output...
*******SuperSave - your friendly local store.....*******
New customer? (Y/N)>> y
Please enter the item number (1000 -> 5000 or zero for none)>> 1000
Invalid item number, please re-enter (1000 -> 5000 or zero to stop)>> 5000
Invalid item number, please re-enter (1000 -> 5000 or zero to stop)>> 999
Invalid item number, please re-enter (1000 -> 5000 or zero to stop)>> 5001
Invalid item number, please re-enter (1000 -> 5000 or zero to stop)>> 1234
Invalid item number, please re-enter (1000 -> 5000 or zero to stop)>> 4000
Invalid item number, please re-enter (1000 -> 5000 or zero to stop)>> 0
*******NO ITEMS WERE BOUGHT BY THIS CUSTOMER.....*******
The while loop says that any item number input is invalid (even when inside the specified range of 1000-5000)
Your loop condition is off.
Since the negatives seems to confuse, first write what is good:
(itemNr >= 1000 && itemNr <= 5000) || itemNr == 0
I.e. must be between 1000 and 5000 (inclusive), OR must be 0.
Since most people can't get the precedence of && vs || right, you should always use parenthesis to explicitly specify the precedence when mixing them, as I just did.
This makes reversing the expression easy, since you just reverse everything, and leave the parenthesis alone:
(itemNr < 1000 || itemNr > 5000) && itemNr != 0 // correct #1
Compare that to what you had, and you'll see the problem:
(itemNr < 1000) && (itemNr > 5000) || (itemNr != 0) // wrong
Since > 5000 means that it definitely is != 0, you can rearrange the expression like this, as others have shown:
(itemNr < 1000 && itemNr != 0) || itemNr > 5000 // correct #2
Technically, #2 performs better than #1, but that's a difference you will not ever notice. Personally, I find #2 less intuitive than #1, but that's a matter of opinion. They both get you want you want.
(itemNr<1000)&&(itemNr>5000)||(itemNr!=0)
There is no itemNr that makes it true.
Consider -1, 1, 1001, 5001.
((itemNr<1000)&&(itemNr!=0))||(itemNr>5000)
You should try this.
(itemNr < 1000) && (itemNr > 5000) Item number has to be smaller than 1000 AND greater than 5000 at the same time, that is not possible.
You are doing a similar check later with (itemNr > 1000) && (itemNr < 5000).
It would be useful to make a function isInRange(int) that does this check. So you can use !isInRange(itemNr) (not in range) for the first check.
The second check is not needed, since the number will always be in range (or 0) after the while loop.
private static boolean isInRange(int i) {
return ((i > 1000) && (i < 5000));
}
...
while(!isInRange(itemNr) && itemNr != 0) {
...
}
if (itemNr == 0) {
...
} else { // no need to check here
...
}
To correct the check for valid inputs, 1000... 5000 and 0, update
while ((itemNr<1000)&&(itemNr>5000)||(itemNr!=0))
to
while ((itemNr < 1000 && itemNr != 0) || itemNr > 5000)
If 1000 and 5000 are valid inputs, then you have to include them while getting the quantity.
else if ((itemNr>1000)&&(itemNr<5000))
to
else if ((itemNr >= 1000) && (itemNr <= 5000))
So the last block will look like
while ((itemNr < 1000 && itemNr != 0) || itemNr > 5000) {
System.out.print("Invalid item nyumber, please re-enter (1000 -> 5000 or zero to stop)>> ");
itemNr = console.nextInt();
}
if (itemNr == 0) {
System.out.println("*******NO ITEMS WERE BOUGHT BY THIS CUSTOMER.....*******");
} else if ((itemNr >= 1000) && (itemNr <= 5000)) {
System.out.print("Enter quantity>> ");
qty = console.nextInt();
}
Related
I'm doing a hackernet challenge where n is an int input. The conditions are:
If n is odd, print Weird
If n is even and in the inclusive range of 2 to 5, print Not Weird
If n is even and in the inclusive range of 6 to 20, print Weird
If n is even and greater than 20, print Not Weird.
Im sure the code makes logic and dont think theres syntax. It gives the correct responses and hackernet still says its incorrect so ive come here to see if anyone can see what the problem is
public static void main(String[] args)
{
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
if (N % 2 != 0 || N % 2 == 0 && N >= 6 && N <= 20)
{
System.out.print("weird");
}
else
{
System.out.print("not weird");
}
}
The problem is the logic in your else condition, which would also catch values of N which are less than 2. Try this version:
if (N % 2 != 0)
{
System.out.print("weird");
}
else if (N >= 2 && N <= 5 || N > 20)
{
System.out.print("not weird");
}
else if (N >= 6 && N <= 20)
{
System.out.print("weird");
}
else
{
// NOTE: if the code still fails, remove this else condition
System.out.print("unexpected value of N");
}
Note: To get your code to pass the Hackernet task, you might have to completely remove the else condition. I added it for completeness, but Hackernet might test N=1 to see if nothing gets printed.
Read this condition :
if (N % 2 != 0 || N % 2 == 0 && N >= 6 && N <= 20)
as
if (N % 2 != 0 || (N % 2 == 0 && N >= 6 && N <= 20))
Then see how operator precedence changes the behaviour and yield desired results.
Check the following one
public static void main(String[] args)
{
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
if(N%2!=0) {
System.out.print("weird");
}else if(N>=2 && N<=5) {
System.out.print("not weird");
}else if(N>=6 && N<=20) {
System.out.print("weird");
}else if(N>20) {
System.out.print("not weird");
}
}
For the technical part: start by reading about
precedence of java operators and then make your code easier to read.
Pushing that many conditions into a single if is not helpful. You see it yourself: you think the code is correct, but probably it isn't. And now you look to other people to explain your overly complex code back to you. And of course, all the other answers do all that for you ... but beyond that:
The "real" answer here is: learn how to test your code.
Instead of having a main that somehow asks for a number, and then makes decisions, write a method boolean isWeird() that takes a number and returns true/false according to your requirements.
And then simply test that method with all reasonable cases. And then check if that result is as expected.
Using JUnit, you could write something like
assertThat(isWeird(1), true);
assertThat(isWeird(21), true);
assertThat(isWeird(22), true);
...
Ideally, you write such tests before you implement that method. And then you implement all the conditions, and any check that fails tells you that you got something wrong.
I feel, In the if (N % 2 != 0 || N % 2 == 0 && N >= 6 && N <= 20) condition, you are verifiying the odd and even values at same time using && and || operator. Can you modify the condition into like this if (N % 2 != 0 || (N % 2 == 0 && N >= 6 && N <= 20)) and check? If N is odd weird will be printed or if N is even and it falls under the 6 and 20 inclusive, weird will be printed.
You already have a good few answers here but if you think logically about what you actually need, you can break it down easier.
It looks like the only "Not Weird" print out is 2, 4 and even numbers > 20
So an example could be something like:
if (n % 2 == 0) {
if ((n >= 2 && n <= 5) || (n > 20)) {
return "Not Weird";
}
}
return "Weird";
You can try this
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
if (n % 2 == 1 || (n >= 6 && n <= 20)) {
System.out.println("Weird");
} else {
System.out.println("Not Weird");
}
scanner.close();
}
im having a problem in my java exercice
the question is saying
The program will end when the sum of even is >= 50 or the sum of odd is >= 49.
so while solving it i tried to use
while (sumeven < 50 || sumodd < 49 )
and it didnt worked at all but when i checked the solution they use
while (evensum <= 50 && oddsum<=49)
and it worked ( gave same answeres like the sample run)
so my question is did i misunderstood it ? or the question have some kind of a wrong given. thank you for your time
update:
the code :
package sample2;
import java.util.Scanner;
public class Sample2 {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("enter the initial value");
int sumeven=0;
int sumodd=0;
int num;
int initial;
int div5and2=0;
initial=scan.nextInt();
if (initial<=0)
System.out.println("the number must be strictly positive");
else{
if (initial%2==0)
sumeven=initial;
else
sumodd=initial;
System.out.println("start entering your numbers");
num=scan.nextInt();
if (num<=0)
System.out.println("the number must be strictly positive");
else{
while(sumeven<=50||sumodd<=49 )
{
if (num%2==0)
sumeven=sumeven+num;
else
sumodd=sumodd+num;
if (num%5==0 &&num%2==0)
div5and2=div5and2+1;
num=scan.nextInt();
while(num<=0)
{
System.out.println("the number must be strictly positive");
num=scan.nextInt();
}
}
System.out.println("the sum of even numbers: "+sumeven);
System.out.println("the sum of odd numbers: "+sumodd);
System.out.println("the number of integers divided by 2 and 5: "+div5and2);
}
}
}
}
This is basic boolean algebra.
The 'opposite' of or is and, so if you want to stop when
"even >= 50 or odd >= 49"
you have to continue on the opposite, which is
"even < 50 and odd < 49".
while(sumeven<50||sumodd<49 ) means the program will run when sumeven<50 or sumodd<49, which means it will exit when sumeven>=50 and sumodd>=49.
When you negate a statement, >= becomes < and <= becomes >. Similarly, AND becomes OR and OR becomes AND. Here, your stopping condition is even >= 50 OR odd >= 49, the negation of this (which is what is required to continue) is even<50 AND odd<49.
You want the program to end when " the sum of even is >= 50 or the sum of odd is >= 49."
So, need something like (in psuedo code):
while (Not( the sum of even is >= 50 or the sum of odd is >= 49))
De Morgan's laws tell us we need to not each part to remove the brackets and switch between and and or:
while (Not( the sum of even is >= 50) and Not( or the sum of odd is >= 49))
Let's try some examples.
If the even sum is 50 and the odd sum is 40, you want to stop.
If you check
while (sumeven < 50 || sumodd < 49 )
you will keep going since sumeven < 50 is false, but sumodd < 49 is true.
Checking both parts:
while (evensum <= 50 && oddsum<=49)
works.
while(sumeven<50||sumodd<49 )
In this case, the program did not end if sumeven<50 OR sumodd<49. That is, the program will end when sumeven>=50 AND sumodd>=50.
Boolean logic 101:
!(A || B) == !A && !B note the &&.
Essentially if you are looking at a state where either A or B and want to take the compliment of that you must code it as not either A or B which is obviously neither A nor B or not A and not B.
AIM - The program will end when the sum of even is >= 50 or the sum of odd is >= 49.
Therefore, while (sumeven < 50 || sumodd < 49 ) won't work as the program will end when the sum of even is < 50 or the sum of odd is < 49 which is not the expected behavior.
Therefore, we must loop till
The sum of even is less than or equal to 50
The sum of odd is less than or equal to 49
The overall set of conditions should only be true if all conditions are true therefore we use && operator with the 2 conditions.
This program enters in a temperature and based on the temperature a message is outputted. My program is getting stuck tempOutside <= 50 message.. When I enter -10 the output is "Time to turn on the heat". I cannot use && as this is an assignment questions and we have not used this concept.
if (tempOutside > 50)
System.out.println("Enjoy the weather");
else if (tempOutside <= 50)
System.out.println("Time to turn on the heat");
else if (tempOutside <= 30)
System.out.println("Check the gas in you car before leaving");
else if (tempOutside <= -10)
System.out.println("BUNDLE UP it's \"COLD\" outside ");
else
1) if-else must be in numerical order (hi to low)
2) Default must be coded
3) Repeat the code, but reverse the order(low to hi)
All ints are either > 50 or <= 50. The first two conditions match everything.
Change the second condition to
> 30
And the third condition to
> -10
Etc.
it would have been more clear if you could have mentioned your exact question directly and then your implementation as it looks like there could be a misunderstanding of the question here.
But I think perhaps this is what is being asked here??
if (tempOutside > 50)
{
System.out.println("Enjoy the weather");
}
else if (tempOutside <= 50)
{
if(tempOutside <= 30)
{
if(tempOutside <= -10)
{
System.out.println("BUNDLE UP it's \"COLD\" outside ");
}
else {
System.out.println("Check the gas in you car before leaving");
}
}
else
{
//when the temp is between 30 and 50
System.out.println("Time to turn on the heat");
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I just started studying Java and I'm required to use while to decided how many players can be goalkeepers based on their number. The loop is supposed to stop after the user entered 0 and print the counted number of players that can be goalkeepers.
public class Q3_201303719 {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int num; int count=0;
System.out.println("Enter the players' numbers");
num = input.nextInt();
while ((num != 0) && (num < 31) && (num%2==0) || (num%3==0))
count++;
System.out.println(count+ " players can be goalkeepers.\n");
// Above line should be printed once the user enter 0, but in my case it won't
// print and keeps asking the user to enter a number.
}
}
From the question it is difficult to understand what it is you are trying to achieve. However, we can try to help you understand the code as it is written.
The while loop is currently written as:
while ((num != 0) && (num < 31) && (num%2==0) || (num%3==0))
This could be rewritten as the following and it would not make any difference:
while (num != 0 && num < 31 && num%2==0 || num%3==0)
The additional parenthesis that you included are not required.
In Java the operator precedence is && before ||. This means that the && operators will be evaluated first, followed by the ||. Therefore, the above statement could therefore be rewritten as the following and it would not make any difference:
while ((num != 0 && num < 31 && num%2==0) || num%3==0)
However, it may make the code a little easier to understand.
So when the code executes the following occurs:
The count variable is initialised to 0.
A value is retrieved and stored in the variable num. Lets say that this value is 10.
The while statement is evaluated for the first time:
num != 0 is true as 10 != 0.
num < 31 is true as 10 is less than 31.
num%2==0 is true as 10 divided by 2 is 5 and leaves a remainder of 0.
As these all evaluate to true, the OR part (num%3==0) is not evaluated as it is not neccessary. See short circuit evaluation (http://users.drew.edu/bburd/JavaForDummies4/ShortCircuitEval.pdf).
The count is incremented to 1.
The loop executes again for the second time. num is still 10.
num != 0 is true as 10 != 0.
num < 31 is true as 10 is less than 31.
num%2==0 is true as 10 divided by 2 is 5 and leaves a remainder of 0.
The count is incremented to 2.
And so on... in an infinite loop.
If the variable num was instead set to 9. The loop would evaluate as follows:
num != 0 is true as 9 != 0.
num < 31 is true as 9 is less than 31.
num%2==0 is false as 9 divided by 2 is 4 and leaves a remainder of 1.
Therefore, now the || part is evaluted:
num%3==0 is true as 9 divided by 3 is 3 and leaves a remainder of 0.
Again, this would result in an infinite loop.
If the variable num was instead 0:
num != 0 is false
As the first num!=0 is false, the num<31 and num%2==0 parts are not evaluated as their results would not make any difference.
The num%3==0 is then evaluated:
num%3==0 is true as 0 divided by 3 leaves a remainder of 0.
Again, this would result in an infinite loop.
I hope that this may help to clarify your understanding and allow you to correct the code appropriately.
how many players' numbers do you have to input?
you have a while loop that depends on num value, but you never change the num value. that will end up in endless loop...
if you have more players, create a for loop to enter the numbers, store the numbers in an array or an arraylist and if you use the while loop, use it so that it depends on a value that you change inside the loop. otherwise it will loop forever.
do{
System.out.println("Enter the players' numbers");
num = input.nextInt();
count++;
}while(num!=0);
your code with this should look like this:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num;
int count = 0;
do {
System.out.println("Enter the players' numbers");
num = input.nextInt();
count++;
} while (num != 0);
System.out.println(count + " players can be goalkeepers.\n");
// Above line should be printed once the user enter 0, but in my case it
// won't
// print and keeps asking the user to enter a number.
}
If you can't use a do-while-loop as Ubica suggested, then you need to work your way around it:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num=2; // you need to initialize num with a value, that allows you to go inside the while loop at least once
int count = 0;
while((num != 0) && (num < 31) && (num % 2 == 0) || (num % 3 == 0)) {
System.out.println("Enter the players' numbers");
num = input.nextInt(); // user input is here inside the loop
count++; // your count will count every valid input + the user input that ends the loop
}
count--; // if your user entered 0 to exit the loop, count would have still incremented, so you need do subtract one again
System.out.println(count + " players can be goalkeepers.\n");
}
As I commented in the code, first you initialize num with a dummy value, that allows you to enter the loop at least once. Then you listen to the users input and count the loop iterations. But since the loop will execute at least once (even though your user might enter 0 right away to exit the loop) your count would be one higher than the actual number of valid inputs. So you have to subtract that 1 from your count again.
EDIT
I forgot to mention: your loop will not stop when the user enters 0 because
(num % 3 == 0) // is true with num=0
so when the user enters 0 the while condition will evaluate like this:
while( false && true && true || true )
while( false || true )
while( true )
The bracketing for the loop should fix this problem.
You need braces surrounding your while loop block. As a general rule, it is best to overuse braces rather than under-use them.
Your code should look like:
package q3_201303719;
import java.util.Scanner;
public class Q3_201303719 {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int num; int count=0;
System.out.println("Enter the players' numbers");
num = input.nextInt();
while((num != 0) && (num < 31)&& (num%2==0)|| (num%3==0)) {
count++;
}
// The braces added above are required for the while loop and will keep your program
// from not outputting your results.
System.out.println(count+ " players can be goalkeepers.\n");
}
}
Best of luck and happy coding. :)
Your code isn't making sense.
1st : The While loop is not modifying the "num" value, resulting in
infinite loop if entered.
2nd : What is the code supposed to do? It is really hard to tell from what you show.
I think placing while loop upper could help you, but still code makes no much sense
public class Q3_201303719 {
public static void main(String[] args) {
int num = 0; int count=0;
while((num != 0) && (num < 31)&& (num%2==0)|| (num%3==0)) {
Scanner input = new Scanner (System.in);
System.out.println("Enter the players' numbers");
num = input.nextInt();
count++;
}
System.out.println(count+ " players can be goalkeepers.\n");
// Above line should be printed once the user enter 0, but in my case it won't
// print and keeps asking the user to enter a number.
}
}
Edit :
This problem occurred because you didn't follow a really simple rule : think before coding.
For this exercise you need to think about what your code should be doing, and then write the code for it. Coding is just a language, if you know what you want to write then it is easier. Here you obviously don't really know what you wanna do, and as you are new with development it was likely that you get stucked
My task is to write a java program that first asks the user how many numbers will be inputted, then outputs how many odd and even numbers that were entered. It is restricted to ints 0-100. My question is: What am I missing in my code?
import java.util.Scanner;
public class Clancy_Lab_06_03 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n;
System.out.println("How many numbers will be entered?");
n = input.nextInt();
while (n < 0 || n > 100) {
System.out.println("ERROR! Valid range 0-100. RE-Enter:");
n = input.nextInt();
n++;
}
int odd = 0;
int even = 0;
while (n >= 0 || n <= 100) {
n = input.nextInt();
if (n % 2 == 0) {
even++;
} else {
odd++;
}
}
System.out.println(even + "even" + odd + "odd");
}
}
Second while loop is infinite. Relplace it with something like this:
for (int i = 0; i < n; i++) {
int b = input.nextInt();
if (b % 2 == 0) {
even++;
} else {
odd++;
}
}
Also I don't understand why are you incrementing n in first loop. For example when you will first give -5, you will be asked to re-enter the number. Then you type -1, but it gets incremented and in fact program processes 0, altough user typed -1. In my opinion it is not how it suppose to work and you should just remove this n++.
As you asked in comment - the same using while loop:
while(n > 0) {
n--;
int b = input.nextInt();
if (b % 2 == 0) {
even++;
} else {
odd++;
}
}
Also it is good idea to close input when you no longer need it (for example at the end of main method)
input.close();
You had two issues - first you were incrementing n in the first loop, rather than waiting for the user to enter a valid number.
In the second loop, you weren't comparing the number of entries the user WANTED to make with the number they HAD made - you were over-writing the former with the new number.
This version should work, although I've not tested it as I don't have java on this machine.
Note that we now sit and wait for both inputs, and use different variable names for the "how many numbers will you enter" (n) and "what is the next number you wish to enter" (num) variables? Along with a new variable i to keep track of how many numbers the user has entered.
import java.util.Scanner;
public class Clancy_Lab_06_03
{
public static void main (String[] args)
{
Scanner input = new Scanner (System.in);
int n;
System.out.println ("How many numbers will be entered?");
n = input.nextInt();
//Wait for a valid input
while (n < 0 || n > 100)
{
System.out.println ("ERROR! Valid range 0-100. RE-Enter:");
n = input.nextInt();
}
//Setup variables for the loop
int odd = 0;
int even = 0;
int num;
//Keep counting up until we hit n (where n is the number of entries the user just said they want to make)
for(int i = 0; i < n; i++)
{
//Changed this, because you were over-writing n (remember, n is the number of entries the user wants to make)
//Get a new input
while (num < 0 || num > 100)
{
System.out.println ("ERROR! Valid range 0-100. RE-Enter:");
num = input.nextInt();
}
//Check whether the user's input is even or odd
if (num % 2 == 0)
{
even++;
}
else
{
odd++;
}
}
System.out.println(even + " even. " + odd + " odd.");
}
}
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
System.out.println("Enter an Integer number:");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
if ( num % 2 == 0 )
System.out.println("Entered number is even");
else
System.out.println("Entered number is odd");
}
}
My suggestion to you is to have a clear separation of your requirements. From your post, you indicate you need to prompt the user for two distinct data items:
How many numbers will be entered (count)
The values to be analyzed
It is a good practice, especially when you are learning, to use meaningful names for your variables. You are using 'n' for a variable name, then reusing it for different purposes during execution. For you, it is obvious it was difficult to figure out what was 'n' at a particular part of the program.
Scanner input = new Scanner (System.in);
int count;
System.out.println ("How many numbers will be entered?");
count = input.nextInt();
//Wait for a valid input
while (count < 1 || count > 100)
{
System.out.println ("ERROR! Valid range 1-100. RE-Enter:");
count = input.nextInt();
}
Additionally, a count of zero should not be valid. It does not make sense to run a program to evaluate zero values (don't bother a program that does nothing). I believe the lowest count should be one instead.
int odd = 0;
int even = 0;
int value;
do
{
System.out.print("Enter a number between 0 and 100: ");
value = input.nextInt();
while (value < 0 || value > 100)
{
System.out.println ("ERROR! Valid range 0-100. RE-Enter:");
value = input.nextInt();
}
if (value % 2 == 0)
{
even++;
}
else
{
odd++;
}
count--; // decrement count to escape loop
} while (count > 0);
System.out.println(even + " even. " + odd + " odd.");
This example uses a do/while loop because in this case, it is OK to enter the loop at least once. This is because you do not allow the user to enter an invalid number of iterations in the first part of the program. I use that count variable directly for loop control (by decrementing its value down to 0), rather than creating another variable for loop control (for instance , 'i').
Another thing, slightly off topic, is that your requirements were not clear. You only indicated that the value was bounded to (inclusive) values between 0 and 100. However, how many times you needed to repeat the evaluation was not really clear. Most people assume 100 was also the upper bound for your counter variable. Because the requirement is not clear, checking a value greater or equal to 1 for the count might be valid, although highly improbable (you don't really want to repeat a million times).
Lastly, you have to pay attention to AND and OR logic in your code. As it was indicated, your second while loop:
while (n >= 0 || n <= 100) {}
Is infinite. Because an OR evaluation only needs one part to evaluate to TRUE, any number entered will allow the loop to continue. Obviously, the intent was not allow values greater than 100. However, entering 150 allows the loop to continue because 150 >= 0. Likewise, -90 also allows the loop to continue because -90 <= 100. This is when pseudocode helps when you are learning. You wanted to express "a VALUE between lower_limit AND upper_limit." If you reverse the logic to evaluate values outside the limit, then you can say " value below lower_limit OR above upper_limit." These pseudocode expressions are very helpful determining which logical operator you need.
I also took the liberty to add a message to prompt the user for a value. Your program expects the user to enter two numbers (count and value) but only one prompt message is provided; unless they enter an out of range value.
extract even numbers from arrayList
ArrayList numberList = new ArrayList<>(Arrays.asList(1,2,3,4,5,6));
numberList.stream().filter(i -> i % 2 == 0).forEach(System.out::println);