asking the user to input ints and then counting them - java

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'. Im not sure if what i have so far is the correct path (it doesn't compile yet) but this is what i have so far:
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 = 0;
do{
System.out.print("Enter a float or 'e' to exit");
int num = input.nextInt();
if(num < 0){
sum += num;
count++;
negative++;
}
if (num > 0){
sum += num;
count++;
positive++;
}
if (num == 0){
sum += num;
count++;
zeroes++;
if (num = e){
System.out.print(sum + count + zeroes + positive + negative);
}
}
} while(true);
}
}

You could do something like this. Please note comments where I've tried to improve:
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.println("Not a number nor e");
continue; // re-do the loop
}
if(num < 0){//; ends the line, not to be used after if condition
sum += num;
count++;
negative++;
}else if (num > 0){ // num bcan be >0 only if its not <0, so use else
sum += num;
count++;
positive++;
}else{//similar to comment above
sum += num;
count++;
zeroes++;
}
}
} while(true);

Related

How can I make a variable count how many times a while loop has run?

I'm required to code a program that, using a do-while loop, will print 25 prime numbers on a new line. I have everything figured out on it, except for printing out exactly 25 numbers. However, the program is only giving me prime numbers up to the number 25, not 25 prime numbers.
I set a variable "count" to increase every time the loop is run, with the same result. Here's my code (sorry for the length, I would shorten it but I'm worried the whole thing might be messed up):
int firstNum;
int secondNum = 1;
int num;
int count = 0;
System.out.println("Sample run: \n\nPrime numbers");
do {
num = 0;
firstNum = 2;
while (firstNum <= secondNum / 2) {
if (secondNum % firstNum == 0) {
num++;
count++;
break;
}
firstNum++;
}
if (num == 0 && secondNum != 1) {
count++;
System.out.print("\n" + secondNum + " ");
}
secondNum++;
} while(count < 25);
}
}
You need to remove count++; inside first if block to get 25 prime numbers.
Updated Code:
int firstNum;
int secondNum = 1;
int num;
int count = 0;
System.out.println("Sample run: \n\nPrime numbers");
do {
num = 0;
firstNum = 2;
while (firstNum <= secondNum / 2) {
if (secondNum % firstNum == 0) {
num++;
// count++; // <--- Remove this.
break;
}
firstNum++;
}
if (num == 0 && secondNum != 1) {
count++;
System.out.println(secondNum); // Use println() for new line.
}
secondNum++;
} while(count < 25);
}
Here's an example showing how you can iterate through some loop a fixed number of times, and separately calculate your next prime number. I'm clearly not calculating any primes here, just showing how you can separate the two concerns of "loop quantity" and "determine the next prime".
public static void main(String[] args) {
int prime = 0;
for (int k = 1; k <= 25; k++) {
prime = findPrimeAfter(prime);
System.out.println("#" + k + ": " + prime);
}
}
private static int findPrimeAfter(int lastPrime) {
// TODO: find the next prime that is greater than "lastPrime"
return 0;
}

Java next 10 even numbers in an array

I have to work on a question for college using arrays this is it:
Write a Java program that will create an array of size 10 and into it put the first 10 even numbers greater than the given user input.
This is what I'm trying but I can't get the right output
import java.util.Scanner;
public class EvenNumbers
{
public static void main (String args[])
{
Scanner scan = new Scanner(System.in);
int array[]=new int[10];
System.out.println("Please enter a number:");
int num = scan.nextInt();
num+=1;
int i=0;
while(i<10)
{
if(num/2==0)
{
array[i]=num;
num++;
i++;
}
else
num++;
}
for(int j=0; j<array.length; j++)
{
System.out.print(array[j]);
}
}
}
You should check num%2==0 instead of num/2==0
And also you can simplify your program by iterating only on even numbers:
num++;
if (num % 2 == 1) // make sure that num is even
num++;
for(int i = 0; i < 10; i++)
{
array[i] = num;
num += 2; // jump to the next even number
}
you can use if (n % 2 == 0) because if (n % 2 == 0) would run if n was even (n can be divided evenly by 2). if (n % 2 == 1) would run if n was odd.the % sign is called mod. Its like dividing and getting the remainder.
Try this :)
import java.util.Scanner;
public class EvenNumbers
{
public static void main (String args[])
{
Scanner scan = new Scanner(System.in);
int array[]=new int[10];
System.out.println("Please enter a number:");
int num = scan.nextInt();
num+=1;
int i=0;
while(i<10)
{
if(num%2==0)
{
array[i]=num;
num++;
i++;
}
else
num++;
}
for(int j=0; j<array.length; j++)
{
System.out.print(array[j]);
}
}
}
Just for fun, you can do it using java-8 IntStream class
IntStream.iterate(++num + num % 2, i -> i + 2).limit(10).forEach(System.out::println);
// ++num; generated numbers should be greater than the given user input
// ++num + num % 2; num variable should be even itself.

asking the user to input numbers and then counting them and finding the average

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.

Finding the minimum integer after entering a series of numbers in a loop

Hi Everyone can someone tell me how can I find the minimum integer for a series of integers in a loop?
I'm using a scanner class to get the input from the user and I implemented a do-while loop since the loop is terminated when the input is 0. then the program outputs the result.
my code so far in the loop is like this
Scanner console = new Scanner(System.in);
int choice = 0;
int sumPositive = 0;
int sumOdd = 0;
int count = 0;
int min=99999999;
do {
choice = console.nextInt();
if (choice < min) {
min = choice;
} // setting the minimum number
if (choice > 0) {
sumPositive += choice;
count++;
} // positive numbers sum and count
if (choice % 2 == 1 && choice > 0) {
sumOdd += choice;
} // odd numbers sum
} while ( choice != 0);
Something like this should work.
public int findLowestNumberInSeries() {
Scanner console = new Scanner(System.in);
int lowest = Integer.MAX_VALUE;
int input;
do {
input = console.nextInt();
if (input < lowest) {
lowest = input;
}
}
while (input != 0);
return lowest;
}
Use if (choice < min&&choice!=0) rather than if (choice < min)
as when choice == 0 then your min becomes 0.
All you have to do is change the int min = 99999999 to int min = Integer.MAX_VALUE and print the value of min at the end. Cheers.

For loop with summing feature

I'm trying to write a for loop that will take a user input, regardless if it is positive or negative, display the input numbers and then display the sum of the numbers. I can get as far as displaying the numbers, but I am having a hard time getting the summing right.
import java.util.Scanner;
public class DisplayandSum {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//Variable declaration
int size;
int Num1;
int count = 0; //LCV
int sum = 0; //Sum of inputs
String newNum; // Declared string for user input
System.out.println("Please enter an integers and press enter.");
newNum = input.next();
Num1 = Integer.parseInt(newNum);
size = newNum.length();
System.out.print("These are your numbers: ");
if (Num1 > 0) //If number is positive
{
for (count = 0; count <= (size - 1); count++) {
System.out.print(newNum.charAt(count) + "");
}
} else if (Num1 < 0) // If number is negative
{
System.out.print("-");
for (Num1 = 1; Num1 <= (size - 1); count++) {
System.out.print(newNum.charAt(count) + "");
}
}
if (Num1 > 0) //If positive sum
{
for (count = 0; count < (size - 1); count++) {
sum = sum + count;
}
} else if (Num1 < 0) // If negative sum
{
for (Num1 = size; Num1 > 2; Num1--) {
Num1 = Math.abs(Num1);
}
sum = sum + Num1 % 10;
Num1 = Num1 / 10;
}
sum = sum + (0 - Num1);
System.out.println("\nand the sum of your digits is: " + sum);
}
}
When you say "sum of the numbers", I assume you actually mean "sum of the digits"?
Your code is more complicated than it needs to be.
// Variable declaration
int sum = 0; //Sum of inputs
String newNum; // Declared string for user input
System.out.println("Please enter an integer and press enter.");
newNum = input.next();
size = newNum.length();
System.out.print("These are your numbers: ");
for (count = 0; count < size; count++)
{
char ch = newNum.charAt(count);
if (Character.isDigit(ch) {
System.out.print(ch);
sum += (ch - '0');
}
}
System.out.println("\nand the sum of your digits is: " + sum);

Categories

Resources