I'm currently a beginner in Java Programming.
I'm having a little bit of trouble with my while loop that has a sentinel value.
Everything works until I add the while loop, however it's required so I can exit when the value '999' is entered.
I'm also not sure what to add for the 'if' values.
Any help would be appreciated!
import java.util.Scanner;
public class SumAverage {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
System.out.printf("Please Enter the first number%n");
int num1 = keyboard.nextInt();
System.out.printf("Please enter the second number%n");
int num2 = keyboard.nextInt();
System.out.printf("Please enter the third number%n");
int num3 = keyboard.nextInt();
int sum = num1 + num2 + num3; //calculates sum
int average = (num1 + num2 + num3) / 3; //calculates average
while (num1 != ) {
if (sum > 1)
System.out.printf("Sum: %s %nAverage: %s", sum, average);
else if (sum < 1)
System.out.printf("Sum: %s %nAverage: %.2f", sum, average);
}
}
}
You can do it in the following manner:
import java.util.Scanner;
public class SumAverage {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
int input,sum,avg,count;
do{
System.out.printf("Please enter number" + (count+1) + "%n");
input = keyboard.nextInt()
sum += input;
count += 1;
avg = sum/count;
}while(input != 999);
//Do whatever is to be done once sentinel is input.
}
You can modify the while loop as follows
while(num1 !=999 || num2 !=999 || num3 !=999)
|| is used as OR operator. So whenever any input value is 999 the loop wont execute.
Related
I am new to coding and have been trying to understand loops. I am working on a sample project that says to Write an application that prompts a user for two integers and displays every integer between them. Display a message if there are no integers between the entered values. Make sure the program works regardless of which entered value is larger. I for the most part have it but am having an issue with the output.
import java.util.Scanner;
public class Practice2 {
public static void main(String[] args) {
int num1;
int num2;
Scanner input= new Scanner(System.in);
System.out.println("Input one integer");
num1= input.nextInt();
System.out.println("Enter another integer");
num2= input.nextInt();
while(num1<num2) {
num1 += 1;
System.out.println(num1);
}
while(num2<num1){
num2+=1;
System.out.println(num2);
}
}
}
A sample output would be
Input one integer
1
Enter another integer
9
Then this is the output
2
3
4
5
6
7
8
9
As you are incrementing value first so you can try this approach
import java.util.Scanner;
public class Practice2 {
public static void main(String[] args) {
int num1;
int num2;
Scanner input= new Scanner(System.in);
System.out.println("Input one integer");
num1= input.nextInt();
System.out.println("Enter another integer");
num2= input.nextInt();
while(num1<num2-1) {
num1 += 1;
System.out.println(num1);
}
while(num2<num1-1){
num2+=1;
System.out.println(num2);
}
}
}
You can use a for-loop for this purpose. Though you would need to include some logic to ensure that num1 is the smaller of the two numbers.
Note the num1 + 1 is there to make the first number non-inclusive.
A for-loop is broken down into 3 components
start: i = num1 + 1
condition: i <= num2
ensure i is less than or equal to num2
action: i++
after each iteration, i will be incremented by 1
import java.util.Scanner;
public class Practice2 {
public static void main(String[] args) {
int num1;
int num2;
Scanner input= new Scanner(System.in);
System.out.println("Input one integer");
num1 = input.nextInt();
System.out.println("Enter another integer");
num2 = input.nextInt();
for (int i = num1 + 1; i <= num2; i++) {
System.out.println(num2);
}
}
}
I used this method to get a perfect score.
import java.util.Scanner;
public class Inbetween {
public static void main (String args[]) {
Scanner input = new Scanner(System.in);
int x, y, X, Y;
X = input.nextInt();
Y = input.nextInt();
if (Y+1 == X || X+1 == Y ){
System.out.print("There are no integers between " + X + " and " + Y);
}else if (Y>X){
for(x=X+1; x<Y; ++x)
System.out.print(x+" ");
}else if (X>Y){
for(y=Y+1; y<X; ++y)
System.out.print(y+" ");
}
}
}
Am trying to write simple program to find average of 3 int values, but it always seems to get the average wrong
import java.util.Scanner;
public class IntegerAverage {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int num1;
int num2;
int num3;
// Ask user
System.out.println("Input 1st number");
num1 = in.nextInt();
System.out.println("Input 2nd number");
num2 = in.nextInt();
System.out.println("Input 3rd number");
num3 = in.nextInt();
// Print answer
System.out.println("The average is:"+ ((num1 + num1 + num3)/3));
}
}
Help is greatly appreciate
Besides having num1 twice and missing num2, as already mentioned above, you will always get integer-results only, e.g. for num1 = 1, num2 = 2 and num3 = 2 your code will plot 1 as result instead of 1.667. You might enforce double-results in your output by writing
System.out.println("The average is:"+ ((num1 + num2 + num3) / 3.0));
It's supposed to be:
import java.util.Scanner;
public class IntegerAverage {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int num1;
int num2;
int num3;
// Ask user
System.out.println("Input 1st number");
num1 = in.nextInt();
System.out.println("Input 2nd number");
num2 = in.nextInt();
System.out.println("Input 3rd number");
num3 = in.nextInt();
// Print answer
System.out.println("The average is:"+ ((num1 + num2 + num3)/3));
}
}
You accidentally added num1 twice, and forgot num2.
Write a class that asks for a whole number num make sure it is greater than 0 (data validation), and that calculates and returns the sum of the following series of numbers:
1/num + 2/num-1 + 3/num-2 + …. (num-1)/2 + num/1
Don't get caught in integer division!
Test your method by invoking it with num = 2; you will expect the result to be 2.5.
public class LengthSeries
{
public static void main (String[] args)
{
//variable declarations
Scanner keyboard = new Scanner(System.in);
int num;
int sum = 0; // sum
int ctr = 1 ; //counter
//Accept the required data
System.out.print ("Enter a whole number greater than 0 : ");
num = keyboard.nextInt();
//Process the data in order to determine data
while ( num <= 0){
System.out.println ("Sorry number invalid");
System.out.println ("please write a whole number greater than 0 : ");
num = keyboard.nextInt();
}
while (num <= 0){
sum += (ctr / num);
num -= ctr;
ctr++;
}
//Display the output
System.out.println ();
System.out.println ("Your sum is: " +sum);
System.out.println ("\n\nWritten by: KinnahRose Lopez");
} //end main method
} //end LengthSeries class
import java.util.Scanner;`enter code here`
public class Test
{
public static void main (String[] args)
{
//variable declarations
Scanner keyboard = new Scanner(System.in);
int num;
double sum = 0; // sum
//Accept the required data
System.out.print ("Enter a whole number greater than 0 : ");
num = keyboard.nextInt();
//Process the data in order to determine data
while ( num <= 0){
System.out.println ("Sorry number invalid");
System.out.println ("please write a whole number greater than 0 : ");
num = keyboard.nextInt();
}
int temp = num;
for (double i = 1; i <= num; i++)
{
sum = sum + (i/temp);
temp--;
}
//Display the output
System.out.println ();
System.out.println ("Your sum is: " +sum);
System.out.println ("\n\nWritten by: KinnahRose Lopez");
} //end main method
} //end LengthSeries class
You can try this:
public static void main(String[] args) throws Exception {
Scanner keyboard = new Scanner(System.in);
System.out.print ("Enter a whole number greater than 0 : ");
int num = keyboard.nextInt();
while (num<1) {
System.out.print ("Enter a whole number greater than 0 : ");
num = keyboard.nextInt();
}
double sum = 0;
double d = num; // convert num to double to get the correct result.
for (int i = 1; i <= num; i++) {
sum += i/(d-(i-1));
}
System.out.println("sum = " + String.format("%.2f", sum));
}
Example:
Enter a whole number greater than 0 : 2
sum = 2.50
double sum =0;
for(double i=1.0; i<= num; i+=1.0) {
sum+=i/(num-i-1);
}
I have to write a program that asks the user to enter an integer value. After each value, the user has to respond with a "y" or a "n" if he/she wants to continue with the program, and each number the user enters is stated as either odd or even.
I have done this so far with a do-while loop, but I am confused on how to get the averages of the values the user enters. How would you get the average for all the numbers entered?
Here is my code so far:
import java.util.Scanner;
class ProgramTest {
public static void main(String[] args) {
String answer = "";
do {
int num, count = 0;
Scanner scan = new Scanner(System. in );
System.out.print("Enter any number : ");
num = scan.nextInt();
if ((num % 2) == 0) System.out.println(num + " is an even number.");
else System.out.println(num + " is an odd number");
System.out.println("do you want to continue?");
answer = scan.next();
count++;
} while (answer.equals("y"));
}
}
From the Question looks like following things need to handled,
haven't add mechanism for addition into single variable.
put all variable to out from do...while loop body...
created additional variable according to requirement.
see all this things covered by me with following code snippet.
do something likewise,
String answer = "";
double sum = 0; // use for storing addition to all entered values..
int num, count = 0;
Scanner scan = new Scanner(System.in);
do {
System.out.print("Enter any number : ");
num = scan.nextInt(); // getting input from user through console
sum = sum + num; // add every input number into sum-variable
if ((num % 2) == 0) System.out.println(num + " is an even number.");
else System.out.println(num + " is an odd number");
System.out.println("do you want to continue?");
answer = scan.next(); // ask for still want to repeat..
count++;
} while (answer.equals("y"));
System.out.println("Average is : " + sum + "/" + count + " = "+ (sum /count));
In order to calculate Average, you need 2 things: Sum of all numbers and Count of all numbers involved in the Average calculation.
Your sum and count which involved in the Average calculation needs to be out of the do..while scope in order for them to be known at the calculation stage.
I also took the liberty of fixing your code a little bit
import java.util.Scanner;
class ProgramTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System. in );
int count = 0;
int sum = 0;
String answer = "";
do {
System.out.print("Enter any number : ");
int num = scan.nextInt();
boolean isEven = (num % 2 == 0);
System.out.println(num + " is an " + (isEven ? "even" : "odd") + " number.");
sum += num;
System.out.println("do you want to continue?");
answer = scan.next();
count++;
} while (answer.toLowerCase().equals("y"));
System.out.println("Average: " + (sum/count));
}
}
Change your code like this:
import java.util.Scanner;
class ProgramTest {
public static void main(String[] args) {
String answer = "";
int avr =0;
int num, count = 0;
do {
Scanner scan = new Scanner(System. in );
System.out.print("Enter any number : ");
num = scan.nextInt();
if ((num % 2) == 0) System.out.println(num + " is an even number.");
else System.out.println(num + " is an odd number");
System.out.println("do you want to continue?");
avr += num;
answer = scan.next();
count++;
} while (answer.equals("y"));
avr = avr /count;
System.out.println("The avreage of value is:" + avr );
}
}
avr is average. that means when you input an integer. we add num and avr . and when finish looping. we divideto count. like this:
1-5-9-11
avr = 1+5+9+11;
count = 4;
avr = avr/4;
I have a little averaging program I have made and, I am trying to only allow it to take in numbers. Everything else works but, I can't seem to figure it out. I am still learning so, any advise or pointers would be awesome!
Here is my code.
import java.util.Scanner;
public class THISISATEST {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int sum = 0;
int count = 0;
int i = 1;
while (i <= 10) {
i++;
{
System.out.print("Enter the test score: ");
int tS = keyboard.nextInt();
count++;
sum = (sum + tS);
}
System.out.println(sum);
}
System.out.println("The Average is = " + sum / count);
}
}
Inside your while loop use the following code:
System.out.print("Enter the test score: ");
while (!keyboard.hasNextInt()) {//Will run till an integer input is found
System.out.println("Only number input is allowed!");
System.out.print("Enter the test score: ");
keyboard.next();
}
int tS = keyboard.nextInt();
//If input is a valid int value then the above while loop would not be executed
//but it will be assigned to your variable 'int ts'
count++;
sum = (sum + tS);