when i run this code and enter first grade 4 and second is 3 the count should be 2 and the avareg is 3
why the result do not be that?
public static void main (String args[]){
Scanner input = new Scanner (System.in);
System.out.println("enter your grade or -1 to exit ");
int grade = 0 , sum = 0 , count = 1;
while (count <= 5 && grade != -1) {
System.out.println("the grade no " + count);
grade = input.nextInt();
sum += grade;
count++;
}
System.out.println("the avareg is = " + sum/count);
System.out.println(sum);
System.out.println(count);
}
Count start at one and it should start at 0.
At first input, count become 2 and at second input it becomes 3.
I did the folowing changes and it works :
public class myClass {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println("enter your grade or -1 to exit ");
int grade = 0 , sum = 0 ,count = 1 ;
for (; count<=5 && grade!=-1 ;count++) {
System.out.println("Enter " + count + ": ");
grade = input.nextInt();
sum = sum+grade ;
}
System.out.println("the avareg is = " + (sum+1)/(count-2));
System.out.println("sum= "+(sum+1));
System.out.println("count= "+(count-2));
}
}
I just add 1 to sum and taked 2 from count
Related
Here I am using a do while loop to execute a program that allows the user to score student scores in an array and then print them. I am struggling with implementing a system that sends an error message when the user inputs a number below 0 or above 100 and then lets the user try again.
import java.util.Scanner;
public class storeScore {
public static void main(String [] args) {
Scanner input = new Scanner (System.in);
int scores [] = new int [7];
int numberOfStudents = 7;
//User input all scores. For loop works by asking for user input until it reaches the number as input in numberOfStudents.
do
{
for(int i = 0; i<numberOfStudents; i++) {
scores[i] = input.nextInt();
if (i == 0) {
System.out.print("Enter the score for the 1st student: ");
}
else if (i == 1) {
System.out.print("Enter the score for the 2nd student: ");
}
else if (i == 2) {
System.out.print("Enter the score for the 3rd student: ");
}
else if (i >= 3) {
System.out.print("Enter the score for the " + (i+1) + "th student: ");
}
}
//Error output if input is incorrect
}
while (scores[i] < 0 || scores[i] > 100) {
System.out.println("Input out of bounds. Score can only be between 0 and 100");
}
//Printing all scores.
System.out.println("Thank you for your input. Your entered scores are: ");
for (int i=0; i<numberOfStudents; i++)
{
System.out.print(scores[i] + ", ");
}
input.close();
}
}
You have your error message printing in a while loop without any way of breaking out of that loop.
Try capturing your user's input as an int variable first that you can check to see if it's valid before assigning it to scores[].
for(int i = 0; i<numberOfStudents; i++) {
String message;
if (i == 0) {
message = "Enter the score for the 1st student: ";
}
else if (i == 1) {
message = "Enter the score for the 2nd student: ";
}
else if (i == 2) {
message = "Enter the score for the 3rd student: ";
}
else if (i >= 3) {
message = "Enter the score for the " + (i + 1) + " student: ";
}
System.out.println(message);
int score = input.nextInt();
//Now check if the input value is valid
while (score < 0 || score > 100) {
System.out.println("Input out of bounds. Score can only be between 0 and 100");
System.out.println(message);
score = input.nextInt();
}
score[i] = score;
}
Now, the loop will print the message for student i, then take in the score. If the score is invalid, the while loop will print out the error message, and take input again. If the input is valid this time, the while loop breaks and assigns the new score to student i, otherwise it reprints the error message and takes input again.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter number (0 to quit): ");
//largest to-do
double largest = scan.nextDouble();
double count = 0;
while (largest != 0) {
double input = scan.nextDouble();
//max
if (input > largest) {
// not zero
// while (input > largest){
//
// }
largest = input;
//counter
count = 0;
}
//counter start
if(input==largest){
count++;
}
if (input == 0) {
System.out.println("Largest #: " + largest);
System.out.println("Occurance: " + count);
}
}
}
}
This program works! however, its not that complete I think...
Like if a user tries to enter
-17 -5 -2 -1 -1 -1 0
it outputs:
Max: 0
Occurrence: 1
Though: in my mind I want it to be:
Max: -1
Occurrence: 3
how would I do this WITHOUT using arrays? I know its in that part of the code I started above the counter.
Thanks in advance.
Why not something like that?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter number (0 to quit): ");
double largest = 0, nextDouble;
int count = 0;
while ((nextDouble = scan.nextDouble()) != 0) {
if (nextDouble > largest || largest == 0) {
largest = nextDouble;
count = 1;
}
else if (nextDouble == largest) count++;
}
scan.close();
if (count == 0) {
System.out.println("No number entered!");
return;
}
System.out.println("Largest #: " + largest);
System.out.println("Occurance: " + count);
}
}
My program has no syntax error, I can input all the value, but I just can't get the final average number right. Can anyone help me find out the problem?
The following is what I input:
How many employees do you have? 4
How many days was Employee #1 absent? 4
How many days was Employee #2 absent? 2
How many days was Employee #3 absent? 1
How many days was Employee #4 absent? 3
Final answer should be: 2.5
This is the code I use:
import java.util.Scanner;
class Number {
public static void main(String[] args) {
int numEmployee = Number.workers();
int absentSum = Number.totaldays(numEmployee);
double averageAbsent = Number.average(numEmployee, absentSum);
}
public static int workers() {
int number = 0;
Scanner input = new Scanner(System.in);
while (number > 0 || number < 0 || number == 0) {
System.out.println("How many employees do you have?");
number = input.nextInt();
if (number >= 0) {
return number;
} else {
System.out
.println("You can not enter a negative number."
+ " Please enter another number.");
}
}
return number;
}
public static int totaldays(int numEmployee) {
int absentDays = 0;
int absentSum = 0;
for (int employName = 1; employName <= numEmployee; employName++) {
System.out.println("How many days was Employee #" + employName
+ " absent?");
Scanner input = new Scanner(System.in);
absentDays = input.nextInt();
while (absentDays < 0) {
System.out.println("You can not enter a negative number."
+ " Please enter another number.");
System.out.println("How many days was Employee #" + employName
+ " absent?");
absentDays = input.nextInt();
}
absentSum += absentDays;
}
return absentSum;
}
public static double average(int numEmployee, int absentSum) {
double averageAbsent = (double) absentSum / (double) numEmployee;
System.out.println("Your employees averaged " + averageAbsent
+ " days absent.");
return averageAbsent;
}
}
Move absentSum += absentDays; into the loop body in totaldays. If you restrict the visibility of absentSum then the compiler will tell you that you are accessing it out of scope. Something like
public static int totaldays(int numEmployee) {
int absentSum = 0;
for (int employName = 1; employName <= numEmployee; employName++) {
System.out.println("How many days was Employee #" + employName
+ " absent?");
Scanner input = new Scanner(System.in);
int absentDays = input.nextInt();
while (absentDays < 0) {
System.out.println("You can not enter a negative number."
+ " Please enter another number.");
System.out.println("How many days was Employee #" + employName
+ " absent?");
absentDays = input.nextInt();
}
absentSum += absentDays;
}
// absentSum += absentDays;
return absentSum;
}
With the above output (and your provided input) I get (the requested)
2.5
Here's my code:
import java.util.*;
public class InputSum
{
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);
System.out.print("Please enter integer values (-1 to to display numbers & sum): ");
int i = input.nextInt();
int j = 0;
while (i != -1)
{
j += i;
i = input.nextInt();
}
System.out.println("Entered Number: " + i);
System.out.println("The Sum: " + j);
}
}
As of now my output is:
Entered Number: -1
The Sum: (Sum of the numbers entered)
Print them inside the loop :
while (i != -1)
{
System.out.println("Entered Number: " + i);
j += i;
i = input.nextInt();
}
System.out.println("The Sum: " + j);
Or it you want to print them in a single line :
List numbers = new ArrayList<Integer>();
while (i != -1)
{
numbers.add(i);
j += i;
i = input.nextInt();
}
System.out.println("Entered Numbers: " + numbers);
System.out.println("\nThe Sum: " + j);
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Integer> numbers = new ArrayList<Integer>();
System.out
.print("Please enter integer values (-1 to to display numbers & sum): ");
int i = input.nextInt();
int j = 0;
while (i != -1) {
numbers.add(i);
j += i;
i = input.nextInt();
}
System.out.println("Entered Numbers: ");
for (int a = 0; a < numbers.size(); a++) {
System.out.print(" " + numbers.get(a));
}
System.out.println("The Sum: " + j);
}
This should work to print the numbers. You can use an arraylist to store the numbers and then if you need them for later calculations they are still stored in the arraylist.
package net.rajkannan.stackoverflow;
import java.util.*;
public class InputSum {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out
.print("Please enter integer values (-1 to to display numbers & sum): ");
int i = input.nextInt();
int j = 0;
String numbers = "";
while (i != -1) {
j += i;
numbers = numbers + i + " ";
i = input.nextInt();
}
System.out.println("Entered Numbers: " + numbers);
System.out.println("The Sum: " + j);
}
}
this one just is hurting my brain. http://programmingbydoing.com/a/adding-values-in-a-loop.html
Write a program that gets several integers from the user. Sum up all the integers they give you. Stop looping when they enter a 0. Display the total at the end.
what ive got so far:
Scanner keyboard = new Scanner(System.in);
System.out.println("i will add");
System.out.print("number: ");
int guess = keyboard.nextInt();
System.out.print("number: ");
int guess2 = keyboard.nextInt();
while(guess != 0 && guess2 != 0)
{
int sum = guess + guess2;
System.out.println("the total so far is " + sum);
System.out.print("number: ");
guess = keyboard.nextInt();
System.out.print("number: ");
guess2 = keyboard.nextInt();
System.out.println("the total so far is " + sum);
}
//System.out.println("the total so far is " + (guess + guess2));
}
Declare the int sum variable outside of the while loop and only have one guess = keyboard.nextInt() inside the loop. Add the user's guess to the sum in the loop as well.
Then after the loop output the user's sum.
I.e.:
int sum;
while(guess != 0)
{
guess = keyboard.nextInt();
sum += guess;
}
System.out.println("Total: " + sum");
Edit: also remove the guess2 variable, as you will no longer need it.
The code will be as below :
public static void main(String[] args) throws Exception {
Scanner keyboard = new Scanner(System.in);
int input = 0;
int total = 0;
System.out.println("Start entering the number");
while((input=keyboard.nextInt()) != 0)
{
total = input + total;
}
System.out.println("The program exist because 0 is entered and sum is "+total);
}
Programming by Doing :)
int x = 0;
int sum = 0;
System.out.println("I will add up the numbers you give me.");
System.out.print("Number: ");
x = keyboard.nextInt();
while (x != 0) {
sum = x + sum;
System.out.println("The total so far is " + sum + ".");
System.out.print("Number: ");
x = keyboard.nextInt();
}
System.out.println("\nThe total is " + sum + ".");
import java.util.Scanner;
public class AddingInLoop {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int number, total = 0;
System.out.print("Enter a number\n> ");
number = keyboard.nextInt();
total += number;
while (number != 0) {
System.out.print("Enter another number\n> ");
number = keyboard.nextInt();
total += number;
}
System.out.println("The total is " + total + ".");
}
}
You first prompt the user to enter a number. Then you store that number into total (total += number OR total = total + number). Then, if the number entered wasn't 0, the while loop executes. Every time the user enters a nonzero number, that number is stored in total ( the value in total is getting bigger) and the while loops asks for another number. If and when a user enters 0, the while loop breaks and the program displays the value inside total. :D I myself am a beginner and had a bit of an issue with the logic before figuring it out. Happy coding!