我必须编写一个程序,该程序首先请求范围为0 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);
}
}
}
答案 0 :(得分:3)
使用do-while
使IT变得容易。我现在不在编译器上,但是如果使用扫描仪,则应该在代码中添加
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);
}
}
}
让我知道是否可以快速进入编译器,但是do-while
是您的解决方案。
答案 1 :(得分:1)
您将需要一个while
循环,因为您不知道输入了多少次错误的输入
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;
}
}
答案 2 :(得分:0)
要获取电话号码,请尝试使用像这样的支票
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");
}
}
答案 3 :(得分:0)
有许多不同的方法可以解决此问题。但是,为了开始熟悉while循环,我建议您使用以下简单方法:
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();
}
在输入正确值之前,将要求用户重试输入。
答案 4 :(得分:0)
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