Java source code

package?com.zhidao;

import?java.util.ArrayList;

import?java.util.List;

import?java.util.Scanner;

import? ?static?java.lang.System.out;

import?static?java.lang.System.in;

/**

*?Created?by?tracy?on?2017/10/11.

*/

public?class?LR?{

static?int[]?numbers;

static?List<Domain>?domains?=?new?ArrayList<>();

public? static?void?main(String. .asd){

int?m,n;

out.print("Please enter the number of digits N:");

n?=?input(1,Integer.MAX_VALUE);

out.print("Please enter the number of rows of the calculation M:");

m?=? input(1,10000);

out.println("Please enter "+n+" numbers, each between 1 and 1000000, separated by spaces:");

numbers?=?inputNumbers(n);

for(int?i?=?0;?i <m;?i++){

domains.add(inputDomain(n,i+1));

}

for(Domain?domain:domains){

domain.output();

}

}

/**

*? Input an integer between min and max

*? @param?min

*? @param?max

*? @return

*/

private?static?int?input(int?min,int?max){

Scanner?scanner?=?new?Scanner(in);

try?{

int?i?=? scanner.nextInt();

if(i<min?||?i>max){

out.print("The size of the input number does not meet the requirements, please retype"");

return?input(min,max);;

Try?{input(min,max);?

}

return?i;

}catch?(Exception?e){

out.print("What you entered is not a number, please retype "");

return?input(min,max);< /p>

}

}

/**

*? Input N integers between 1 and 1,000,000

*? @param?n

*? @return

*/

private?static?int[]?inputNumbers(int?n){

Scanner?scanner?=?new?Scanner(in);

String?numStr?=? scanner.nextLine();

String[]?nums?=?numStr.trim().split("\\s+");

if(nums.length?! =?n){

out.println("The number of input numbers is incorrect, please re-enter:");

return?inputNumbers(n);

}

int[]?numbers?=?new?int[n];

for(int? i=0;i<n;i++){

try?{

int?number?=?Integer.valueOf(numbers[i]);

if(number<1?||?number?>10000){

out. print("The size of the input number does not meet the requirements, please re-enter it");

numbers[i]? =?input(1,1000000);

}else{

numbers[i]? =?number;

}

}catch?(NumberFormatException?e){

out.print(String.format("The %s you typed is not a number, please retype:",nums[i]));

numbers[i]? =?input(1,1000000);

}

}

return?numbers;

}

private?static?Domain?inputDomain(int?n,int?index){

out.println("Please enter the 2 digits L,R(1<=L<=R<=%d) of the first "+index+"line separated by spaces:");

Scanner?scanner?=?new?Scanner(in);

String?numStr?=? scanner.nextLine();

String[]?nums?=?numStr.trim().split("\\s+");

if(nums.length?! =?2){

out.print(String.format("Incorrect number of digits entered, please re-enter!" ,n));

return?inputDomain(n,index);

}

String?ls?=?nums[0];

String?rs?=?nums[1];

try{

int?l?=? Integer.valueOf(ls);

int?r?=?Integer.valueOf(rs);

if(l<1?||?r<l?||?r>n){

out.print(String.format("Input number Size does not match, please re-enter!" ,n));

return?inputDomain(n,index);

}

return?new?Domain(l,r,index);

}catch?(NumberFormatException?e){

out.println(String.format("Wrong format for input number, please re-enter 2 numbers L,R(1<=L<=R<=%d) separated by spaces:",n));

return?inputDomain(n,index);

}

}

private?static?class?Domain{

int?l;

int?r;

int?index;

Domain(int?l,?int?r,?int?index)? {

this.l?=?l;

this.r?=?r;

this.index?=?index;

}

void?output(){

int?result?=?0;

for(int?i ?=?l-1;?i<r;?i++){

result+=?numbers[i];

}

out.println(String.format("Row %d: sum of numbers %d to %d: %d",index,l,r,result)) ;

}

}

}

}