通过这几个列子来展示和考察自己Java编程的基本功,不要以为简单,如果基础不够扎实,很难在短时间内解决以下问题,所以如果基础一般,还是动手认真实践一下,然后再参考我的解法(不一定是最好的解法,但也是我第一次接触编程题目,自己慢慢实践出来的,希望读者童鞋们也自己动手实践以下)
示例一:蒙特卡罗模拟。假设这个圆的半径是1,那么圆面积就是圆周率,而外接正方形面积为4。现随便产生一个正方形中的一个点,这个点落在圆内的概率为圆周率除以4。编写程序,在正方形内随机产生一百万个点,用numberOfHits表示落在圆内的点,因此圆周率可以用4*numberOfHits/1000000来近似估算,选取的点越多,精度越高。这就是蒙特卡罗模拟,是一种重要的思想方法。下面是实现代码和运行结果展示:
package prcatice4;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Practice4 {
public static void main(String[] args) {
//蒙特卡罗模拟
final int NUMBER_OF_TRIALS = 1000000;
int numberOfHits = 0;
for(int i = 0;i < NUMBER_OF_TRIALS;i++){
double x = Math.random()*2.0-1;
double y = Math.random()*2.0-1;
if(x*x+y*y<=1)
numberOfHits++;
}
double pi = 4.0*numberOfHits/NUMBER_OF_TRIALS;
System.out.println("PI is "+pi);
}
}
运行结果:
示例二:提示用户输入一个1到15之间的整数,然后显示一个金字塔形状的图案,先给出运行效果图,然后根据效果图的要求设计程序。
效果图:此效果是输入14得到的图形。
要解决此问题,可以将其分解成若干个小问题,比如先画出左半边,再画出右半边,然后将两个合并整合。还可以“画出”各种金字塔的程序。下面就将我思考的方式和代码展示如下:
package Class;
import java.util.Scanner;
public class chengxu {
public static void main(String[] args) {
//画各种金字塔的程序
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer between 1 and 15 :");
int n = input.nextInt();
//左金字塔
int count = 1;
for(int i = 1;i <=n;i++){
for(int j = n;j>=1;j--){
if(count <= n-i){
System.out.print(" ");
count++;
}
else{
System.out.printf("%2d",j);
System.out.print(" ");
}
}
count = 1;
System.out.println();
}
//右金字塔
for(int i = 1;i <= n;i++){
for(int j = 1;j <= i;j++){
System.out.printf("%2d",j);
System.out.print(" ");
}
System.out.println();
}
//整个金字塔
int countFront = 1;
for(int i = 1;i <=n;i++){
int out = i;
for(int j = 1;j <= 2*n-1;j++){
if(countFront <= n-i || countFront >=n+i ){
System.out.print(" ");
}
else{
if(j <=n){
System.out.printf("%2d",out);
System.out.print(" ");
out--;
}
if(j > n){
System.out.printf("%2d",out+2);
System.out.print(" ");
out++;
}
}
countFront++;
}
countFront = 1;
System.out.println();
}
//右金字塔翻版倒影
for(int i = n;i >= 1;i--){
for(int j = 1;j <= i;j++){
System.out.printf("%2d",j);
System.out.print(" ");
}
System.out.println();
}
//斜对角数字一样的左金字塔
for(int i = n;i >= 1;i--){
int cnt = 1;
for(int j = 1;j <= n;j++){
if(cnt <= n-i){
System.out.print(" ");
cnt++;
}
else{
System.out.printf("%2d",j-n+i);
System.out.print(" ");
}
}
System.out.println();
}
}
}
学习完上述程序之后,有一个变种的画金字塔的题目,现将运行效果图展示出来,读者可以自己编程设计出来,以作为此示例学习的考察。
效果图:先试试看能否通过上述学习解决此题,先自己研究设计,再看看我的思路和代码。实现代码如下:
package Class;
import java.util.Scanner;
public class chengxu {
public static void main(String[] args) {
//指数形式的金字塔
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer between 8:");
int n = input.nextInt();
int countFront = 1;
for(int i = 1;i <=n;i++){
int out = 0;
for(int j = 1;j <= 2*n-1;j++){ //打印每一行
if(countFront <= n-i || countFront >=n+i ){
System.out.print(" ");
}
else{
if(j <=n){
int result = (int)(Math.pow(2, out));
System.out.printf("%3d",result);
System.out.print(" ");
out++;
}
if(j > n){
int result = (int)(Math.pow(2, out-2));
System.out.printf("%3d",result);
System.out.print(" ");
out--;
}
}
countFront++;
}
countFront = 1;
System.out.println();
}
}
}
示例三:分解质因数。就是提示用户输入一个整型数,然后输出这个数由其质因数的乘积得到的等式。不要以为这个题目简单,先自己动手试试看。
运行效果如图所示:注意:先动手再看代码!
package Class;
import java.util.Scanner;
public class chengxu {
public static void main(String[] args) {
//分解质因数,不要以为简单,自己写写看
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int data = input.nextInt();
int number = 2;
System.out.print(data+"=");
while(number <= data){
if(number == data){
System.out.println(data);
break;
}
else
if(data % number == 0){
System.out.print(number+"*");
data /=number;
}
else
number++;
}
}
}
示例四:提示输入年份,如2014,则输出2014年每个月第一天是星期几。先给出运行效果图如下:
你能不看答案自己动手设计出来吗?
程序语言设计没有捷径,只有从敲一个代码一个代码中熟练起来。
贴出示例四实现代码如下:
package shiyanPractice;
import java.util.Scanner;
public class ShiYanPractice {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of year: ");
int year = input.nextInt();
int month = 1;
int day = 0;
for(int i = 0;i < 12;i++){
int h,q,m,j,k;
month = i+1;
day = 1;
q = day;
m = month;
//泽勒一致性,如果月份是1或2则要算为上一年的13或14月,并将年数减一
if(m == 1 || m == 2){
m+=12;
year -= 1;
}
j = year/100; //求出h表示的星期几,其中0表示周日,1表示周六,2表示周一,...
k = year%100;
h = (q+(int)(26*(m+1)/10.0)+k+(int)(k/4.0)+(int)(j/4.0)+5*j)%7;
//计算完h之后要将year的值加回来
if(month == 1 ||month == 2)
year += 1;
String dayTranslate = "";
switch(h){
case 0:
dayTranslate = "Saturday";
break;
case 1:
dayTranslate = "Sunday";
break;
case 2:
dayTranslate = "Monday";
break;
case 3:
dayTranslate = "Thusday";
break;
case 4:
dayTranslate = "Wednesday";
break;
case 5:
dayTranslate = "Thursday";
break;
case 6:
dayTranslate = "Friday";
break;
}
String monthFor = "";
switch(month){
case 1:
monthFor = "January";
break;
case 2:
monthFor = "February";
break;
case 3:
monthFor = "March";
break;
case 4:
monthFor = "April";
break;
case 5:
monthFor = "May ";
break;
case 6:
monthFor = "June ";
break;
case 7:
monthFor = "July ";
break;
case 8:
monthFor = "August";
break;
case 9:
monthFor = "September";
break;
case 10:
monthFor = "October";
break;
case 11:
monthFor = "November";
break;
case 12:
monthFor = "December";
break;
}
String output = monthFor+" 1,\t"+(int)year+" is\t"+dayTranslate;
System.out.println(output);
}
}
}