java积累
数组的使用
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);
}
}
java积累的更多相关文章
- Java 积累复习用
1.jvm 默认编码:Java的默认编码 2.jvm heap : Java虚拟机的内存组成以及堆内存介绍 3.Java命令学习系列(一)--Jps 4.Java命令学习系列(二)--Jstack 5 ...
- Selenium Webdriver java 积累一
Selenium Webdriver 学习: http://jarvi.iteye.com/category/203994 https://github.com/easonhan007/webdriv ...
- java积累的细节问题
一.将几个数字进行拼接,拼接成字符串 一定要将""放到几个数字之前,否则就会把数字相加之后的数转化成字符串 如你需要拼接年份,月份,和日 如果:2018+12+13+"& ...
- 【转载】分享下多年积累的对JAVA程序员成长之路的总结
注:该文是从百度贴吧转载过来,之前看到觉得写得还不错,对Java开发学习者来说很有意义的,可以看看. 我也搞了几年JAVA了,由于一向懒惰,没有成为大牛,只是一普通程序猿,不爱玩社交网站,不爱玩微博, ...
- 分享下多年积累的对JAVA程序员成长之路的总结
http://blog.csdn.net/zhongzelin/article/details/8643269我也搞了几年JAVA了,由于一向懒惰,没有成为大牛,只是一普通程序猿,不爱玩社交网站,不爱 ...
- java 基础导航
ecplise 常用快捷键 java notepad++ java封装好处和原则 java1 基本概述和java环境变量配置 java2 基本概念介绍和基本关键字.基本数据类型 java3 基本流程语 ...
- 分享下对JAVA程序员成长之路的总结<转>
我也搞了几年JAVA了,由于一向懒惰,没有成为大牛,只是一普通程序猿,手痒来给新人分享下从新手成长为老鸟的已见. 首先初识语法的阶段,必须要学会怎么操作对象,操作if和for,操作list set ...
- 2019年京东Java研发岗社招面经(面试经历+真题总结+经验分享)!
本篇先以日历形式回顾秋招之路,方便各位参考某厂的处理进度:然后是总结归纳春秋招Java面试题库:最后做个总结还有展望,开始新的征程~ 面试经历京东面试真题面试经验分享1.面试经历 2018年的冬季特别 ...
- java学习大方向
总结Java程序员成长之路 转载 http://bbs.javazhijia.com/topic/1bb0733f80d94aedb50cc3b66d9792b6.html 我也搞了几年JAVA ...
随机推荐
- ajax读取txt文件
注意url为网络路径 <html><head><script type="text/javascript"> function loadXML ...
- ZRender源码分析6:Shape对象详解之路径
开始 说到这里,就不得不提SVG的路径操作了,因为ZRender完全的模拟了SVG原生的path元素的用法,很是强大. 关于SVG的Path,请看这里: Path (英文版) 或者 [MDN]SVG教 ...
- your local changes would be overwritten by merge. commit stash or revert them to proceed. view them
error log: your local changes would be overwritten by merge. commit stash or revert them to proceed. ...
- java Timer 使用小结
Java自带的java.util.Timer类,通过调度一个java.util.TimerTask任务.这种方式可以让程序按照某一个频度执行,但不能指定时间运行.用的较少. 任务的调用通过起的子线程进 ...
- Flink Program Guide (8) -- Working with State :Fault Tolerance(DataStream API编程指导 -- For Java)
Working with State 本文翻译自Streaming Guide/ Fault Tolerance / Working with State ---------------------- ...
- td太多内容显示...
table style="table-layout:fixed;"td style="text-overflow: ellipsis;white-space: nowra ...
- SQL Server 数据库文件 4 点注意
1.数据库被分解成逻辑页面,每页8K,在每一个文件中页面从 0 --> x 连续编号: 2.可以通过 [database_id] [file_id] [page_id] 的形式引用页面: ...
- ApiDemos示例学习(1)——ApiDemos示例的导入
---恢复内容开始--- 今天准备开始写这个ApiDemos示例的学习日记了,放在网上以监督自己! 首先是导入该示例.如果我们在配置Android开发环境是,利用Android SDK 安装包中的SD ...
- Delphi下获取IE的UserAgent的方法
方法一:使用SHDocVw, MSHtml单元提供的一些方法利用浏览器的特性来获取. uses SHDocVw, MSHtml; function GetUserAgent: string;var ...
- docker 数据管理3
实际应用: 第一个容器使用: docker run -itd -v /data/:/data1 centos bash // -v 用来指定挂载目录, 后面的容器使用之前的容器数据卷 docker: ...