猿问

从用户输入 1-10 次五次并将它们存储在一个数组中,如果输入错误,则提示输入正确

我循环了五次并使用 util.Scanner 获取用户输入,我被困在我必须提示用户正确输入的部分,以及何时给出正确的输入并将其存储在数组中。然后循环继续。


while(a<5){

        Scanner input = new Scanner(System.in);

        System.out.println("Please enter the value: ");

        int x = input.nextInt();

        //what code should be added here to prompt user if input is not in 1-10

        //and after checking only, the value should be stored in the array

        userInputs[a] = x;


        a++;

    }


守着星空守着你
浏览 125回答 2
2回答

翻过高山走不出你

首先,向用户询问输入,然后根据该输入循环并使用扫描仪获取输入。Scanner input= new Scanner(System.in);System.out.println("Enter Five Numbers");循环部分:&nbsp; &nbsp; int num;&nbsp; &nbsp; int [] arr= new int[5];&nbsp; &nbsp; for(int i=0; i<arr.length(); i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter Input "+i);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; number=input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; TakeInput ti= new TakeInput();&nbsp; &nbsp; &nbsp; &nbsp; if(ti.validate_input(number)==true) arr[i]=number;&nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter Number "+i+" Again");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; number=input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

胡说叔叔

你可以试试:import java.util.Arrays;import java.util.Scanner;public class TakeInput {&nbsp; &nbsp; public static void main(String[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Scanner input= new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter Five Numbers Between 1 and 10");&nbsp; &nbsp; &nbsp; &nbsp; double number;&nbsp; &nbsp; &nbsp; &nbsp; //set length of array which stores the user input&nbsp; &nbsp; &nbsp; &nbsp; double [] arr= new double[5];&nbsp; &nbsp; &nbsp; &nbsp; for(int i=0; i<5; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter Input "+i);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //accept input from users&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; number=input.nextDouble();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TakeInput ti= new TakeInput();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //prompts to renter value if value is not valid&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(ti.validate_input(number)==true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[i]=number;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter Number "+i+" Again");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; number=input.nextDouble();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Array List: "+Arrays.toString(arr));&nbsp; &nbsp; }&nbsp; &nbsp; //validate user input, ensure that input is not out of range&nbsp; &nbsp; public boolean validate_input(double input)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; boolean response;&nbsp; &nbsp; &nbsp; &nbsp; if(input<=1 || input >=10)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response=false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response=true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return response;&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答