6、  打印1-100之间所有的偶数,每行显示10个数字,数字右对齐。
右对齐 这怎么做啊,我实在不知道方法啦。要用方法调用做。

解决方案 »

  1.   

    用printf吧,查一下格式化输出的相关内容
      

  2.   

    public class test
    {
         public static void main(String[] args)
        {
            int i=0;
            int a[50]=new int[50];
            while(i<=100)
           {
              i++;
              while(i%2==0)
              {
                 a[i/2]=i;
              }       }
           for(int j=0;j<50;j++)
          {
                if(a[j]<10)
                {
                     System.out.print(a[j]+"  ");
                }
                if(a[j]>=10&&a[j]<100)
                {
                     System.out.print(a[j]+" ");
                }
                if(j%10==0)
                {
                    System.out.println();
                }
           }
    }
    }
      

  3.   

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;public class Test {
        public static void main(String[] args) throws Exception {
            int count = 1;
            List<String> list = new ArrayList<String>();
            StringBuilder sb = new StringBuilder();
            for (int i = 1; i < 100; i++) {
                if ((i & 1) == 0) {
                    count++;
                    sb.append(i);
                    if (count == 10) {
                        list.add(sb.toString());
                        count = 1;
                        sb = new StringBuilder();
                    } else {
                        if (i == 98) list.add(sb.toString());
                    }
                }
            }
            List<String> temp = new ArrayList<String>();
            for (String str : list)
                temp.add(str);
            // Collections.copy(temp,list);
            Collections.sort(list, new Comparator<String>() {
                public int compare(String o1, String o2) {
                    return o1.length() > o2.length() ? -1 : (o1.length() == o2.length() ? 0 : 1);
                }
            });
            int maxLength = list.get(0).length();
            for (String str : temp) {
                System.out.println(getNewString(str, maxLength));
            }
        }    private static String getNewString(String str, int maxLength) {
            if (str.length() < maxLength) {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i <maxLength - str.length(); i++) {
                    sb.append(" ");
                }
                str = sb.toString() + str;
            }
            return str;
        }
    }
      

  4.   


    public class Main {
    public static void main(String[] args) {
    int count = 0;
    for(int i=1; i<=100; i++) {
    if(i % 2 == 0)
    System.out.printf("%3d ", i);
    //输出三位,默认就是右对齐,如果想左对齐的话,就在3前面加个-
    count ++;
    if(count % 10 == 0) 
    System.out.println();
    }
    }
    }/*
    输出结果
       2    4    6    8   10
     12  14  16  18   20
     22  24  26  28   30
     32  34  36  38   40
     42  44  46  48   50
     52  54  56  58   60
     62  64  66  68   70
     72  74  76  78   80
     82  84  86  88   90
     92  94  96  98 100
    */
      

  5.   

    public static void main(String[] args) {
        printEven(1, 100, 1, 10);
    }public static void printEven(int min, int max, int numSpace, int numPerLine) {
        numSpace = numSpace < 1 ? 1 : numSpace;
        numPerLine = numPerLine < 1 ? 10 : numPerLine;
        if (min > max) {
            min = min ^ max;
            max = min ^ max;
            min = min ^ max;
        }
        min = min > 0 ? (min + 1) / 2 * 2 : min / 2 * 2;
        max = max > 0 ? max / 2 * 2 : (max - 1) / 2 * 2;
        int len1 = getLength(max), len2 = getLength(min);
        int len = len1 > len2 ? len1 : len2;
        String format = String.format("%%%dd", len);
        String space = String.format(String.format("%%%ds", numSpace), "");
        for (int i = min, count = 0; i <= max; i += 2, count++) {
            System.out.printf(format, i);
            if (count % numPerLine == numPerLine - 1)
                System.out.println();
            else
                System.out.print(space);
        }
    }public static int getLength(int i) {
        int len = 0, tmp = i;
        while (tmp != 0) {
            tmp /= 10;
            len++;
        }
        if (i < 0)
            len++;
        return len;
    }比ls多了些泛用性的玩意
      

  6.   

    public class Test {
        public static void main(String[] args) {
         new Test().fangfa();
        }
        public void fangfa(){
         for(int i=0;i<5;i++){
         for(int j=1;j<=10;j++){
         int k=2*j+20*i;
         if(k<10)System.out.print("   "+k);
         else if(k<100)System.out.print("  "+k);
         else System.out.print(" "+k);
         }
         System.out.println();
         }
        }
    }
    结果
       2   4   6   8  10  12  14  16  18  20
      22  24  26  28  30  32  34  36  38  40
      42  44  46  48  50  52  54  56  58  60
      62  64  66  68  70  72  74  76  78  80
      82  84  86  88  90  92  94  96  98 100
    所谓的右对齐?这也算实现了吧
      

  7.   

    这个很对吧 我赞同 public class test {
        public static void main(String args[]) {
            int n=10;
            for (int i=1;i<=100;i++) {
                if (i%2==0)
                    System.out.printf("%3d   ",i);
                n++;
                if (n%10==0)
                    System.out.println();
            }
        }}
      

  8.   

    #include <iostream>
    #include <iomanip>using namespace std;int main()
    {
    int i;
    int t=0;
    cout<<setw(5);
    for(i=1;i<=100;i++)
    {
    if( i % 2 == 0 )
    {
    cout<<i<<setw(5);
    t++;
    }
    if( t % 10 == 0 )
    cout<<endl;
    }
    return 0;
    }