Java编程入门程序实践
2016-12-25 00:04
555 查看
java初学者,练习几个入门程序!
执行结果:
执行结果:
打印结果为什么出现换行呢?
执行结果:
又有换行问题?
执行结果:
执行结果:
执行结果:
For-Each访问多维数组
执行结果:
乘法表
public class Print99 { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("99 form"); System.out.print(" "); for(int i=1;i<9;i++) System.out.print(i+" "); System.out.println(); for(int i=1;i<=9;i++){ System.out.print(i+" "); for(int j=1;j<=9;j++) if(j<=i) System.out.print(i*j+" "); System.out.println(); } } }
执行结果:
99 form 1 2 3 4 5 6 7 8 1 1 2 2 4 3 3 6 9 4 4 8 12 16 5 5 10 15 20 25 6 6 12 18 24 30 36 7 7 14 21 28 35 42 49 8 8 16 24 32 40 48 56 64 9 9 18 27 36 45 54 63 72 81
螺旋矩阵
import java.io.*; public class RingDemo { public static void main(String[] args) { // TODO Auto-generated method stub String strln=""; System.out.print("now input your row number: "); InputStreamReader input = new InputStreamReader(System.in); BufferedReader buff = new BufferedReader(input); try { strln = buff.readLine(); }catch(IOException e) { System.out.println(e.toString()); } int int1 = Integer.parseInt(strln); int n = int1; System.out.println("this row "+n+" is spiral array:"); int intA = 1; int[][] array = new int ; int intB; if(n%2 != 0) { intB = n/2 + 1; }else intB = n/2; for(int i=0;i<intB;i++) { for(int j=i;j<n-i;j++) { array[i][j] = intA; intA++; } for(int k=i+1;k<n-i;k++) { array[k][n-i-1] = intA; intA++; } for(int l=n-i-2;l>=i;l--) { array[n-i-1][l] = intA; intA++; } for(int m=n-i-2;m>i;m--) { array[m][i] = intA; intA++; } } for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { System.out.println(array[i][j]+" "); } System.out.println(); } } }
执行结果:
now input your row number: 7 this row 7 is spiral array: 1 2 3 4 5 6 7 24 25 26 27 28 29 8 23 40 41 42 43 30 9 22 39 48 49 44 31 10 21 38 47 46 45 32 11 20 37 36 35 34 33 12 19 18 17 16 15 14 13
打印结果为什么出现换行呢?
数组排序
public class SelectionSort { public static void main(String[] args) { // TODO Auto-generated method stub int[] intArray = {12,11,45,6,8,40,33,3,60}; int keyValue; int index; int temp; System.out.println("排序前的数组:"); for(int i=0;i<intArray.length;i++) System.out.print(intArray[i]+" "); for(int i=0;i<intArray.length;i++) { index = i; keyValue = intArray[i]; for(int j=i;j<intArray.length;j++) if(intArray[j]<keyValue) { index = j; keyValue = intArray[j]; } temp = intArray[i]; intArray[i] = intArray[index]; intArray[index] = temp; } System.out.println("\n排序后的数组:"); for(int i=0;i<intArray.length;i++) System.out.print(intArray[i]+" "); } }
执行结果:
排序前的数组: 12 11 45 6 8 40 33 3 60 排序后的数组: 3 6 8 11 12 33 40 45 60
又有换行问题?
冒泡排序
public class BubbleSort { public static void main(String[] args) { // TODO Auto-generated method stub int[] intArray = {12,33,44,5,67,13,56,20}; System.out.println("排序前的数组:"); for(int i=0;i<intArray.length;i++) System.out.print(intArray[i]+" "); System.out.println(); int temp; for(int i=0;i<intArray.length;i++) { for(int j=i;j<intArray.length;j++) { if(intArray[j]<intArray[i]) { temp = intArray[i]; intArray[i]=intArray[j]; intArray[j]=temp; } } } System.out.println("排序后的数组"); for(int i=0;i<intArray.length;i++) System.out.print(intArray[i]+" "); } }
执行结果:
排序前的数组: 12 33 44 5 67 13 56 20 排序后的数组 5 12 13 20 33 44 56 67
快速排序
public class QuickSort { public static void main(String[] args) { // TODO Auto-generated method stub int[] intArray = {23,14,67,3,42,16,67,88,37,56}; int keyValue; int index; int temp; System.out.println("排序前的数组:"); for(int i=0;i<intArray.length;i++) System.out.print(intArray[i]+" "); System.out.println(); for(int i=0;i<intArray.length;i++) { index = i; keyValue = intArray[i]; for(int j=i;j<intArray.length;j++) if(intArray[j]<keyValue) { index = j; keyValue = intArray[j]; } temp = intArray[i]; intArray[i] = intArray[index]; intArray[index] = temp; } System.out.println("排序后的数组:"); for(int i=0;i<intArray.length;i++) System.out.print(intArray[i]+" "); } }
执行结果:
排序前的数组: 23 14 67 3 42 16 67 88 37 56 排序后的数组: 3 14 16 23 37 42 56 67 67 88
For-Each循环
public class ForEach { public static void main(String[] args) { // TODO Auto-generated method stub int sum = 0; int[] nums = {1,2,3,4,5,6,7,8,9,0}; for(int i:nums) { System.out.println("数组元素:"+i); sum+=i; } System.out.println("数组元素和:"+sum); } }
执行结果:
数组元素:1 数组元素:2 数组元素:3 数组元素:4 数组元素:5 数组元素:6 数组元素:7 数组元素:8 数组元素:9 数组元素:0 数组元素和:45
For-Each访问多维数组
public class ForEach2 { public static void main(String[] args) { // TODO Auto-generated method stub int sum =0; int nums[][] = new int[4][4]; int k=0; for(int i=0;i<4;i++) for(int j=0;j<4;j++) nums[i][j]=k++; for(int x[]:nums) for(int y:x) { System.out.println("数组元素:"+y); sum+=y; } System.out.println("数组元素:"+sum); } }
执行结果:
数组元素:0 数组元素:1 数组元素:2 数组元素:3 数组元素:4 数组元素:5 数组元素:6 数组元素:7 数组元素:8 数组元素:9 数组元素:10 数组元素:11 数组元素:12 数组元素:13 数组元素:14 数组元素:15 数组元素:120
相关文章推荐
- JAVA 编程 入门程序简析
- 程序设计入门—Java语言 . 翁恺-第四周编程作业-2.念整数
- Java 并发编程实践基础 读书笔记: 第三章 使用 JDK 并发包构建程序
- 一些入门的java小程序-zt
- Java2实用教程(第二版)程序代码——第一章 Java语言入门
- [转载]Java程序实现Logo画面的编程
- WIN32 编程示例:嵌入式式软件开发程序入门(完全从网上copy的文章)
- Java、XML与数据库编程实践
- ASP编程入门进阶(十一):Chat聊天程序
- 初学者入门 JAVA 的 XML 编程实例解析
- Java加密和数字签名编程快速入门
- Java数据访问对象(DAO)编程模入门
- Java手机程序设计入门 电子书开放下载
- [ASP.NET入门随想三]抽象的力量——WEB程序编程思想的演进
- Java加密和数字签名编程快速入门
- Java网络编程入门学习
- Java、XML与数据库编程实践(一)
- Java、XML与数据库编程实践(二)
- Java、XML与数据库编程实践
- Java加密和数字签名编程快速入门