HelloWorld

借读《Java核心技术-卷I》的机会,对Java知识体系进行梳理,写博进行记录。

Eclipse:https://eclipse.org/downloads/ 

JDK:http://www.oracle.com/technetwork/java/javase/downloads/index.html

注意eclipse-inst需要用管理员模式运行,否则安装文件无法正常下载。

参考:http://stackoverflow.com/questions/32637399/error-installing-eclipse-ide-on-windows

编写一段代码进行调试

1 public class HelloWorld
2 {
3     public void main()
4     {
5         System.out.println("Hello World");
6     }
7 }

编译报错"Registry key Error: Java version has value '1.8', but '1.7' is required"

delete java.exe, javaw.exe and javaws.exe  from Windows\System32 问题解决

参考:http://stackoverflow.com/questions/29697543/registry-key-error-java-version-has-value-1-8-but-1-7-is-required

        http://stackoverflow.com/questions/26324486/properly-installing-java-8-along-with-java-7

之后编译报错"错误: 在类 HelloWorld 中找不到主方法, 请将主方法定义为:public static void main(String[] args)"

代码修改为

1 public class HelloWorld
2 {
3     public static void main(String[] args)
4     {
5         System.out.println("Hello World");
6     }
7 }

问题解决

存疑:为什么main一定要写成 public static void main(String[] args) 

 

Eclipse 修改JRE

eclipse\eclipse.ini

Build path→Configer Build path,Libraries这个选项卡中选择你当前的JRE,点击删除。Add Library ,选择JRE System Library ,next,Alternate JRE ,配一个当前的JDK

问题未解决,修改后还是JRE7

 

PrintWriter

 1 package sample;
 2 import java.util.*;
 3 import java.io.FileNotFoundException;
 4 import java.io.PrintWriter;
 5 public class Sample {
 6 
 7     public static void main(String[] args) throws FileNotFoundException {
 8         // TODO Auto-generated method stub
 9         //System.out.printf("%,2f",10000.0/3.0);
10         Scanner in =new Scanner(System.in);
11         System.out.printf("Name?");
12         String name = in.nextLine();
13         System.out.printf("Hello "+name);
14         PrintWriter out= new PrintWriter("D:\\Issac\\JavaDev\\JavaSample160515\\src\\sample\\readme.txt");
15         out.print("Name?");
16         out.println("Hello "+name);
17         out.close();
18         //System.exit(0);
19     }
20 
21 }

需要加入 FileNotFoundException

结束时需要close,否则不会保存到文件

参考:http://stackoverflow.com/questions/11496700/how-to-use-printwriter-and-file-classes-in-java

 

数组

        final int NMAX=10;
        int[][] odds=new int[NMAX+1][];
        for (int n=0;n<=NMAX;n++)
            odds[n]=new int[n+1];
        
        for (int n=0;n<odds.length;n++)
            for(int k=0;k<odds[n].length;k++)
            {
                int lotteryOdds=1;
                for (int i=1;i<=k;i++)
                    lotteryOdds=lotteryOdds*(n-i+1)/i;
                odds[n][k]=lotteryOdds;
            }
        for(int[]row:odds)
        {
            for(int odd:row)
                System.out.printf("%4d", odd);
            System.out.println();
        }

输出:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1

posted @ 2016-05-15 15:23  IssacPan  阅读(177)  评论(0)    收藏  举报