//这是个获取当前时间的简单实例,如下: //-------------------------------------------- import java.util.*; import java.text.*;
public class NowString { public static void main(String[] args) { Date now = new Date(); DateFormat d = DateFormat.getDateInstance(); String str = d.format(now); System.out.println("Today is " + str); } } //--------------------------------------------
----------------解决方案--------------------------------------------------------
//-------java小程序(6)
//输入一个整数,然后输出各位数的数值.如下:
//--------------------------------------------
import java.io.*;
class GetNumber
{
private static InputStreamReader isr; //用作键盘输入
private static BufferedReader in;
private static String inputValue;
private static String getString() //此函数用于在键盘输入一个字符串
{
isr=new InputStreamReader(System.in);
in=new BufferedReader(isr);
System.out.print("请输入任意不超过long整型数:");
try
{
inputValue=in.readLine();
}
catch(Exception e)
{
System.out.println("输入出错!");
}
return inputValue;
}
private static int getLength(String str) //此函数用于返回字符
{
return str.length();
}
private static long toLong(String str) //将字符串转成长整型
{
return Long.parseLong(str);
}
private static void outPut(long value,int l)
{
int bit=1;
for(int i=1;i<l;i++)
{
bit*=10;
}
for(;l>0;l--)
{
System.out.println("第"+l+"位上的数字是:"+value/bit);
value-=(value/bit)*bit;
bit/=10;
}
}
public static void main(String[] args)
{
String stringValue;
stringValue=getString();
int l=getLength(stringValue);
long longValue=toLong(stringValue);
outPut(longValue,l);
//System.out.println("Hello World!");
}
}
原理:如输入一个3位数693:
则
百位数=693/100;
十位数=(693-100*6)/10;
个位数=(693-100*6)%10;
//--------------------------------------------
----------------解决方案--------------------------------------------------------
//-------java小程序(7)
//打印1~100的素数,并求出他们的和!
public class ss {
public static void main(String[] args) {
int s=0;
System.out.print("1 ");
for (int i=2;i<101;i=i+1) {
int a=0;
for (int j=2;j<101;j=j+1) {
if (i%j==0) {
a++;
s+=i;
}
}
if (a==1) {
System.out.print( i + " " );
}
}
System.out.println(" ");
System.out.println("1-100之间素数的和为"+s);
}
}
----------------解决方案--------------------------------------------------------
//-------java小程序(8)
//画正弦曲线的程序
//--------------------------------------------
import java.applet. *;
import java.awt.*;
import java.awt.event.*;
public class zxqx extends Applet implements ActionListener
{
int x,y;
double a;
Button bn1=new Button("Sin波形");
Button bn2=new Button("清除");
public void init()
{
add(bn1);
add(bn2);
bn1.addActionListener(this);
bn2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Graphics g=getGraphics();
if(e.getSource()==bn1)
{
for(x=0;x<=360;x+=9)
{
a=Math.sin(x*Math. PI/180);
y=(int)(80+40*a);
g.drawString("*",x,y);
}
}
if(e.getSource()==bn2)
repaint();
}
}
//--------------------------------------------
----------------解决方案--------------------------------------------------------
注意:运行以上的Applet程序时,应用IE或Netscape打开网页,用腾讯的TT不能运行,因为TT中没有集成JVM.
----------------解决方案--------------------------------------------------------
//各种数字类型转换成字符串型: String s = String.valueOf( value); //其中value为任意一种数字类型。 字符串型转换成各种数字类型: String s = "169"; byte b = Byte.parseByte( s ); short t = Short.parseShort( s ); int i = Integer.parseInt( s ); long l = Long.parseLong( s ); Float f = Float.parseFloat( s ); Double d = Double.parseDouble( s ); 数字类型与数字类对象之间的转换: byte b = 169; Byte bo = new Byte( b ); //bo为类对象 b = bo.bytevalue(); short t = 169; Short to = new Short( t ); //to类似bo,以下都类似 t = to.shortvalue(); int i = 169; Integer io = new Integer( i ); i = io.intvalue(); long l = 169; Long lo = new Long( l ); l = lo.longvalue(); float f = 169f; Float fo = new Float( f ); f = fo.floatvalue(); double d = 169f; Double dObj = new Double( d ); d = dObj.doublevalue(); 实际应用中还有可能会用到各数字类型与字节数组间的转换, Java 没有直接提供这样的支持。
----------------解决方案--------------------------------------------------------
//-------java小程序(9)
//此程序实现画线或填充图形(矩形,圆形)等
//--------------------------------------------
import java.applet.Applet;
import java.awt.Graphics;
public class Ex6_1 extends Applet
{
public void paint(Graphics g)
{
g.drawRect(0,0,200,300);
g.drawLine(0,0,300,400);
g.drawRoundRect(20,20,200,300,30,400);
g.fillRect(40,80,50,90);
g.drawOval(150,120,70,40);
g.fillOval(100,100,200,200);
g.drawString("They are figures",400,300);
}
}
//--------------------------------------------
----------------解决方案--------------------------------------------------------
//下面举几个数组实例: //-------------------------------------------- public class Array1 { public static void main(String[] args) { int[] a1 = {10,20,30,40,50}; for (int i = 0; i < a1.length; ++i) System.out.print(a1[i] + " "); System.out.println(); } } //一维数组比较简单,下面举个三行两列的二维数组,如下: public class Array2 { public static void main(String[] args) { int[][] a2 = {{1,2}, {3,4}, {5,6}}; for (int j = 0; j < a2.length; ++j) //a2.length为3 { for (int k = 0; k < a2[j].length; ++k)//a2[j].length为2 System.out.print(a2[j][k] + " "); System.out.println(); } } }
//这个是3维数组的例子 public class Array3 { public static void main(String[] args) { int[] a1 = {10,20,30,40,50}; int[][] a2 = {{1,2}, {3,4}, {5,6}}; //对于多维数组的定义,定义时一定要定义列数,此数组 //定义为2列 int[][][] a3 = new int[2][][]; a3[0] = a2; a3[1] = new int[2][]; a3[1][0] = a1; a3[1][1] = new int[1]; a3[1][1][0] = 99; for (int p = 0; p < a3.length; ++p) { for (int q = 0; q < a3[p].length; ++q) { for (int r = 0; r < a3[p][q].length; ++r) System.out.print(a3[p][q][r] + " "); System.out.println(); } System.out.println(); } System.out.println(); } } //--------------------------------------------
----------------解决方案--------------------------------------------------------
//标号(label)在c语言中一般很少用,而利用循环语句或条件语句来实现跳转,在java中可有
//label,举个简单的例子:
//--------------------------------------------
public class Label {
public static void main(String[] args) {
outer:
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
System.out.println("i: " + i + ", j: " + j);
if (i + j == 2)
continue outer;
if (i + j == 4)
break outer;
}
}
}
}
//--------------------------------------------
//当i+j等于4时循环才结束.
----------------解决方案--------------------------------------------------------
[QUOTE] //求各数据类型的最值 //-------------------------------------------- class Limits { public static void main(String[] args) { System.out.println("Byte: [" + Byte.MIN_VALUE + "," + Byte.MAX_VALUE + "]"); System.out.println("Short: [" + Short.MIN_VALUE + "," + Short.MAX_VALUE + "]"); System.out.println("Integer: [" + Integer.MIN_VALUE + "," + Integer.MAX_VALUE + "]"); System.out.println("Long: [" + Long.MIN_VALUE + "," + Long.MAX_VALUE + "]"); System.out.println("Float: [" + Float.MIN_VALUE + "," + Float.MAX_VALUE + "]"); System.out.println("Double: [" + Double.MIN_VALUE + "," + Double.MAX_VALUE + "]"); } } [/QUOTE] //-------------------------------------------- //以后的程序都用"引用"发表,字体放大些,这样看起来没那么费力。
----------------解决方案--------------------------------------------------------