JAVA 基础入门课本例题代码(第二章)
Example01
public class Example01 {
public static void main (String[] ags)
{
int num=4;
byte b=(byte)num;
System.out.println(b);
}
}
Example02
public class Example02 {
public static void main (String[] ags)
{
byte a;
int b=298;
a=(byte)b;
System.out.println("a="+a);
System.out.println("b="+b);
}
}
Example03
public class Example03 {
public static void main (String[] ags)
{
byte a=1;
byte b=2;
byte c=(byte)(a+b);
System.out.println("c="+c);
}
}
Example04
public class Example04 {
public static void main (String[] ags)
{
int a=12;
{
int b=13;
System.out.println("a="+a);
System.out.println("b="+b);
}
//b=a;
System.out.println("a="+a);
//System.out.println("b="+b);
}
}
Example05
public class Example05 {
public static void main (String[] ags)
{
short s=1;
int a=10;
s+=a;
System.out.println("s="+s);
}
}
Example06
public class Example06 {
public static void main (String[] ags)
{
int a,b,c;
a=b=c=0;
boolean x,y;
x=a>0&b++>1;
System.out.println(x);
System.out.println("b="+b);
y=a>0&&c++>1;
System.out.println(y);
System.out.println("c="+c);
}
}
Example07
public class Example07 {
public static void main (String[] ags)
{
int x=5;
if(x<10){
x++;
}
System.out.println("x="+x);
}
}
Example08
public class Example08 {
public static void main (String[] ags)
{
int num=19;
if(num%2==0){
System.out.println("这是一个偶数");
}
else{
System.out.println("这是一个奇数");
}
}
}
Example09
public class Example09 {
public static void main (String[] ags)
{
int great=75;
if(great>80){
System.out.println("该成绩的登级为优等");
}
else if(great>70){
System.out.println("该成绩的登级为良");
}
else if (great>60){
System.out.println("该成绩的登级为中");
}
else{
System.out.println("该成绩的登级为差");
}
}
}
Example10
public class Example10 {
public static void main (String[] ags)
{
int week=5;
switch (week){
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期天");
break;
default:
System.out.println("输入的数字不正确.....");
}
}
}
Example11
public class Example11 {
public static void main (String[] ags)
{
int week=2;
switch (week){
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("今天是工作日");
break;
case 6:
case 7:
System.out.println("今天是休息日");
break;
default:
System.out.println("输入的数字不正确.....");
}
}
}
Example12
public class Example12 {
public static void main (String[] ags)
{
int x=1;
while (x<=4){
System.out.println("x="+x);
x++;
}
}
}
Example13
public class Example13 {
public static void main (String[] ags)
{
int x=1;
do{
System.out.println("x="+x);
x++;
}while (x<=4);
}
}
Example14
public class Example14 {
public static void main (String[] ags)
{
int sum=0;
for(int i=1;i<=4;i++){
sum+=i;
}
System.out.println("sum="+sum);
}
}
Example15
public class Example15 {
public static void main (String[] ags)
{
int i,j;
for(i=1;i<=9;i++){
for(j=1;j<=i;j++){
System.out.print("*");
}
System.out.print("\n");
}
}
}
Example16
public class Example16 {
public static void main (String[] ags)
{
int x=1;
while (x<=4){
System.out.println("x="+x);
if(x==3){
break;
}
x++;
}
}
}
Example17
public class Example17 {
public static void main (String[] ags)
{
int i,j;
itcast:for(i=0;i<9;i++){
for(j=0;j<i;j++){
if(i==3)
break itcast;
System.out.print("*");
}
System.out.print("\n");
}
}
}
Example18
public class Example18 {
public static void main (String[] ags)
{
int sum=0;
int i;
for(i=1;i<=100;i++){
if(i%2==0)
continue;
sum+=i;
}
System.out.println("sum="+sum);
}
}
Example19
public class Example19 {
public static void main (String[] ags)
{
for(int i=0;i<3;i++){
for(int j=0;j<5;j++){
System.out.print("*");
}
System.out.print("\n");
}
System.out.print("\n");
for(int i=0;i<2;i++){
for(int j=0;j<4;j++){
System.out.print("*");
}
System.out.print("\n");
}
System.out.print("\n");
for(int i=0;i<6;i++){
for(int j=0;j<10;j++){
System.out.print("*");
}
System.out.print("\n");
}
System.out.print("\n");
}
}
Example20
public class Example20 {
public static void main (String[] ags)
{
printRectaing(3,5);
printRectaing(2,4);
printRectaing(6,10);
}
public static void printRectaing(int height,int width){
for(int i=0;i<height;i++){
for(int j=0;j<width;j++){
System.out.print("*");
}
System.out.print("\n");
}
System.out.print("\n");
}
}
Example21
public class Example21 {
public static void main (String[] ags)
{
int area=getAear(3,5);
System.out.println("There area is "+area);
}
public static int getAear(int i,int j){
return i*j;
}
}
Example22
public class Example22 {
public static void main (String[] ags)
{
int sum01=add01(1,2);
int sum02=add02(1,2,3);
double sum03=add03(1.2,2.3);
System.out.println("sum01="+sum01);
System.out.println("sum02="+sum02);
System.out.println("sum03="+sum03);
}
public static int add01(int x,int y){
return x+y;
}
public static int add02(int x,int y,int z){
return x+y+z;
}
public static double add03(double x,double y){
return x+y;
}
}
Example23
public class Example23 {
public static void main (String[] ags)
{
int sum01=add(1,2);
int sum02=add(1,2,3);
double sum03=add(1.2,2.3);
System.out.println("sum01="+sum01);
System.out.println("sum02="+sum02);
System.out.println("sum03="+sum03);
}
public static int add(int x,int y){
return x+y;
}
public static int add(int x,int y,int z){
return x+y+z;
}
public static double add(double x,double y){
return x+y;
}
}
Example24
public class Example24 {
public static void main (String[] ags)
{
int sum=getSum(4);
System.out.println("sum="+sum);
}
public static int getSum(int n){
if(n==1){
return 1;
}
else {
int temp=getSum(n-1);
return temp+n;
}
}
}
Example25
public class Example25 {
public static void main (String[] ags)
{
int[]arr;
arr=new int[4];
System.out.println("arr[0]="+arr[0]);
System.out.println("arr[1]="+arr[1]);
System.out.println("arr[2]="+arr[2]);
System.out.println("arr[3]="+arr[3]);
}
}
Example26
public class Example26 {
public static void main (String[] ags)
{
int []arr= new int [4];
arr[0]=1;
arr[1]=2;
System.out.println("arr[0]="+arr[0]);
System.out.println("arr[1]="+arr[1]);
System.out.println("arr[2]="+arr[2]);
System.out.println("arr[3]="+arr[3]);
}
}
Example27
public class Example27 {
public static void main (String[] ags)
{
int[]arr={1,2,3,4};
System.out.println("arr[0]="+arr[0]);
System.out.println("arr[1]="+arr[1]);
System.out.println("arr[2]="+arr[2]);
System.out.println("arr[3]="+arr[3]);
}
}
Example28
public class Example28 {
public static void main (String[] ags)
{
int []arr=new int [4];
System.out.println("arr[3]="+arr[3]);
//System.out.println("a[4]="+a[4]);
}
}
/**
这部分代码是为了验证数组的角标是从0开始的,四个数组元素即是arr[0]到arr[3]
*/
Example29
public class Example29 {
public static void main (String[] ags)
{
int []arr=new int [2];
System.out.println("arr[0]="+arr[0]);
System.out.println("arr[1]="+arr[1]);
arr=null;
System.out.println("arr[0]="+arr[0]);
System.out.println("arr[1]="+arr[1]);
}
}
Example30
public class Example30 {
public static void main (String[] ags)
{
int[]arr={1,2,3,4};
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
}
Example31
public class Example31 {
public static void main (String[] ags)
{
int []arr={8,4,3,5,7,3};
int max=getMax(arr);
System.out.println(max);
}
static int getMax(int []arr){
int max=arr[0];
for(int i=1;i<arr.length;i++){
if(max<arr[i])
max=arr[i];
}
return max;
}
}
Example32
public class Example32 {
public static void main (String[] ags)
{
int []arr={9,8,3,5,2};
System.out.print("冒泡排序前:");
printArray(arr);
bubbleSort(arr);
System.out.println("冒泡排序后:");
printArray(arr);
}
public static void printArray (int []arr){
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");//数字后面加一个空格
}
System.out.print("\n");
}
public static void bubbleSort (int []arr){
for(int i=0;i<arr.length-1;i++){
for(int j=0;j<arr.length-i-1;j++){
if(arr[i]>arr[i+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
System.out.printf("第"+(i+1)+"轮排序后:");
printArray(arr);
}
}
}
Example33
public class Example33 {
public static void main (String[] ags)
{
int [][]arr=new int [3][];
arr[0]=new int []{11,12};
arr[1]=new int []{21,22,23};
arr[2]=new int []{31,32,33,34};
int sum=0;
for(int i=0;i<arr.length;i++){
int groupSum=0;
for(int j=0;j<arr[i].length;j++){
groupSum=groupSum+arr[i][j];
}
sum+=groupSum;
System.out.println("第"+(i+1)+"小组销售额为:"+groupSum+"万元");
}
System.out.println("总销售额为:"+sum+"万元");
}
}