Here is what I have to do:
"Write a segment of code that reads a sequence of integers from the keyboard until the user enters a negative number. It should then output a count of the number of even integers and the number of odd integers read (not including the final negative value in either count). Remember - 0 is an even number. For example, if the sequence is:
2
7
15
5
88
1243
104
-1
Then the output should be
Number of even integers: 3
Number of odd integers: 4
My code just keeps going even after inputting -1. I have a feeling I am missing a {somewhere or wrote the code wrong. Here is my code:
int oddCount = 0, evenCount = 0;
Scanner in = new Scanner(System.in);
while (oddCount>=0&&evenCount>=0){
System.out.print("Enter an integer: ");
int temp = in.nextInt();
if (temp>0) {
if (temp%2==0)
evenCount = evenCount + 1;
else oddCount = oddCount + 1;
while (temp>0);
System.out.println("Number of even integers: "+evenCount);
System.out.println("Number of odd integers: " +oddCount);
}
}
I think using do While loop will also prevent your isses,
int oddCount = 0;
int evenCount = 0;
Scanner in = new Scanner(System.in);
do {
System.out.print("Enter an integer: ");
int temp = in.nextInt();
if (temp > 0) {
(temp % 2==0)? evenCount++:oddCount++;
}
} while (temp > 0);
Your loop will always continue because while (oddCount>=0&&evenCount>=0) will always be true in your case. Try it like this:
int oddCount = 0, evenCount = 0;
Scanner in = new Scanner(System.in);
boolean continue = true;
while (continue){
System.out.print("Enter an integer: ");
int temp = in.nextInt();
if (temp>0) {
if (temp%2==0)
evenCount++;
else
oddCount++;
}
else {
System.out.println("Number of even integers: "+evenCount);
System.out.println("Number of odd integers: " +oddCount);
continue = false;
}
}
Your code reads just one integer, then stuck at this line: while (temp>0);
The code below solve your problem:
int oddCount = 0, evenCount = 0, temp;
Scanner in = new Scanner(System.in);
System.out.print("Enter an integer: ");
while (true) {
temp = in.nextInt();
if (temp < 0) {
break;
} else if (temp % 2 == 0) {
evenCount = evenCount + 1;
} else {
oddCount = oddCount + 1;
}
}
System.out.println("Number of even integers: " + evenCount);
System.out.println("Number of odd integers: " + oddCount);
There are a few problems here.
First, since you never decrease oddCount and evenCount, the condition oddCount>=0&&evenCount>=0 will always be true.
Second, you have an empty loop that loops endlessly, since it's condition is true and it has no body: while (temp>0);
I'd just take out the first loop, which is redundant, and use a do while loop:
int oddCount = 0;
int evenCount = 0;
Scanner in = new Scanner(System.in);
do {
System.out.print("Enter an integer: ");
int temp = in.nextInt();
if (temp > 0) {
if (temp % 2==0) {
evenCount++;
} else {
oddCount++;
}
}
} while (temp > 0);
System.out.println("Number of even integers: " + evenCount);
System.out.println("Number of odd integers: " + oddCount);
Try this..
Scanner s=new Scanner(System.in);
int evenCount=0, oddCount=0;
while(true)
{
System.out.println("Enter a number");
int n=s.nextInt();
if(n<0) break;
if(n%2==0) evenCount+=n;
else oddCount+=n;
}
System.out.println("even count "+evenCount);
System.out.println("odd count "+oddCount);
Related
I've had a ton of issues with this program, added a while hasNextInt, multiple other things and i've finally got the code working, but my output is just the first input, instead of max/avg of the inputs. Could somebody let me know where i'm messing up?
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int count = 0, max = 0, total = 0;
int num = scnr.nextInt();
if (num >= 0) {
count++;
total += num;
max = Math.max(max, num);
num = scnr.nextInt();
}
int avg = count == 0 ? 0 : total/count;
System.out.println(avg + " " + max);
}
}
you are not using a loop to get numbers from the console. Also, logic for avg may result in the wrong answer. find below solution.
Scanner scnr = new Scanner(System.in);
int count = 0, max = 0, total = 0;
System.out.println("Enter any other characters expect numbers to terminate:");
while(scnr.hasNextInt()) {
int num = scnr.nextInt();
if (num >= 0) {
count++;
total += num;
max = Math.max(max, num);
}
}
double avg = count == 0 ? 0 : (double)total/(double)count; // to print correct avg
System.out.println(avg + " " + max);
sample Output:
Enter any other characters expect numbers to terminate:
3
3
34
a
13.333333333333334 34
public class Ex51 {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Enter as many integers as you wish. Enter 0 to stop." + "\nReturns "
+ "\nNumber of negative integers input. " + "\nNumber of positive integers input." + "\nTotal sum."
+ "\nMean.");
int i = 0;
int sum = 0;
int temp = 0;
int minusCount = 0;
int plusCount = 0;
float mean = 0;
boolean isZero = false;
do {
System.out.print("Enter an integer: ");
temp = input.nextInt();
if (temp == 0 && i == 0) {
isZero = true;
break;
} else if (temp < 0) {
minusCount++;
} else if (temp > 0)
plusCount++;
if (temp != 0) {
i++;
sum += temp;
mean = (float) sum / (float) i;
}
} while (temp != 0);
if (!isZero) {
System.out.printf("\nThe number of positives is: %d" + "\nThe number of negatives is: %d"
+ "\nThe total is: %d" + "\nThe average is: %.2f", plusCount, minusCount, sum, mean);
} else {
System.out.println("No numbers are entered except 0.");
}
}
}
And this is a sample run:
Enter as many integers as you wish. Enter 0 to stop. Returns
Number of negative integers input. Number of positive integers
input. Total sum. Mean. Enter an integer: 8 Enter
an integer: 6 Enter an integer: 20 Enter an integer: -2
Enter an integer: 0 The number of positives is: 3 The
number of negatives is: 1 The total is: 32 The average is:
8$00
My problem is right at the bottom: 8$00
When I specify %.2f for the float with no special characters in between.
The desired output would be "8.00" for arg mean.
I posted all of the code because I think I would make obvious any beginner mistakes fairly obvious and if not is there any issues with my settings.
I'm running this on Eclipse with jre9.
You are missing a number before '.' for example 2.2f.
For the rest of the code, for initializing you can do it all in one line. The last if statement is useless because you already verified in the first if it is 0, so you dont need a condition for this one. And you will not get there anyway because your while condition is that temp is not 0.
For the mean you can do:
mean = (float)(sum/i);
I think you are just complecating your life.
you could do this:
int temp = 1;
int positive = 0;
int negative = 0;
int i = 0;
int sum = 0;
while (temp != 0)
{
System.out.print("Please enter an integer:");
temp = input.nextInt();
if(temp == 0)
//your print statement here
else if(temp > 0)
positive ++;
else
negative ++;
sum += temp;
i++;
}
I'm trying to Write the main method of a Java program that has the user enter two integers, i and n. If either integer is less than 2, output “Please enter numbers above 1.” Otherwise, output the n positive multiples of i, separated by spaces.
I'm close but can't figure out how to do display the multiples.
Here's what a sample run should look like:
Enter i: 4
Enter n: 6
6 multiples of 4 are: 8 12 16 20 24 28
import java.util.*;
public class HW5Problem3 {
public static void main (String [] args) {
int i = 0;
int n = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter i: ");
i = input.nextInt();
System.out.print("Enter n: ");
n = input.nextInt();
if ((i <= 1) || (n <= 1)) {
System.out.println("Please enter numbers above 1");
System.exit(1);
}
System.out.print(n + " multiples of " + i + " are: ");
}
}
import java.util.Scanner;
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int i = 0;
int n = 0;
//It's use to verify the inputs (i and n).
do{
System.out.print("Enter i :");
i = input.nextInt();
System.out.print("\nEnter n :");
n = input.nextInt();
if(i >= 1 || n <= 1){
System.out.println("Please enter numbers above 1 \n");
}
}while(i <= 1 || n <= 1);
System.out.print(n + " multiples of " + i + " are: ");
for (int counter = 0 ; counter < n ; counter++) {
System.out.print(i*(2 + counter) + " ");
}
}
You'll need to create a loop (for loop or while loop) to iterate from 2 to n+1, and multiply i by your loop variable, outputting each value inside the loop
U can use following method in that class
public static void mult(int i,int n){
int[] arr=new int[n];
int count=2;
for(int x=0;x<n;x++){
arr[x]=i*count++;
}
for(int y=0;y<arr.length;y++){
System.out.print(arr[y]+" ");
}
and now your final code looks like
import java.util.*;
public class HW5Problem3 {
private int i = 0;
private int n = 0;
public static void main(String[] args) {
int i = 0;
int n = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter i: ");
i = input.nextInt();
System.out.print("Enter n: ");
n = input.nextInt();
if ((i <= 1) || (n <= 1)) {
System.out.println("Please enter numbers above 1");
System.exit(1);
} else {
System.out.print(n + " multiples of " + i + " are: ");
mult(i, n);
}
}
public static void mult(int i, int n) {
int[] arr = new int[n];
int count = 2;
for (int x = 0; x < n; x++) {
arr[x] = i * count++;
}
for (int y = 0; y < arr.length; y++) {
System.out.print(arr[y] + " ");
}
}
}
n is the multiplier, i is the factor, right? In programming the multiplier is the loop maximum:
System.out.print(n + " multiples of " + i + " are: ");
for (int inc=1; inc<=n; inc++) {
System.out.print(" " + i*inc);
}
This prints out: 4 8 12 16 20 24
If you really want to have this as output: 8 12 16 20 24 28 copy/paste this line:
for (int inc=2; inc<=(n+1); inc++)
my problem is the following. If I input number 2, the code counts it as an odd number.
Remainder for 2 / 2 = 0 so the error doesn't make sense.
Below is the program:
import java.util.Scanner;
public class Ohjelma {
public static void main(String[] args) {
// Tänne voit kirjoittaa ohjelmakoodia. Ohjelmasi voit ajaa
// valitsemalla menusta Run->Run File tai painamalla Shift+F6
Scanner reader = new Scanner(System.in);
System.out.println("Type numbers: ");
int number = Integer.parseInt(reader.nextLine());
int sum = 0;
int many = 0;
double average = 0;
int even = 0;
int odd = 0;
while (number != -1) {
System.out.println("Type numbers: ");
sum = sum + number;
number = Integer.parseInt(reader.nextLine());
many++;
average = (double)sum / many;
if (number%2 == 0) {
even++;
} else {
odd++;
}
}
System.out.println("Thank you and see you later!");
System.out.println("The sum is " + sum);
System.out.println("How many numbers: " + many);
System.out.println("Average: " + average);
System.out.println("Even numbers: " + even);
System.out.println("Odd numbers: " + odd);
The main problem is that for the critical part of your program it largely ignores the first input, apart from adding it to the running sum. You want to recast it like this:
Scanner reader = new Scanner(System.in);
int sum = 0;
int many = 0;
double average = 0;
int even = 0;
int odd = 0;
do {
System.out.println("Type numbers: ");
number = Integer.parseInt(reader.nextLine());
if (number == -1)
break;
sum = sum + number;
many++;
average = (double)sum / many;
if (number%2 == 0) {
even++;
} else {
odd++;
}
} while (true);
This will certainly processes even and odd numbers correctly.
Your code reads the second line of input into number before it checks whether number is odd ... and -1 is odd.
I am doing an assignment and have most of what i think will work down, i have to promt the user for random numbers, then display how many negatives as well as positives then sum and average, can anyone help me as i cannot get my code to display the correct amount of values for negative or positive or get it to sum. Here is what i have so far( i also added the break in the end , otherwise it went to an infinate loop)
//Random number evaluation
package chapter_4;
import java.util.Scanner;
public class Four_One {
public static void main(String[] args) {
int positive = 0;
int negative = 0;
int sum = 0;
int count = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter in a value, if 0 is entered program stops: ");
int data = input.nextInt();
while (data != 0) {
sum += data;
if (data < 0)
negative++;
else if (data > 0)
positive++;
count++;
System.out.println("The number of positives is: " + positive);
System.out.println("The number of negatives is:" + negative);
System.out.println("The total is: " + sum);
System.out.println("The average is: " + sum / data);
break; }
}
}
You are not asking for the nextInt() within the while loop - you do want to ask more than once, correct?
Your average is not an average. I suggest dividing by count instead of data.
at the end of the loop (instead of the break) add data = input.nextInt();
btw for average you should display sum/(double)count (the cast to double is there so you'll see the fraction)
This might help you.
Explanation: Using a do-while loop might be easier because it asks for input and than checks the input. Also put the input statement inside the do-While loop so it asks for multiple inputs.
public static void main(String[] args)
{
int positive = 0;
int negative = 0;
int sum = 0;
int count = 0;
Scanner input = new Scanner(System.in);
int data = 0;
do
{
System.out.print("Enter in a value, if 0 is entered program stops: ");
data = input.nextInt();
sum += data;
count ++;
if(data < 0)
negative ++;
else if(data > 0)
positive ++;
}
//Stops if the value of data is ZERO(0) and continues if it's not
while(data != 0);
System.out.println("Positive Numbers = " + positive);
System.out.println("Negative Numbers = " + negative);
System.out.println("Sum of Numbers = " + sum);
System.out.println("Total Numbers = " + count);
}
import java.util.Scanner;
public class Four_One {
public static void main(String[] args) {
int positive = 0;
int negative = 0;
int sum = 0;
int count = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter in a value, if 0 is entered program stops: ");
int data = input.nextInt();
while (data != 0) {
sum += data;
if (data < 0)
negative++;
else if (data > 0)
positive++;
count++;
data = input.nextInt();
}
System.out.println("The number of positives is: " + positive);
System.out.println("The number of negatives is:" + negative);
System.out.println("The total is: " + sum);
System.out.println("The average is: " + sum / (double)count);
}
}