本学期学了Java语言,就把书上前三个单元例题run了一下,并做好记录,每个例题所学到的知识点也做好注释,以便以后重新阅读时候,能够快速get到点。
public class HelloWord {
public static void main(String targs[]){
System.out.println("Hello rabbit YangNa!");
}
}
//第一个程序,输出函数的用法
import java.awt.*;
import java.applet.*;
public class HelloWordApplet extends Applet {
public void init(){
}
public void paint(Graphics g){
g.drawString("Hello rabbit YangNa!",25, 25);
}
}
//将之前的application程序改为Applet程序,可以用窗口表示输出
public class Assign {
public static void main (String args[]){
int x,y;
float z = 1.234f;
double w = 1.234;
boolean flag = true;
char c;
String str ;
String str1 = "Hi";
c = 'A';
str = "Bye";
x=12;
y=300;
System.out.println("x="+x);
System.out.println("y="+y);
System.out.println("z="+z);
System.out.println("w="+w);
System.out.println("str="+str);
System.out.println("str1="+str1);
System.out.println("c="+c);
}
}
//了解各种数据类型的定义和赋值用法
public class TestNumber {
public static void main (String[] args){
//ceil取上界
System.out.print(Math.ceil(5.2)+" ");
System.out.print(Math.ceil(5.6)+" ");
System.out.print(Math.ceil(-5.2)+" ");
System.out.print(Math.ceil(-5.6)+" ");
//floor取下界
System.out.print(Math.floor(5.2)+" ");
System.out.print(Math.floor(5.6)+" ");
System.out.print(Math.floor(-5.2)+" ");
System.out.print(Math.floor(-5.6)+" ");
//round四舍五入
System.out.print(Math.round(5.2)+" ");
System.out.print(Math.round(5.6)+" ");
System.out.print(Math.round(-5.2)+" ");
System.out.print(Math.round(-5.6)+" ");
}
}
public class CalSoldiery {
public static void main(String args[]){
for(int i = 1;i<1000;i++){
if(i%3==1&&i%7==5&&i%5==0){
System.out.println("应有士兵"+i+"人");//println=print+'\n'
//System.getProperty("line.seprator");
//break;
}
}
}
}
public class TestLeapYear {
public static void isLeapYear(int year){
boolean n1=(year%4==0);
boolean n2=(year0==0);
boolean n3=(year@0==0);
if((n1==true&&n2!=true)||(n2==true&&n3==true))
System.out.println(year+"年是闰年");
else
System.out.println(year+"年不是闰年");
}
public static void main(String args[]){
isLeapYear(1900);
isLeapYear(1904);
isLeapYear(2000);
}
}
//本题运用关系表达式简化了条件语句判断的复杂度,
//在同一个条件参与了多次判断的情况下,
//运用该方法,可以简化书写,便于阅读。
//boolean函数的用法
public class TestConditionExpression {
public static void main(String[] args){
float sum =1.5f;
int num=2;
System.out.println((sum<2?1:num/sum));
}
}
//输出为1.0而不是1,因为sum是浮点数,1发生了隐式转换
public class TestLogicSymbole {
public static void main(String[] args){
int out = 10;
boolean b1 = false;
if((b1!=true)&&(out+=10)==20)
System.out.println("equal,out="+out);
else
System.out.println("not equal,out="+out);
}
}
//逻辑判断语句,与C/C++类似
public class TestIf {
public static void main (String[] args){
int x,y;
x=7;y=1;
if(x>6)
if(y>6)
System.out.println("设备正常");
else
System.out.println("设备出错");
}
}
//else对应最近的那个if,Java语言不关注缩进
public class TestFor {
static boolean foo(char c){
System.out.print(c+" ");
return true;
}
public static void main(String[] argv){
int i=0;
for(foo('A');foo('B')&&(i<2);foo('C')){
i++;
foo('D');
}
}
}
//根据输出的字母来追踪循环体的执行顺序
public class CalChicken {
public static void main(String args[]){
int z=0;boolean isAnswer =false;
for(int i=1;i<=20;i++){
for(int j=1;j<=33;j++){
z=100-i-j;
if(z%3==0&&(5*i+3*j+z/3==100)){
System.out.println("公鸡"+i+"只,母鸡"+j+"只,小鸡"+z+"只");
isAnswer = true;
}
}
}
if(!isAnswer){
System.out.println("本题无解");
}
}
}
//isAnswer来标记是否有解,为bool变量
public class CountPrime {
public static void main(String[] args){
int n=1,m,j,k,i;
boolean h;
System.out.print(2+"\t");
for(i=3;i<=100;i+=2){
m=(int)Math.sqrt(i);//找到平方根,为检索最大因数
h=true;
for(j=2;j<=m;j++){
if(i%j==0){
h=false;//找到可以整除的因数,退出当前循环
break;
}
}
if(h){
if(n%6==0){//一行放6个数
System.out.println(" ");//利用println换行
}
System.out.print(i+"\t");//\t为制表符
n++;
}
}
System.out.println(" ");
System.out.print("一共有"+n+"个素数");
}
}
public class TestReturn {
public static void main(String[] args){
int i=10;
if(i<5){
return;
//i=6;
}
else{
//return;
}
i=5;
}
}
//尝试将注释改为代码,将发生错误
//因为return语句后面不能有可执行语句
public class TestArgs {
public static void main(String[] args){
for(int i=0;i<args.length;i++){
System.out.println(args[i]);
}
}
}
//编程打印:
//8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
//8 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8
//8 7 6 6 6 6 6 6 6 6 6 6 6 6 6 7 8
//8 7 6 5 5 5 5 5 5 5 5 5 5 5 6 7 8
//8 7 6 5 4 4 4 4 4 4 4 4 4 5 6 7 8
//8 7 6 5 4 3 3 3 3 3 3 3 4 5 6 7 8
//8 7 6 5 4 3 2 2 2 2 2 3 4 5 6 7 8
//8 7 6 5 4 3 2 1 1 1 2 3 4 5 6 7 8
//8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8
//8 7 6 5 4 3 2 1 1 1 2 3 4 5 6 7 8
//8 7 6 5 4 3 2 2 2 2 2 3 4 5 6 7 8
//8 7 6 5 4 3 3 3 3 3 3 3 4 5 6 7 8
//8 7 6 5 4 4 4 4 4 4 4 4 4 5 6 7 8
//8 7 6 5 5 5 5 5 5 5 5 5 5 5 6 7 8
//8 7 6 6 6 6 6 6 6 6 6 6 6 6 6 7 8
//8 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8
//8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
public class PrintSpecialArray {
public static void main(String args[]){
final int num = 8;//final用来定义常量,方阵的大小
int[][]t=new int[2*num+1][2*num+1];
for(int i=0;i<=num;i++){
for(int j=0;j<=num;j++){
if(i<j)t[i][j]=num-i;
else t[i][j]=num-j;
t[i][2*num-j]=t[i][j];
t[2*num-i][j]=t[i][j];
t[2*num-i][2*num-j]=t[i][j];
}
}
//输出方阵
for(int i=0;i<=2*num;i++){
for(int j=0;j<=2*num;j++){
System.out.print(t[i][j]+" ");
}
System.out.println("");
}
}
}
//先确定方阵的长度,规模为n时,长度应为2*n+1;
//然后用二重循环给数组赋值,矩阵以0为中心对称,故找到左上角的规律,再传递给其余三角即可;
//其规律为:n减去数组下标中最小的值
//编程输出数字斜塔:
//1 3 6 10 15
//2 5 9 14
//4 8 13
//7 12
//11
public class PrintXT {
public static void main(String[] args){
int n=5,colSpan=2,colSpanBase=2;
int arr[][]=new int[n][n];
arr[0][0]=1;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(j==n-i-1)break;
arr[i][j+1]=arr[i][j]+colSpan;
colSpan+=1;
}
if(i==n-1)break;
arr[i+1][0]=arr[i][0]+i+1;
colSpanBase+=1;
colSpan=colSpanBase;
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
System.out.print(arr[i][j]);
System.out.print('\t');
if(j==n-i-1)break;
}
System.out.println("");
}
}
}
//设n为行数colSpan为列加因子,colSpanBase为行加数因子;
//输出完一行之后再为下一行的首个元素赋值,调整行列加数因子
import java.awt.*;
import java.applet.*;
/*Java的注释方法同C/C++,有两种方式
* 这一种可以管理多行
* 但每行都以*开头方便阅读
* 但多行注释不可嵌套,否则违法
*/
public class HelloWordApplet extends Applet {
public void init(){
}
public void paint(Graphics g){
g.drawString("HelloWord!",75, 125);//坐标表示展示文字在框体内的位置
}
}
转载请注明原文地址: https://ju.6miu.com/read-39415.html