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