USER INPUT CHALLENGES

 Read 10 numbers from the console entered by the user and print the sum of those numbers.
create a scanner
use hasNextInt() method from the scanner to check if the user has entered an int value
if hasNextInt() returns false, print the message "Invalid Number".Continue reading until you have 10 numbers.
Use the nextInt() method to get the number and add it to the sum
be4 the user enters each num, print the message "Enter number #x:" where x represents the count i.e 1,2,3, etc
For eg the first message printed to the user would be "Enter number #1:", the next "Enter number #2", etc.

hint

  • use while loop.
  • use a counter variable for counting valid num.
  • close the scanner if you don't need it anymore.


Soln

package com.cnc;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int counter = 0;
int sum = 0;

while(true){
int order = counter + 1;
System.out.println("Enter number #"+order+":");

boolean isAnInt = scanner.hasNextInt();

if(isAnInt){
int number = scanner.nextInt();
counter++;
sum += number;
if(counter == 10){
break;
}
}else {
System.out.println("Invalid number");
}
scanner.nextLine(); //handle end line
}

System.out.println("sum = "+sum);
scanner.close();
}
}
Read the numbers from the console entered by the user and print the minimum and maximum number the user has entered.
Before the user enters the number , print the message "Enter number"
if then user enters an invalid number, break out of the loop and print the minimum and maximum number.
*Use endless while loop .
Soln:
package com.cnc;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);

int min = 0;
int max = 0;
boolean first = true;

while (true){
System.out.println("ENTER NUMBER");
boolean isAnInt = scanner.hasNextInt();

if(isAnInt){
int number = scanner.nextInt();

if(first){
first = false;
min = number;
max = number;
}
if(number>max){
max = number;
}
if(number<min){
min = number;
}
}else {
break;
}
scanner.nextLine();// handle input
}
System.out.println("min = "+min+", max = "+max);
scanner.close();
}
}

Read the numbers from the console entered by the user and print the sum and average value of the number the user has entered.
Soln:
package com.cnc;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
inputThenPrintSumAndAverage();

}

public static void inputThenPrintSumAndAverage(){
Scanner scanner =new Scanner(System.in);

int sum = 0;
int avg = 0;
int store = 0;
while (true){
System.out.println("ENTER NUMBER");
boolean isAnInt = scanner.hasNextInt();
if(isAnInt){
int number = scanner.nextInt();
sum = sum +number;
store = store +1;
avg = sum/store;
}else {
break;
}
scanner.nextLine();
}
System.out.println("SUM ="+sum + " AVG ="+avg);
scanner.close();

}
}

Popular posts from this blog

NUMBER PALINDROME CHALLENGE

ENCAPSULATION CHALLENGE

USER INPUT PRINT ORIGINAL,REVERSE ARRAY AND SORTED ARRAY