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);
}
}
Related
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++;
}
Here is the code but it's not optimized for all the exceptions. For example, if the user types 0 from the beginning, it will give the division by zero error and also I don't know why the sum of the numbers work.
Basically, I make num = 1 so it's different than 0 so the while loop can start, but in the end, I made sum = sum - 1; but it gave a wrong number.
Please help me.
import java.util.Scanner;
class Main {
public static void main (String[] args) {
int count = 0;
int num = 1;
double average = 0.0;
int soma = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Type a integer. 0 to exit.");
while (num != 0) {
num = scanner.nextInt();
sum = sum + num;
count++;
}
if (num == 0) {
average = sum / (count -1);
System.out.println("The sum of the numbers is: "+ soma);
System.out.println("The average of the numbers is: "+ media);
}
}
}
change while to do while:
do {
num = scanner.nextInt();
sum = sum + num;
count++;
}while (num != 0);
Change FROM:
average = sum / (count -1);
System.out.println("The sum of the numbers is: "+ soma);
System.out.println("The average of the numbers is: "+ media);
TO:
average = sum / (count);
System.out.println("The sum of the numbers is: "+ sum);
System.out.println("The average of the numbers is: "+ average);
it should work
I need to write a code that takes the users input of numbers and adds them, displays the amount of positives, negatives, zeroes, and the count of the amount of numbers inputted once the user enters the letter 'e'.
When i enter 'e' though, the program terminates without printing the count or average.
Also, the average doesn't work because it says i cannot divide by zero.
public static void main (String[] args){
Scanner input = new Scanner(System.in);
int negative = 0;
int positive = 0;
int zeroes = 0;
int sum = 0;
int count = 1;
int average = sum / (count - 1);
do{
System.out.print("Enter a float or 'e' to exit");
String entered = input.nextLine();
if("e".equals(entered)){
//print stuff
break;
}else{
int num;
try {
num = Integer.parseInt(entered);
} catch (NumberFormatException e) {
System.out.print(negative + positive +
zeroes + sum + (count - 1) + average);
continue; // re-do the loop
}
if(num < 0){
sum += num;
count++;
negative++;
}else if (num > 0){
sum += num;
count++;
positive++;
}else{//similar to comment above
sum += num;
count++;
zeroes++;
}
}
} while(true);
}
}
You are missing the print in "if" condition. Modify the if condition as follows
if("e".equals(entered)){
//print stuff
System.out.print(negative + positive + zeroes + sum + (count - 1) + average);
break;
}
As others people have commented, you need to consider cases like "divide by zero" and also handle input validation.
(Count positive and negative numbers and compute the average of numbers) Write
a program that reads an unspecified number of integers, determines how many
positive and negative values have been read, and computes the total and average of
the input values (not counting zeros). Your program ends with the input 0. Display
the average as a floating-point number. Here is a sample run:
Enter an integer, the input ends if it is 0: 1 2 -1 3 0
The number of positives is 3
The number of negatives is 1
The total is 5.0
The average is 1.25
I have 2 major issues. 1) I cannot get the loop to stop. 2) even if I did, the average comes up short. Using the example above, my average is always 1.0, not 1.25 as it should be. It's like the program is reading 5 numbers total instead of the 4 numbers that equate to 5. What is seen in the code below is all I can use: Bare basics of java...
import java.util.Scanner;
public class NewClass {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int positive = 0, negative = 0, total = 0, count = 0;
float average;
System.out.println("Enter the number: ");
int number = input.nextInt();
while(number != 0) {
total += number;
count++;
if(number > 0){
positive++;
}
else if(number < 0){
negative++;
}
average = total / 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 "+ total);
System.out.println("The average is "+ average);
}
}
}
You need to read more numbers. You read one value before your loop. You could do something like
int number;
while((number = input.nextInt()) != 0) {
total += number;
count++;
if(number > 0){
positive++;
} else if(number < 0) {
negative++;
}
} // <-- end loop body.
float average = total / (float) count; // <-- not integer math.
System.out.println("The number of positives is " + positive);
System.out.println("The number of negatives is " + negative);
System.out.println("The total is " + total);
System.out.println("The average is " + average);
You should just use the do while.
import java.util.*;
public class PosAndNeg {
public static void main(String [] args){
Scanner input = new Scanner(System.in);
int positive = 0, negative = 0, total = 0, count = 0;
int number;
float average;
System.out.println("Enter the number: ");
do { number = input.nextInt();
total += number;
count++;
if(number > 0){
positive++;
}
else if(number < 0){
negative++;
}
}
while(number != 0);
average = total / 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 "+ total);
System.out.println("The average is "+ average);
}
}
// use type conversion while evaluating average
import java.util.Scanner;
public class newClass{
public static void main(String[] args){
int pos=0;
int neg=0;
int total=0;
Scanner sc= new Scanner(System.in);
System.out.println("enter the total numbers user want to enter");
int i=sc.nextInt();
int [] num= new int[i];
System.out.println("Please enter number");
for (int j = 0; j < num.length; j++)
{
num[j] = sc.nextInt();
if(num[j]>0)
pos++;
else
neg++;
total=total+num[j];
}
System.out.println(num.length);
double avg =(float)total/(num.length);
System.out.println("positive count="+pos+"negative count="+neg+" average ="+avg);
}
}
import java.util.Scanner;
public class NewClass{
public static void main(String[] args) {
int N=0,p=0,t=0;
System.out.println("enter
integers");
Scanner obj=new Scanner
(System.in);
int a=obj.nextInt();
while (a!=0){
t+=a;
if (a<0){
N++;
}
else if (a>0){
p++;
}
int b=obj.nextInt();
a=b;
}
System.out.println("the
number of negative
numbers is\t\t"+N);
System.out.println("the
number of positive number
is\t\t"+p);
float l=N+p;
float average=(float) t/l;
System.out.println("the total
is\t\t"+t);
System.out.println("the
average is\t\t"+average);
}
}
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.