数组的使用
package javaDemo;import java.util.*;/** * * @author Administrator * @version 1.0 * * */public class test { public static void main(String[] args) { String[] greeting = new String[3]; greeting[0] = "Welcome to Core Java"; greeting[1] = "by Cay Horstmann"; greeting[2] = "and Gary Cornell"; for (String g : greeting) System.out.println(g); } }
eclipse格式化快捷键Ctrl+Shift+F
jFrame
package javaDemo;/** * * @author Administrator * @version 1.0 * * */import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;/** * A program for viewing images. */public class ImageViewer { public static void main(String[] args) { JFrame frame = new ImageViewerFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }}/** * A frame with a label to show an image. */class ImageViewerFrame extends JFrame { public ImageViewerFrame() { setTitle("ImageViewer"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // use a label to display the images label = new JLabel(); add(label); // set up the file chooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File(".")); // set up the menu bar JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu menu = new JMenu("文件"); menuBar.add(menu); JMenuItem openItem = new JMenuItem("打开"); menu.add(openItem); openItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { // show file chooser dialog int result = chooser.showOpenDialog(null); // if file selected, set it as icon of the label if (result == JFileChooser.APPROVE_OPTION) { String name = chooser.getSelectedFile().getPath(); label.setIcon(new ImageIcon(name)); } } }); JMenuItem exitItem = new JMenuItem("退出"); menu.add(exitItem); exitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); } private JLabel label; private JFileChooser chooser; private static final int DEFAULT_WIDTH = 400; private static final int DEFAULT_HEIGHT = 500;}
BigIntegerTest
package javaDemo; //首先要引入包/** * @version 1.20 2004-02-10 * @author Cay Horstmann * **/import java.math.*; //引入一些math相关的东西import java.util.*;public class BigIntegerTest //类名和文件名要相同{ public static void main(String[] args) //主函数也就是入口函数 { Scanner in = new Scanner(System.in); //键盘输入的对象 System.out.print("How many numbers do you need to draw? "); int k = in.nextInt(); //获取数据,存入变量k中 System.out.print("What is the highest number you can draw? "); int n = in.nextInt(); //获取数据,存入变量n中 /* compute binomial coefficient n * (n - 1) * (n - 2) * . . . * (n - k + 1) ------------------------------------------- 1 * 2 * 3 * . . . * k */ BigInteger lotteryOdds = BigInteger.valueOf(1); for (int i = 1; i <= k; i++) lotteryOdds = lotteryOdds .multiply(BigInteger.valueOf(n - i + 1)) .divide(BigInteger.valueOf(i)); System.out.println("Your odds are 1 in " + lotteryOdds + ". Good luck!");//进行一些处理 }}
稍微复杂的算法
package javaDemo; //首先要引入包/** @version 1.40 2004-02-10 @author CayHorstmann*/public class CompoundInterest{ public static void main(String[] args) { final int STARTRATE = 10; //定义常量 final int NRATES = 6; final int NYEARS = 10; // set interest rates to 10 . . . 15% double[] interestRate = new double[NRATES]; //新建数组 for (int j = 0; j < interestRate.length; j++) interestRate[j] = (STARTRATE + j) / 100.0; //给数组赋值 double[][] balances = new double[NYEARS][NRATES]; //定义二维数组 // set initial balances to 10000 for (int j = 0; j < balances[0].length; j++) balances[0][j] = 10000; // compute interest for future years for (int i = 1; i < balances.length; i++) { for (int j = 0; j < balances[i].length; j++) { // get last year's balances from previous row double oldBalance = balances[i - 1][j]; // compute interest double interest = oldBalance * interestRate[j]; // compute this year's balances balances[i][j] = oldBalance + interest; } } // print one row of interest rates for (int j = 0; j < interestRate.length; j++) System.out.printf("%9.0f%%", 100 * interestRate[j]); System.out.println(); // print balance table for (double[] row : balances) { // print table row for (double b : row) System.out.printf("%10.2f", b); System.out.println(); } }}
同一个包下可以互相引用类,并使用其中方法
CompoundInterest comp = new CompoundInterest();comp.main(args);
package javaDemo; //首先要引入包/** @version 1.10 2004-02-10 @author Cay Horstmann*/import java.util.*;public class InputTest{ public static void main(String[] args) { Scanner in = new Scanner(System.in); // get first input System.out.print("你叫什么名字? "); String name = in.nextLine(); // get second input System.out.print("你多大了? "); int age = in.nextInt(); // display output on console System.out.println("你好, " + name + ". 明年, 你的年龄将会是 " + (age + 1)); }}
如何调整console字体大小?
打开window - preferences-- general - appearance - colors and fonts --debug - console font 就可以调节了。
我表示看不懂,这算法太复杂了,工作这么久,都不懂。
package javaDemo;/** @version 1.20 2004-02-10 @author Cay Horstmann*/public class LotteryArray{ public static void main(String[] args) { final int NMAX = 5; //定义常量 // allocate triangular array int[][] odds = new int[NMAX + 1][]; //定义数组 for (int n = 0; n <= NMAX; n++) odds[n] = new int[n + 1]; // fill triangular array for (int n = 0; n < odds.length; n++) for (int k = 0; k < odds[n].length; k++) { /* compute binomial coefficient n * (n - 1) * (n - 2) * . . . * (n - k + 1) ------------------------------------------- 1 * 2 * 3 * . . . * k */ int lotteryOdds = 1; for (int i = 1; i <= k; i++) lotteryOdds = lotteryOdds * (n - i + 1) / i; odds[n][k] = lotteryOdds; } // print triangular array for (int[] row : odds) { for (int odd : row) System.out.printf("%4d", odd); System.out.println(); } }}
表示作者很牛逼。
/** @version 1.20 2004-02-10 @author Cay Horstmann*/package javaDemo;import java.util.*;public class LotteryDrawing{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("How many numbers do you need to draw? "); int k = in.nextInt(); System.out.print("What is the highest number you can draw? "); int n = in.nextInt(); // fill an array with numbers 1 2 3 . . . n int[] numbers = new int[n]; //定义一维数组 for (int i = 0; i < numbers.length; i++) numbers[i] = i + 1;//赋值 // draw k numbers and put them into a second array int[] result = new int[k]; for (int i = 0; i < result.length; i++) { // make a random index between 0 and n - 1 int r = (int) (Math.random() * n); //直接使用Math的random方法 // pick the element at the random location result[i] = numbers[r]; // move the last element into the random location numbers[r] = numbers[n - 1]; n--; } // print the sorted array Arrays.sort(result); //排序数组 System.out.println("Bet the following combination. It'll make you rich!"); for (int r : result) System.out.println(r); }}