-2

我有以下问题:我需要一个有效的 Java 程序来生成 -100 到 99999 的分类区间。

Example: A = -10 to 100, B = 200-300...

例外:

  • 不能有重叠范围。
  • 必须填写所有数字。
  • 根据数不能小于第一个

///编辑对不起,我使用google lang进行通信,大部分仍然使用哈哈所以代码与我现在在工作中所做的非常相似,但是我当然不能深入交谈哈哈我想快速或更简单的东西

package com.teste;

import java.util.ArrayList;
import java.util.List;

//It is for java 1.4
public class Main {
    public static void main(String[] args) {
        try {


        //These numbers are provided by user
        Intervalo a = new Intervalo("20", "50");

        //The lowest number will define the first num of interval
        //In this case -20
        Intervalo b = new Intervalo("-20", "150");

        //Invalid interval, in conflict with 'a'
        Intervalo c = new Intervalo("10", "30");

        Intervalo d = new Intervalo("40", "10");//Will throw 2
        Intervalo e = new Intervalo("100","200");
        //Then the range of interval is -20 to 99999
        //But the parameters are dynamic so i did the follow 
        List lista = new ArrayList();

        int menorNum = 99999;

        lista.add(a);
//      lista.add(b);
//      lista.add(c);
//      lista.add(d);
        lista.add(e);



        //Now, I need to validade
        //1-I need to fill all numbers x to 99999 otherwise throw some exception like "there is numbers to categorize..."
        //2-throw exception if getIntervalo2()<getIntervalo1();
        //3-throw exception if one interval are in other interval of numbers

        for (int i = 0; i < lista.size(); i++) {
            Intervalo intervalo = (Intervalo) lista.get(i);

            int num1 = Integer.parseInt(intervalo.getIntervalo1());
            int num2 = Integer.parseInt(intervalo.getIntervalo2());

            if(num2<num1){
                throw new Exception("Exception 2...");
            }
        }

        //finally the lowest number
        for (int i = 0; i < lista.size(); i++) {
            Intervalo intervalo = (Intervalo) lista.get(i);
            if(Integer.parseInt(intervalo.getIntervalo1())<menorNum){
                menorNum = Integer.parseInt(intervalo.getIntervalo1());
            }
        }

        List listaNumeros = new ArrayList();
        for (int i = menorNum; i < 100000; i++) {
            listaNumeros.add(i);
        }
        //I was thinking to make listaNumeros something imutable,
        // copying to new List, but i don't know if it is worth about memory usage
        for (int i = 0; i < lista.size(); i++) {
            Intervalo intervalo = (Intervalo) lista.get(i);

            int num1 = Integer.parseInt(intervalo.getIntervalo1());
            int num2 = Integer.parseInt(intervalo.getIntervalo2());

                int range = num2 - num1;
                int indexOfNum1 =  listaNumeros.indexOf(num1);

            if(indexOfNum1==-1){
                //Throw the exception 3
                throw new Exception("Exception 3... numbers already in use");
            }else{
                for (int k = indexOfNum1; k <=range; k++) {
                    listaNumeros.set(k, null);//The number already used haha...
                }
            }
        }

        //To exception 1
        //Copying again
        List listOfExceptions = new ArrayList();
        String[] interval = new String[2];
        //My idea is, [1,2,3,null,null,4,...], then i know that left to fill [1-3] and [4-99999] 
        for (int i = 0; i < listaNumeros.size(); i++) {
            if(listaNumeros.get(i)!=null&&interval[0]==null){
                interval[0]=listaNumeros.get(i)+"";//I forget what i dided in this line but in my work '+' to concat String are prohibited haha ;
            }
            if(listaNumeros.get(i)==null&&interval[0]!=null){
                interval[1]=listaNumeros.get(i-1)+"";
            }
            if(i==listaNumeros.size()-1){
                interval[1]=listaNumeros.get(i)+"";
            }
            if(interval[0]!=null&&interval[1]!=null){
                listOfExceptions.add(new Exception("Left interval "+interval[0]+" - "+interval[1]));
                interval = new String[2];
            }

        }
        StringBuffer sb = new StringBuffer("");
        for (int i = 0; i < listOfExceptions.size(); i++) {
            Exception ex = (Exception) listOfExceptions.get(i);
            sb.append(ex.getMessage()).append("\n");
        }
        if(!sb.toString().equals("")){
            throw new Exception(sb.toString());
        }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println(":-D");
        }

    }
}
4

2 回答 2

1

我不确定您是否已经尝试过,但您可以尝试类似的方法,我知道这不是最好的方法,但应该可以。

for(int i = -100; i< 99999; i++ ){
        if(i>=-10 && i<=100){
            System.out.println("A");
        }
        else if(i>=200&&i<=300){
            System.out.println("B");
        }
        // more else ifs
        else{
            System.out.println("Catch");
        }
    }

如果这有帮助的话。

于 2013-06-06T22:35:08.647 回答
0

根据问题的原始版本和提供的代码,在我看来问题是:给定一个区间范围,验证这些区间是否满足 3 个条件。我在下面解决这个问题。

我不会给你代码,但这里的想法是:

sort the intervals
if first.start != -100
  exception - not filling all numbers
iterate through the sorted intervals
  if current.end > current.start
    exception - invalid interval
  if previous.end + 1 < current.start
    exception - not filling all numbers
  if previous.end + 1 > current.start
    exception - overlapping intervals
if last.end != 99999
  exception - not filling all numbers
于 2013-06-07T08:00:02.303 回答