南昌大学技术交流...吧 关注:89贴子:975

27号及以前的学习代码(安卓部与web部所学习的java)提问讨论区

取消只看楼主收藏回复

咳咳。。。从今天起。。。java课上的讲解代码也同步开更了。。。


IP属地:浙江1楼2015-10-29 23:25回复
    猜数字
    package guess;
    import java.util.Scanner;
    public class guess {
    public static void main(String[] args) {
    int i = (int)(Math.random()*99+1);
    System.out.print("请猜一个1-100的数字:");
    Scanner scanner = new Scanner(System.in);
    int a;
    int score=0,k;
    for(k=0;k<10;k++){
    a = scanner.nextInt();
    if(i==a){
    System.out.println("恭喜你猜对了!");
    i = (int)(Math.random()*99+1);
    score+=10;
    System.out.print("请猜一个1-100的数字:");
    }
    else{
    if(a>i){
    System.out.println("你猜的数有点大。。。");
    }
    if(a<i){
    System.out.println("你猜的数有点小。。。");
    }
    }
    }
    System.out.println("你的得分是:"+score+"!");
    }
    }


    IP属地:浙江2楼2015-10-29 23:26
    回复
      三角形类型判定
      package triangle;
      import java.util.Scanner;
      //三角形
      public class triangle {
      public static void main(String[] args) {
      System.out.print("请输入三角形的三条边:");
      int x=0,y=0,z=0;
      Scanner scanner = new Scanner(System.in);
      x=scanner.nextInt();
      y=scanner.nextInt();
      z=scanner.nextInt();
      if(x+y>=z||x+z>=y||z+y>=x){
      System.out.println("这不是三角形!");
      }
      else{
      if(x==y||y==z||z==x){
      if(x==y&&y==z){
      System.out.println("这是等边三角形!");
      }
      else{
      System.out.println("这是等腰三角形!");
      }
      }
      else{
      System.out.println("这是普通三角形!");
      }
      }
      }
      }


      IP属地:浙江3楼2015-10-29 23:27
      回复
        打印空心菱形
        package rhombus;
        import java.util.Scanner;
        //菱形
        public class rhombus {
        public static void main(String[] args) {
        System.out.print("请输入菱形边长:");
        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);
        int bian = scanner.nextInt();
        for(int i=1;i<=bian;i++){
        for(int k=1;k<=bian-i;k++){
        System.out.print(" ");
        }
        System.out.print("*");
        if(i!=1){
        for(int k=1;k<=2*i-3;k++){
        System.out.print(" ");
        }
        System.out.print("*");
        }
        System.out.println();
        }
        for(int i=1;i<=bian-1;i++){
        for(int k=1;k<=i;k++){
        System.out.print(" ");
        }
        System.out.print("*");
        if(i!=bian-1){
        for(int k=1;k<=(bian-i)*2-3;k++){
        System.out.print(" ");
        }
        System.out.print("*");
        }
        System.out.println();
        }
        }
        }


        IP属地:浙江5楼2015-10-29 23:29
        回复
          数组之按数组的和排序
          class arrayfour
          {
          public static void main(String[] args)
          {
          int a[][] ={{80001,86,88,89},{80002,88,79,94},{80003,76,84,99}};
          //int[][] a = new a[3][];
          int temp;
          for(int i = 0; i<3;i++){
          for(int j=i;j<3;j++){
          if((a[i][0]+a[i][1]+a[i][2])<(a[j][0]+a[j][1]+a[j][2])){
          for(int k=0;k<4;k++){
          temp = a[i][k];
          a[i][k] = a[j][k];
          a[j][k] = temp;
          }
          }
          }
          }
          for(int z=0; z<3; z++)
          System.out.printf("%d %d %d %d \n",a[z][0],a[z][1],a[z][2],a[z][3]);
          }
          }


          IP属地:浙江8楼2015-10-29 23:45
          回复
            数组之排列大小
            class arraythree
            {
            public static void main(String[] args)
            {
            int[] a = new int[]{12,32,34,312,32};
            int temp = 0;
            for(int i=0; i<5; i++){
            for(int j=i; j<5;j++)
            if(a[i] > a[j]){
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
            }
            }
            for(int j=0; j<5;j++){
            System.out.printf("%d ",a[j]);
            }
            }
            }


            IP属地:浙江9楼2015-10-29 23:46
            回复
              数组值之揪出最大最小值
              class arraytwo
              {
              public static void main(String[] args)
              {
              int[] a = new int[]{12,32,34,312,32};
              int max = a[0];
              int min = a[0];
              int c=0;
              int d=0;
              int temp = 0;
              for(int i=0; i<4; i++){
              if(a[i]>max)
              {
              max = a[i];
              c = i;
              }
              if(a[i]<min){
              min = a[i];
              d = i;
              }
              }
              System.out.printf("max: a[%d] %d\nmin: a[%d] %d\n",c,a[c],d,a[d]);
              }
              }


              IP属地:浙江10楼2015-10-29 23:48
              回复
                数组之一维数组
                class arrayone
                {
                public static void main(String[] args)
                {
                int [] a = new int[10];//下标
                int [] b = {9,8,7};
                for(int i=0; i<10; i++){
                a[i] = i;
                System.out.printf("a[%d]: %d \n",i,a[i]);
                }
                }
                }


                IP属地:浙江12楼2015-10-29 23:54
                回复
                  循环之while
                  class whileone
                  {
                  public static void main(String[] args)
                  {
                  int a = 9;
                  int sum = 0;//不可放在循环内
                  while(a>0){
                  System.out.println("a = "+a+" sum = "+sum);
                  sum+=a;
                  a--;
                  }
                  System.out.println("The sum is: "+sum);
                  }
                  }


                  IP属地:浙江13楼2015-10-29 23:55
                  回复
                    循环之for 1
                    class forone
                    {
                    public static void main(String[] args)
                    {
                    for(int i=1; i<10; i++){
                    System.out.println("Hello World!");
                    }
                    }
                    }


                    IP属地:浙江14楼2015-10-29 23:56
                    回复
                      循环之for 2
                      class fortwo{
                      public static void main(String[] args)
                      {
                      for(int i= 1; i < 10; i++)
                      {
                      for(int j = 1; j <= i; j++)
                      {
                      System.out.printf("%d*%d=%2d ",i,j,i*j);
                      }
                      System.out.printf("\n");
                      }
                      }
                      }


                      IP属地:浙江15楼2015-10-29 23:56
                      回复
                        循环之for 3
                        import java.util.*;
                        class forthree
                        {
                        public static void main(String[] args)
                        {
                        System.out.println("请选择菱形的边长:");
                        Scanner p = new Scanner(System.in);
                        int a = p.nextInt();
                        for(int i=1; i<=a; i++){
                        for(int j=1; j<=a-i;j++)
                        {
                        System.out.printf(" ");
                        }
                        for(int k=1; k<=i;k++)
                        {
                        System.out.printf("* ");
                        }
                        System.out.println();
                        }
                        for(int q=a-1; q>0; q--){
                        for(int w=1; w<=a-q;w++)
                        {
                        System.out.printf(" ");
                        }
                        for(int e=1; e<=q;e++)
                        {
                        System.out.printf("* ");
                        }
                        System.out.println();
                        }
                        }
                        }


                        IP属地:浙江16楼2015-10-29 23:57
                        回复
                          for的用法实例 4就是空心菱形的代码


                          IP属地:浙江17楼2015-10-29 23:58
                          回复
                            循环之do while
                            class dowhileone
                            {
                            public static void main(String[] args)
                            {
                            int a = 10;
                            int sum = 0;//不可放在循环内
                            do{
                            System.out.println("a = "+a+" sum = "+sum);
                            sum+=a;
                            a--;
                            }while(a>0);
                            System.out.println("The sum is: "+sum);
                            }
                            }


                            IP属地:浙江18楼2015-10-29 23:59
                            回复
                              循环之break
                              class breakone
                              {
                              public static void main(String[] args)
                              {
                              int i=0;
                              for(;;i++){
                              if(i>9)
                              {
                              System.out.println("退出运行!");
                              break;
                              }
                              if(i==0){
                              continue;
                              }
                              System.out.println("程序正在尝试运行第"+i+"次");
                              }
                              }
                              }


                              IP属地:浙江19楼2015-10-30 00:00
                              回复