Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 执行while循环和数组_Java_Loops_Do While_Do Loops - Fatal编程技术网

Java 执行while循环和数组

Java 执行while循环和数组,java,loops,do-while,do-loops,Java,Loops,Do While,Do Loops,有人能帮我做这个循环吗?我希望用户可以选择在程序结束时继续Y/N以重新运行或终止程序。最好是做一个while循环 代码(Java): publicstaticvoidmain(字符串[]args){ int size=10;//数组的大小 Scanner scan=新扫描仪(System.in);//创建扫描仪 while(true) { System.out.println(“输入值:”); int[]myArray=new int[size];//声明数组 对于(int i=0;i

有人能帮我做这个循环吗?我希望用户可以选择在程序结束时继续Y/N以重新运行或终止程序。最好是做一个while循环

代码(Java):

publicstaticvoidmain(字符串[]args){
int size=10;//数组的大小
Scanner scan=新扫描仪(System.in);//创建扫描仪
while(true)
{
System.out.println(“输入值:”);
int[]myArray=new int[size];//声明数组
对于(int i=0;i对于(int i=0;i您可以执行以下操作:

    public static void main(String[] args) {
        int size = 10; //size of array
        String nextInput;
        Scanner scan = new Scanner(System.in); //create scanner
        do {
            System.out.println("Enter values: ");
            int[] myArray = new int[size]; //declare array

            for (int i = 0; i < size; i++) {
                int userInput = scan.nextInt(); //receive user input
                myArray[i] = userInput;
            }


            System.out.print("You entered these values: "); //print all values entered by user

            for (int i = 0; i < myArray.length; i++) {
                System.out.print(" " + myArray[i]);
            }

            System.out.println("");

            IndexOfLargestBatista largeIndex = new IndexOfLargestBatista(); //object of IndexOfLargest class

            System.out.println("Index of Largest value: " + largeIndex.findIndex(myArray)); //pass array as parameter and get index number in return

            System.out.println("Do you want to continue?: Y/N ");
            nextInput = scan.nextLine();

        } while (nextInput.equalsIgnoreCase("Y"));
    }
publicstaticvoidmain(字符串[]args){
int size=10;//数组的大小
字符串输入;
Scanner scan=新扫描仪(System.in);//创建扫描仪
做{
System.out.println(“输入值:”);
int[]myArray=new int[size];//声明数组
对于(int i=0;i
您可以使用
while(true)
循环,一旦用户进入
N
您就可以
中断
循环,否则继续。非常感谢!!!
    public static void main(String[] args) {
        int size = 10; //size of array
        String nextInput;
        Scanner scan = new Scanner(System.in); //create scanner
        do {
            System.out.println("Enter values: ");
            int[] myArray = new int[size]; //declare array

            for (int i = 0; i < size; i++) {
                int userInput = scan.nextInt(); //receive user input
                myArray[i] = userInput;
            }


            System.out.print("You entered these values: "); //print all values entered by user

            for (int i = 0; i < myArray.length; i++) {
                System.out.print(" " + myArray[i]);
            }

            System.out.println("");

            IndexOfLargestBatista largeIndex = new IndexOfLargestBatista(); //object of IndexOfLargest class

            System.out.println("Index of Largest value: " + largeIndex.findIndex(myArray)); //pass array as parameter and get index number in return

            System.out.println("Do you want to continue?: Y/N ");
            nextInput = scan.nextLine();

        } while (nextInput.equalsIgnoreCase("Y"));
    }