要求用户输入3个不同的数,然后去掉中间数以后从小到大排列,再求这个数列的和。
比如说:
Enter a number: 10 
Enter a number: 4
Enter a number:1
1,2,3,5,6,7,8,9,10
51
(这个例子里,10,4,1这三个数都是用户输入的,下面的数列1,2,3,5,6,7,8,9,10跳过了用户输入的中间数4后从小到大排列,51是上面数列的和)以下是我自己编的:package abc;import java.io.*;public class abc {    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));        System.out.print("Enter a number: ");
        int a = Integer.parseInt(in.readLine());
        System.out.print("Enter a number: ");
        int b = Integer.parseInt(in.readLine());
        System.out.print("Enter a number: ");
        int c = Integer.parseInt(in.readLine());
        if (a > b) {
            if (b > c) {
                for (int i = 0; i <= (b - c - 1); i++) {
                    System.out.print((i + c) + ",");
                }
                for (int j = 1; j <= (a - b); j++) {
                    System.out.print((j + b) + ",");
                }
            }
        }
    }
}我先假设了a最大,b中间,c最小,然后打算依次假设。可是这个输出的数列结果中,最后一个符号是逗号,按老师要求是不能出现逗号的(就像例子里的一样),我尝试用substring,可是一用就把所有逗号都去掉了还有不知道怎样求这个数列之和。小弟刚学java,老师又教的比较快,盼各位的帮助啊。

解决方案 »

  1.   

    你最后一个写个判断的语句就行了如果是最后一个就只输出数字,不输出逗号。求数列的和直接一个for循环就搞定了呀。在你原有的基础上,增加一个变量int sum  然后在for循环里面把输出的值加起来就行了。
      

  2.   


    import java.io.*;public class Abc {  public static void main(String[] args) throws IOException {
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));  System.out.print("Enter a number: ");
      int a = Integer.parseInt(in.readLine());
      System.out.print("Enter a number: ");
      int b = Integer.parseInt(in.readLine());
      System.out.print("Enter a number: ");
      int c = Integer.parseInt(in.readLine());
      
      
      int max = Math.max(a, b) ;
      int min = Math.min(a, b) ;
      max = Math.max(max, c) ;
      min = Math.min(min, c) ;
      String str = "" ;
      for (int i = min ; i<= max ; i++){
       str = str + "," + i ;
      }
      
      System.out.println(str.substring(1)) ;
    //  if (a > b) {
    //  if (b > c) {
    //  for (int i = 0; i <= (b - c - 1); i++) {
    //  System.out.print((i + c) + ",");
    //  }
    //  for (int j = 1; j <= (a - b); j++) {
    //  System.out.print((j + b) + ",");
    //  }
    //  }
    //  }
      }
    }
    E:\>java Abc
    Enter a number: 5
    Enter a number: 2
    Enter a number: 11
    2,3,4,5,6,7,8,9,10,11
      

  3.   


    import java.io.*;public class Abc {  public static void main(String[] args) throws IOException {
       BufferedReader in = new BufferedReader(new InputStreamReader(System.in));   System.out.print("Enter a number: ");
      int a = Integer.parseInt(in.readLine());
      System.out.print("Enter a number: ");
      int b = Integer.parseInt(in.readLine());
      System.out.print("Enter a number: ");
      int c = Integer.parseInt(in.readLine());
      
        //你随便输入3个数 ,然后得到最大最小 
      int max = Math.max(a, b) ;
      int min = Math.min(a, b) ;
      max = Math.max(max, c) ;
      min = Math.min(min, c) ;
      String str = "" ;
      for (int i = min ; i<= max ; i++){
       str = str + "," + i ;
      }
      
       System.out.println(str.substring(1)) ;
       System.out.println("-------") ;
        //你的方法只能是输入3个数 ,一定要从大到小
      if (a > b) {
      if (b > c) {
      for (int i = 0; i <= (b - c - 1); i++) {
       System.out.print((i + c) + ",");
      }
      for (int j = 1; j <= (a - b); j++) {  
       if (j == a-b){  
       System.out.print((j + b));
       }else{
           System.out.print((j + b) + ",");
        }
      }
      }
      }
      }
    }
      

  4.   

    不好意思 ,没注意看题 ,原来是不能把中间的那个输出啊 ,改了一下import java.io.*;public class Abc {  public static void main(String[] args) throws IOException {
       BufferedReader in = new BufferedReader(new InputStreamReader(System.in));   System.out.print("Enter a number: ");
      int a = Integer.parseInt(in.readLine());
      System.out.print("Enter a number: ");
      int b = Integer.parseInt(in.readLine());
      System.out.print("Enter a number: ");
      int c = Integer.parseInt(in.readLine());
      
        //你随便输入3个数 ,然后排好序 ,a最小 ,b中间 ,c最大
        int tmp  ;
        if (a > b){
         tmp = a ;
         a = b ;
         b = tmp ;
        
        }
        if (b > c) {
         tmp = b ;
         b = c;
         c = tmp; 
        }
        
        System.out.println(a + " " + b + " " + c) ;
        tmp = a ;
        a = c ;
        c = tmp ;
      String str = "" ;
      for (int i =  c  ; i<= a ; i++){
       if (i != b ){
         str = str + "," + i ;
        }
      }
      
       System.out.println(str.substring(1)) ;
       System.out.println("-------") ;
        //你的方法只能是输入3个数 ,一定要从大到小
      if (a > b) {
      if (b > c) {
      for (int i = 0; i <= (b - c - 1); i++) {
       System.out.print((i + c) + ",");
      }
      for (int j = 1; j <= (a - b); j++) {  
       if (j == a-b){  
       System.out.print((j + b));
       }else{
           System.out.print((j + b) + ",");
        }
      }
      }
      }
      }
    }
      

  5.   

    谢谢1楼的思路,求和只要int sum就可以了也谢谢2-4楼的代码,不过我运行了一下4楼的代码,貌似有些错误,已在自己的作业上修改再次感谢两位。