Reputation:
I have to write a program that starts by requesting an integer in the range 0 < N < 20. Numbers outside this range are rejected and a new request is made. Output the sum of the sequence of numbers starting at 1 and ending with N. I have got most of the codes but I can not continuously ask users for inputs until an input meets the requirement. I tried to use "return" in line 11, however, it does not go back into the loop after getting another input. What should I do now?
import java.util.*;
class ExamTesterNine{
public static void main(String args[]){
Scanner kbReader= new Scanner(System.in);
int num=kbReader.nextInt();
System.out.println("Enter an integer smaller than 20 and larger than 0");
int result;
int sum=0;
if (!(num>0&&num<20)){
return;
}else{
for(int i=1; i<=num; i++)
sum=sum+i;
int [] number= new int [num];
for (int a=0; a<(number.length-1); a++ ){
number[a]=a+1;
System.out.print(number[a]+"+");}
System.out.print(num+"="+sum);
}
}
}
Upvotes: 0
Views: 905
Reputation: 273
import java.util.*;
class ExamTesterNine{
static int num;
public static void readInput() {
System.out.println("Enter an integer smaller than 20 and larger than 0");
Scanner kbReader= new Scanner(System.in);
num=kbReader.nextInt();
if (!(num>0&&num<20)){
ExamTesterNine.readInput();
}else {
calculate(num);
}
}
public static void calculate(int sum) {
for(int i=1; i<=num; i++)
sum=sum+i;
int [] number= new int [num];
for (int a=0; a<(number.length-1); a++ ){
number[a]=a+1;
System.out.print(number[a]+"+");}
System.out.print(num+"="+sum);
}
public static void main(String args[]){
int result;
int sum=0;
ExamTesterNine.readInput();
}
}
Are you expecting this?
Out put:Enter an integer smaller than 20 and larger than 0
23
Enter an integer smaller than 20 and larger than 0
34
Enter an integer smaller than 20 and larger than 0
56
Enter an integer smaller than 20 and larger than 0
45
Enter an integer smaller than 20 and larger than 0
15
1+2+3+4+5+6+7+8+9+10+11+12+13+14+15=135
Upvotes: 0
Reputation: 78
To get a number, try using a check like this
int number=0;
boolean flag;
while{
flag=false;
System.out.println("Enter a number smaller than 20 and greater than 0 : ");
try{
number=kbReader.nextInt();
flag=true;
}catch(Exception e){ //catching the exception that occurs when an input other than integer is entered
System.out.println("OOPS!!!only Integer is allowed :-(");
}
if(flag==true && number>0 && number<20){
break;
}else{
if(flag){
System.out.println("Oops!!!only numbers in the range 0<number<20 is allowed...Re-enter again");
}
}
Upvotes: 0
Reputation: 61
There are many different ways to approach this. However, as a start to become familiar with while loops, I recommend this simple method:
System.out.println("Enter an integer smaller than 20 and larger than 0");
int num = kbReader.nextInt();
while(num > 20 || num < 0)
{
System.out.println("That value does not meet the criteria. Please try again:");
num = kbReader.nextInt();
}
Until the correct value is entered, the user will be asked to retry their input.
Upvotes: 0
Reputation: 2846
IT should be easy with do-while
. I am not on my compiler right now however this you should add in your code if you are using scanner
import java.util.*;
class ExamTesterNine{
public static void main(String args[]){
Scanner kbReader= new Scanner(System.in);
int num = 0;
System.out.println("Enter an integer smaller than 20 and larger than 0");
do{
num=kbReader.nextInt();
} while(num<0 && num <20);
int result;
int sum=0;
for(int i=1; i<=num; i++)
sum=sum+i;
int [] number= new int [num];
for (int a=0; a<(number.length-1); a++ ){
number[a]=a+1;
System.out.print(number[a]+"+");}
System.out.print(num+"="+sum);
}
}
}
Let me know if it don't I can get on the compiler quickly however do-while
is solution for you.
Upvotes: 3
Reputation: 44854
you will need a while
loop as you do not know how many times the wrong input will be entered
while (true) {
System.out.println("Enter an integer smaller than 20 and larger than 0");
int num=kbReader.nextInt(); // get input
// test
if (goodInput (num)) {
break;
}
}
Upvotes: 1