2 回答
TA贡献1875条经验 获得超3个赞
首先,向用户询问输入,然后根据该输入循环并使用扫描仪获取输入。
Scanner input= new Scanner(System.in);
System.out.println("Enter Five Numbers");
循环部分:
int num;
int [] arr= new int[5];
for(int i=0; i<arr.length(); i++)
{
System.out.println("Enter Input "+i);
number=input.nextInt();
TakeInput ti= new TakeInput();
if(ti.validate_input(number)==true) arr[i]=number;
else{
System.out.println("Enter Number "+i+" Again");
number=input.nextInt();
}
}
TA贡献1804条经验 获得超8个赞
你可以试试:
import java.util.Arrays;
import java.util.Scanner;
public class TakeInput {
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
System.out.println("Enter Five Numbers Between 1 and 10");
double number;
//set length of array which stores the user input
double [] arr= new double[5];
for(int i=0; i<5; i++)
{
System.out.println("Enter Input "+i);
//accept input from users
number=input.nextDouble();
TakeInput ti= new TakeInput();
//prompts to renter value if value is not valid
if(ti.validate_input(number)==true)
{
arr[i]=number;
}
else
{
System.out.println("Enter Number "+i+" Again");
number=input.nextDouble();
}
}
System.out.println("Array List: "+Arrays.toString(arr));
}
//validate user input, ensure that input is not out of range
public boolean validate_input(double input)
{
boolean response;
if(input<=1 || input >=10)
{
response=false;
}
else
{
response=true;
}
return response;
}
}
添加回答
举报