java语言程序设计基础班
leafinsnowfield
这个作者很懒,什么都没留下…
展开
-
插入排序
//插入排序public class InsertionSort { public static void insertionSort(double[] list) { for(int i=1;i<list.length;i++) { double currentElement=list[i]; int k; for(k=i-1;k>=0&&list[k]>curr原创 2015-08-22 15:15:56 · 247 阅读 · 0 评论 -
生成随机数
public class RandomCharacter { public static char getRandomCharacter(char ch1,char ch2) { return (char)(ch1+Math.random()*(ch2-ch1+1)); } public static char getRandomLowerCharacter() { return原创 2015-08-19 19:03:36 · 348 阅读 · 0 评论 -
计算身体质量指数
import java.util.Scanner;public class ComputeAndInterpretBMI { public static void main(String [] args) { Scanner input = new Scanner(System.in); System.out.print("Enter weight in pounds:");原创 2015-08-19 00:25:56 · 1413 阅读 · 0 评论 -
浮点数在计算机中不能准确表示范例
public class TestSum { public static void main(String [] args) { float sum = 0; for(float i=0.01f;i<=1.0f;i+=0.01f)//结果50.499985最后一个i比1大且浮点数导致结果不准确 { sum+=i; } System.out.println("The s原创 2015-08-19 16:23:48 · 559 阅读 · 0 评论 -
测试某个数是否为素数
public class PrimeNumber { public static void main(String [] args) { final int NUMBER_OF_PRIMES=50; final int NUMBER_OF_PRIMES_PER_LINE=10; int count=0; int number=2; System.out.println("T原创 2015-08-19 16:48:56 · 396 阅读 · 0 评论 -
10进制转16 进制
import java.util.Scanner;public class Decimal2HexConversion { public static void main(String [] args) { Scanner input =new Scanner(System.in); System.out.print("Enter a decimal number:"); int原创 2015-08-19 18:51:17 · 569 阅读 · 0 评论 -
求最大公倍数
import java.util.Scanner;public class GreatestCommonDivisor { public static void main(String [] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first integer: "); int原创 2015-08-19 16:31:12 · 420 阅读 · 0 评论 -
打印日历
import java.util.Scanner;public class PrintCalendar { public static void main(String [] args) { Scanner input=new Scanner(System.in); System.out.print("Enter full year(e.g. 2001)"); int year=原创 2015-08-19 19:36:55 · 264 阅读 · 0 评论 -
乐透号码
import java.util.Scanner;public class LottoNumbers { public static void main(String [] args) { Scanner input=new Scanner(System.in); boolean [] isCovered=new boolean[99]; int number=input.ne原创 2015-08-19 20:15:42 · 419 阅读 · 0 评论 -
一副牌
public class DeckOfCards { public static void main(String [] args) { int [] deck=new int[52]; String [] suits={ "Spades","Hearts","Diamonds","Clubs" }; String [] ranks={ "Ace","2","3原创 2015-08-19 20:30:31 · 455 阅读 · 0 评论 -
线性查找
public class LinearSearch { public static int linerSearch(int[] list,int key) { for(int i=0;i<list.length;i++) { if(key==list[i]) return i; } return -1; }}原创 2015-08-22 14:52:23 · 438 阅读 · 0 评论 -
二分查找法
//二分查找法public class BinarySearch { public static int binarySearch(int[] list,int key) { int low=0; int high=list.length-1; while(high>=low) { int mid=(low+high)/2; if(key<list[mid])原创 2015-08-22 15:02:49 · 239 阅读 · 0 评论 -
选择排序
//选择查找public class SelectionSort { public static void selectionSort(double[] list) { for(int i=0;i<list.length-1;i++) { double currentMin=list[i]; int currentMinIndex=i; for(int j=i+1;原创 2015-08-22 15:09:16 · 328 阅读 · 0 评论 -
Arrays类中sort,binarySearch,fill实例
import java.util.Arrays.*;public class Array { public static void main(String [] args) { double[] numbers={6.0,4.4,1.9,2.9,3.4,3.5}; java.util.Arrays.sort(numbers); for(int i=0;i<numbers.leng原创 2015-08-22 15:32:31 · 799 阅读 · 0 评论 -
使用确定对话框来控制循环
import javax.swing.JOptionPane;public class SentinelValueUsingConfirmationDialog { public static void main(String [] args) { int sum=0; int option=JOptionPane.YES_OPTION; while(option==JOptio原创 2015-08-19 16:56:05 · 520 阅读 · 0 评论 -
蒙特卡罗模拟(使用随机数和概率来解决问题)
public class MonteCarloSimulation {//求出pi的值 public static void main(String [] args) { final int NUMBER_OF_TRIALS=10000000; int numberOfHits=0; for(int i=0;i<NUMBER_OF_TRIALS;i++) { double原创 2015-08-19 16:41:35 · 7411 阅读 · 0 评论 -
可变长参数列表
public class VarArgsDemo {//可变长参数列表 public static void main(String [] args) { printMax(34,3,3,2,56,5); printMax(new double[]{1,2,3}); } public static void printMax(double...numbers) { if原创 2015-08-22 14:49:46 · 400 阅读 · 0 评论 -
九宫图
import java.util.Scanner;public class CheckSudokuSolution { public static void main(String[] args) { int[][] grid=readASolution(); System.out.println(isValid(grid)?"Valid solution":"Invalid sol原创 2015-08-22 16:30:02 · 766 阅读 · 0 评论 -
检测回文字符串
import java.util.Scanner;public class CheckPalindrome { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("Enter a string: "); String s=input.nex原创 2015-08-30 13:29:22 · 520 阅读 · 0 评论 -
使用字符生成器的reverse功能的会问字符串的检测
import java.util.Scanner;//使用字符生成器的reverse功能的会问字符串的检测public class PalindromeIgnoreNonAlphanumeric { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.pr原创 2015-08-30 14:51:03 · 370 阅读 · 0 评论 -
计算器
//计算器public class Calculator { public static void main(String[] args) { if(args.length!=3) { System.out.println("Usage: java Calculator operand1 operator operand2"); System.exit(0); }原创 2015-08-30 14:51:57 · 378 阅读 · 0 评论 -
设计堆栈类
public class StackOfIntegers { private int[] elements; private int size; public static final int DEFAULT_CAPACITY=16; public StackOfIntegers() { this(DEFAULT_CAPACITY); } public StackOfInte原创 2015-08-31 20:36:44 · 1144 阅读 · 0 评论 -
运用Polygon类绘制六边形
import javax.swing.JPanel;import java.awt.Graphics;import java.awt.Polygon;public class PolygonsPanel extends JPanel { protected void paintComponent(Graphics g) { super.paintComponent(g); in原创 2015-09-03 21:15:01 · 5609 阅读 · 0 评论 -
运用comparable类对数据对象做排序
public class GenericSort { public static void main(String[] args) { Integer[] intArray={ new Integer(2),new Integer(4),new Integer(3) }; Double[] doubleArray={ new Double(3.4),new Doub原创 2015-09-02 18:39:57 · 402 阅读 · 0 评论 -
有理数类Rational实例
public class Rational extends Number implements Comparable { private long numerator=0; private long denominator=1; public Rational() { this(0,1); } public Rational(long numerator,long denomina原创 2015-09-02 20:08:19 · 2377 阅读 · 0 评论 -
猜生日
import java.util.Scanner;public class GuessBirthday { public static void main(String [] args) { String set1 = "1 3 5 7\n" + "9 11 13 15\n" + "17 19 21 23\n" + "25 17 29 31\n"原创 2015-08-19 00:05:32 · 454 阅读 · 0 评论 -
BigInteger和BigDecimal的实例
import java.math.*;public class LargeFactorial { public static void main(String[] args) { System.out.println("50! is \n"+factorial(50)); } public static BigInteger factorial(long n) { BigIn原创 2015-09-02 18:44:34 · 317 阅读 · 0 评论 -
自定义栈类
public class MyStack { private java.util.ArrayList list=new java.util.ArrayList(); public boolean isEmpty() { return list.isEmpty(); } public int getSize() { return list.size(); } public O原创 2015-09-01 19:46:34 · 951 阅读 · 0 评论 -
Cloneable接口实例
public class House implements Cloneable, Comparable { private int id; private double area; private java.util.Date whenBuilt; public House(int id,double area) { this.id=id; this.area=area;原创 2015-09-02 17:13:29 · 393 阅读 · 0 评论 -
八皇后问题
import java.awt.*;import javax.swing.*;public class EightQueens extends JApplet{ public static final int SIZE=8; private int[] queens=new int[SIZE]; public EightQueens() { search(0); add(ne原创 2015-09-15 10:21:32 · 396 阅读 · 0 评论