栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Java

简单入门的Java程序

Java 更新时间:发布时间: 百科书网 趣学号
1.打印9*9乘法表
public class Test {
    public static void main(String[] args){
        for(int i=1;i<=9;i++){
            for(int j=1;j<=i;j++){
                System.out.print(j+"*"+i+"="+i*j+"t");
            }
            System.out.println();
        }
    }
}

2.从0~100随机输出一个数
mport java.util.Random;

public class Test {
    public static void main(String[] args){
        Random e= new Random();
        int a=e.nextInt(101);
        System.out.println(a);
    }
}

3.纸张厚度为8cm,求折叠多少次大于珠穆朗玛峰的高度
public class Test {
    public static void main(String[] args){
        int count=0;
        double paper=0.08;
        while(1==1){
            paper=paper*2;
            count++;
            System.out.println("折叠"+count+"次,高度为"+paper);
            if(paper>=8848000){
                break;
            }
        }
    }
}

4.数组的逆序
public class Test {
    public static void main(String[] args){
        int[]a={1,2,3,4};
        System.out.println("数组的逆序为:");
        for(int i=a.length-1;i>=0;i--){
            System.out.print(a[i]+" ");
        }
    }
}

5.遍历数组
public class Test {
    public static void main(String[] args){
       double[]d={3.14,6.66,7.89,2.68,0.5};
       for(double x:d){
           System.out.println("当前循环取出值为:"+x);
       }
    }
}

6.异常处理
import java.util.Scanner;

public class Test {
    public static void main(String[] args){
      Scanner input=new Scanner(System.in);
      System.out.println("请输入a,b:");
      int a=input.nextInt();
      int b=input.nextInt();
      int c=a/b;
      System.out.println(c);
      if(b==0){
          System.out.println("输出错误");
          try{
              throw new Exception("异常,请重新输入");
          }catch(Exception e){
              e.printStackTrace();
          }finally{
              System.out.println("测试完毕!");
          }
      }
    }
}

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/1072144.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号