展开全部
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 domains &#61; new ArrayList<>();
public static void main(String...asd){
int m,n;
out.print("请输入数字e69da5e887aa3231313335323631343130323136353331333363393033个数N&#xff1a;");
n &#61; input(1,Integer.MAX_VALUE);
out.print("请输入计算行数M&#xff1a;");
m &#61; input(1,10000);
out.println("请输入"&#43;n&#43;"个数字,每个数字介于1跟1000000之间&#xff0c;用空格隔开:");
numbers &#61; inputNumbers(n);
for(int i &#61; 0; i
domains.add(inputDomain(n,i&#43;1));
}
for(Domain domain:domains){
domain.output();
}
}
/**
* 输入一个介于min跟max之间的整数
* &#64;param min
* &#64;param max
* &#64;return
*/
private static int input(int min,int max){
Scanner scanner &#61; new Scanner(in);
try {
int i &#61; scanner.nextInt();
if(imax){
out.print("输入数字大小不符合要求&#xff0c;请重新输入”");
return input(min,max);
}
return i;
}catch (Exception e){
out.print("您输入的不是数字&#xff0c;请重新输入“");
return input(min,max);
}
}
/**
* 输入N个介于1到1000000之间的整数
* &#64;param n
* &#64;return
*/
private static int[] inputNumbers(int n){
Scanner scanner &#61; new Scanner(in);
String numStr &#61; scanner.nextLine();
String[] nums &#61; numStr.trim().split("\\s&#43;");
if(nums.length !&#61; n){
out.println("输入数字个数不对&#xff0c;请重新输入&#xff1a;");
return inputNumbers(n);
}
int[] numbers &#61; new int[n];
for(int i&#61;0;i
try {
int number &#61; Integer.valueOf(nums[i]);
if(number<1 || number >10000){
out.print("输入数字大小不符合要求&#xff0c;请重新输入”");
numbers[i] &#61; input(1,1000000);
}else{
numbers[i] &#61; number;
}
}catch (NumberFormatException e){
out.print(String.format("您输入的%s不是数字&#xff0c;请重新输入&#xff1a;",nums[i]));
numbers[i] &#61; input(1,1000000);
}
}
return numbers;
}
private static Domain inputDomain(int n,int index){
out.println("请输入第"&#43;index&#43;"行的2个数字L,R(1<&#61;L<&#61;R<&#61;%d)用空格隔开&#xff1a;");
Scanner scanner &#61; new Scanner(in);
String numStr &#61; scanner.nextLine();
String[] nums &#61; numStr.trim().split("\\s&#43;");
if(nums.length !&#61; 2){
out.print(String.format("输入数字个数不对&#xff0c;请重新输入!",n));
return inputDomain(n,index);
}
String ls &#61; nums[0];
String rs &#61; nums[1];
try{
int l &#61; Integer.valueOf(ls);
int r &#61; Integer.valueOf(rs);
if(l<1 || rn){
out.print(String.format("输入数字大小不符合&#xff0c;请重新输入!",n));
return inputDomain(n,index);
}
return new Domain(l,r,index);
}catch (NumberFormatException e){
out.println(String.format("输入数字格式错误&#xff0c;请重新输入2个数字L,R(1<&#61;L<&#61;R<&#61;%d)用空格隔开&#xff1a;",n));
return inputDomain(n,index);
}
}
private static class Domain{
int l;
int r;
int index;
Domain(int l, int r, int index) {
this.l &#61; l;
this.r &#61; r;
this.index &#61; index;
}
void output(){
int result &#61; 0;
for(int i &#61; l-1; i
result&#43;&#61; numbers[i];
}
out.println(String.format("第%d行&#xff1a;第%d到第%d个数之和为&#xff1a;%d",index,l,r,result));
}
}
}