1.求天干地支
实现功能(及过程)
输入需要计算天干地支的年份并为int型;
根据规律计算出属于第几个天干和地支;
定义天干地支的数组;
输出天干地支;(如果nian%10==0
则输出癸亥年)
输出结果
![结果]()
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package jiazi; import java.util.Scanner; public class Zi { public static void main(String[ ] args) { Scanner scan=new Scanner(System.in); System.out.print("请输入需要计算的年份:"); int m=scan.nextInt(); int nian=(m+57)%60; if (nian%10==0) { System.out.print("癸亥年"); System.exit(0); } int jia=nian%10-1; int zi=nian%12-1; char[] jiashu= {'甲', '乙', '丙', '丁', '戊', '已', '庚', '辛', '壬', '癸'}; char[] zishu= {'子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥'}; System.out.print( ""+jiashu[jia]+ zishu[zi]+ "年"); } }
|
2.倒置
实现功能(及过程)
输入待转化的行数和列数,输入n和m中;
以n和m定义两个互为倒置的二维数组;
第一个双重for循环是将二维数组a中的每一个数赋予0~9的随机数,并且每一个i和j都有对应的b[j][i]=a[i][j];
来进行倒置(二维数组b的j行i列的数赋予二维数组a的i行j列的数值);
第二个双重for循环输出二维数组b;
输出结果
![结果]()
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package Dao; import java.util.Scanner; public class zhi { public static void main(String[] args) { Scanner s=new Scanner(System.in); System.out.print("请输入待转化的行数:"); int m=s.nextInt(); System.out.print("请输入待转化的列数:"); int n=s.nextInt(); int[][] a=new int [m][n]; int[][] b=new int [n][m]; for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ a[i][j]=(int)(Math.random()*10); System.out.print(" "+a[i][j]); b[j][i]=a[i][j]; } System.out.println(""); } System.out.println("倒置后"); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ System.out.print(" "+b[i][j]); } System.out.println(""); } } }
|
3.调用方法在尾巴加上字符
实现功能(及过程)
输入一行文字输入为String
调用私有方法为这行文字加上小尾巴
输出加上小尾巴的文字
输出结果
![结果]()
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package Hello; import java.util.Scanner; public class hellowolrd { public static void main(String[] args) { System.out.println("请输入一行文字:"); Scanner sc = new Scanner(System.in); String str1 = sc.nextLine(); str1 = addstr(str1); System.out.print("加字符串处理后:"); System.out.print(str1); }
private static String addstr(String str2) { String str3=str2+"后面加上字符串"; return str3; } }
|
4.显示出数值类型的最大值
实现功能(及过程)
使用每个类型提供的MAX_VALUE方法来显示数值类型的最大值
结果
![image-20230628211358491]()
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package Hello;
public class max { public static void main(String[ ] args) {
byte largestByte = Byte.MAX_VALUE; short largestshort = Short.MAX_VALUE; int largestInteger = Integer.MAX_VALUE; long largestLong = Long.MAX_VALUE; float largestFloat = Float.MAX_VALUE; double largestDouble = Double.MAX_VALUE; char aChar = 's'; boolean aBoolean = true; System.out.print("Byte最大值为:"); System.out.println( largestByte) ; System.out.print("short最大值为:"); System.out.println( largestshort); System.out.print("Integer最大值为:"); System.out. println( largestInteger); System.out.print("Long最大值为:"); System.out. println( largestLong ); System.out.print("Float最大值为:"); System.out.println( largestFloat); System.out.print("Double最大值为:"); System.out.println( largestDouble); } }
|
5.求出最大公约数和最小公倍数
实现功能(及过程)
输入俩整数,并比较其大小
用for循环寻找最大公约数
max=m*n/min
计算最小公倍数
结果
![结果]()
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package Hello;
import java.util.Scanner;
public class suanshu { public static void main(String[] args) { Scanner scan=new Scanner(System.in); System.out.println("请输入两个整数:"); int m=scan.nextInt(); int n=scan.nextInt(); int min=m<n?m:n; for(int i=min;i>0;i--){ if(m%i==0&n%i==0){ min=i; break; } } int max=m*n/min; System.out.println("最大公约数:"+min); System.out.print("最小公倍数:"+max); } }
|
6.使用弹窗输入数值计算贷款每月分期
实现功能(及过程)
使用JOptionPane.showInputDialog()
方法输入字符串,再用Double.parseDouble()
方法将字符串转double型;
算出月利率并结合年数和贷款金额计算出每月付款
结果
![结果]()
![结果2]()
![结果3]()
![image-20230629222739226]()
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| package java_duihuakuang_shuru; import javax.swing.JOptionPane; public class ComputeLoanUsingDialog { public static void main(String[] args) { String annualInterestRateString = JOptionPane.showInputDialog( "输入年利率,例如8.25: "); double annualInterestRate = Double.parseDouble(annualInterestRateString); double monthlyInterestRate = annualInterestRate/1200; String numberOfYearsString = JOptionPane.showInputDialog( "输入年数作为整数,\n例如5:"); int numberOfYears = Integer.parseInt(numberOfYearsString); String loanString = JOptionPane.showInputDialog( "输入贷款金额,例如1200000.95:"); double loanAmount = Double.parseDouble(loanString); double monthlyPayment = loanAmount * monthlyInterestRate / ( 1 - 1 / Math.pow ( 1 + monthlyInterestRate , numberOfYears * 12 ) ); double totalPayment = monthlyPayment*numberOfYears*12; monthlyPayment=(int)(monthlyPayment*100)/100.0; totalPayment = (int)(totalPayment * 100) / 100.0; String output="每月付款为 " + monthlyPayment + "\n付款总额为 "+totalPayment; JOptionPane.showMessageDialog(null,output); }
}
|
7.【Java实践】将小游戏改为可以联网更新必要的图片资源
Init类
在小游戏中插入一个类作为转接口,InitTip.Initip();
是一个初始化方法,后续会提到。
创建一个数组用来存放资源图片的名称,并且用for循环判断每个文件是否存在,如果不存在则删除upd(用来判断服务器上是否存在更新,如果upd存在代表需要更新,并且更新的图片不存在则删除upd为下一次检测做好准备),判断为更新失败。并使用UpdateFailed.UpdateFailed();
进行更新失败提示,并修改标记jc=0
,如果jc==1那么可以判断没有需要更新的操作,则执行DownloadFinish.Finish();
提示更新完成,最后启动游戏框架。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| package com.pig.main;
import com.pig.util.DownloadFinish; import com.pig.util.InitTip; import com.pig.util.UpdateFailed;
import java.awt.*; import java.io.File; import javax.swing.*;
public class Init { public static void Init() { int jc=1; InitTip.Initip(); String [] img_name = new String [10]; img_name = new String[]{"barrier.png", "Barrier_down.png", "barrier_up.png", "cloud0.png", "cloud1.png", "pig_bk.png", "pig_down.png", "pig_normal.png", "pig_up.png", };
File upd = new File("C:\\ProgramData\\Dreamy Pig\\update.png"); for (int i = 0; i < 9; i++) { File img = new File("C:\\ProgramData\\Dreamy Pig\\img\\" + img_name[i]); if (!img.exists() ) { boolean deleted = upd.delete(); UpdateFailed.UpdateFailed(); jc=0; break; } } if (jc==1){DownloadFinish.Finish();} new GameFrame(); } }
|
InitTip类
1.显示窗口框架,主题框架是居中显示,标题为“初始化”,内容为 g.drawString("xxxxxxx", MESSAGE_X, MESSAGE_Y);
方法
其次判断文件夹是否存在,如果不存在则创建
其次第一次初始化判断资源图片是否存在以及upd.exists(),如果upd.exists()存在则需要更新,并且删除第一次初始化判断存在的资源图片。
之后使用以下代码进行更新:
1 2 3 4
| URL url = new URL("https://img1.i-nmb.cn/pig/"+img_name[j]); image0 = ImageIO.read(url);
ImageIO.write(image0, "png", new File("C:\\ProgramData\\Dreamy Pig\\img\\"+img_name[j]));
|
从我的服务器(域名https://i-nmb.cn)中的`"https://img1.i-nmb.cn/pig/"+img_name[j]`更新数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
| package com.pig.util;
import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.net.URL;
import static com.pig.util.BatWriter.creatBat;
public class InitTip { public static void Initip() {
JFrame frame = new InitipFrame(); EventQueue.invokeLater(new Runnable() { public void run() {
frame.setLocationRelativeTo(null); frame.setTitle("初始化"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } );
File MainFolder = new File("C:\\ProgramData\\Dreamy Pig"); if (!MainFolder.exists() && !MainFolder.isDirectory()) { MainFolder.mkdirs(); System.out.println("创建主文件夹"); } else { System.out.println("主文件夹已存在"); } File ImgFolder = new File("C:\\ProgramData\\Dreamy Pig\\Img"); if (!ImgFolder.exists() && !ImgFolder.isDirectory()) { ImgFolder.mkdirs(); System.out.println("创建图片文件夹"); } else { System.out.println("图片文件夹已存在"); }
BufferedImage png1 = null; try { URL url = new URL("https://i-nmb.cn/pig/update-on.png"); png1 = ImageIO.read(url); ImageIO.write(png1, "png", new File("C:\\ProgramData\\Dreamy Pig\\update.png")); } catch (IOException e) { e.printStackTrace(); }
String [] img_name = new String [10]; img_name = new String[]{"barrier.png", "Barrier_down.png", "barrier_up.png", "cloud0.png", "cloud1.png", "pig_bk.png", "pig_down.png", "pig_normal.png", "pig_up.png", };
File upd = new File("C:\\ProgramData\\Dreamy Pig\\update.png"); for (int i = 0; i < 9; i++) { File img = new File("C:\\ProgramData\\Dreamy Pig\\img\\"+img_name[i]); if(!img.exists()||upd.exists()){
System.out.println("C:\\ProgramData\\Dreamy Pig\\img\\"+img_name[i]+"不存在或存在更新"); if (upd.exists()) {
new showMessageFrame("正在更新"); for (int k = 0; k < 9; k++) {
File old = new File("C:\\ProgramData\\Dreamy Pig\\img\\" + img_name[k]); System.out.println("拟删除" + old);
Boolean dpBoolean = old.delete(); System.out.println("删除"+dpBoolean); if (!dpBoolean&&old.exists()) { System.out.println( old + " " + "存在" + old.exists()); creatBat(); new showMessageFrame("正在请求管理员权限"); OpenBat.BatOpen(); System.exit(1);
} }
File bat = new File("C:\\ProgramData\\Dreamy Pig\\admin.bat"); bat.delete(); boolean deleted = upd.delete(); System.out.println(deleted); }
for (int j = 0; j < 9; j++){ new showMessageFrame(img_name[j]+"正在下载"); BufferedImage image0 = null; try { URL url = new URL("https://img1.i-nmb.cn/pig/"+img_name[j]); image0 = ImageIO.read(url);
ImageIO.write(image0, "png", new File("C:\\ProgramData\\Dreamy Pig\\img\\"+img_name[j])); } catch (IOException e) { e.printStackTrace(); }
} if(!img.exists()){ new showMessageFrame(img_name[i]+"加载失败"); }
}else if(img.exists()){ new showMessageFrame("C:\\ProgramData\\Dreamy Pig\\img\\"+img_name[i]+"加载成功");
} }
frame.dispose(); frame.setVisible(false); } }
class InitipFrame extends JFrame{
public InitipFrame() { add(new com.pig.util.InitipComponent()); pack(); }
}
class InitipComponent extends JComponent{ public static final int MESSAGE_X = 100; public static final int MESSAGE_Y = 100;
private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 200;
public void paintComponent(Graphics g) { g.drawString("正在检测本地资源", MESSAGE_X, MESSAGE_Y); }
public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT); }
}
|
UpdateFailed类
是对下载失败的弹窗提示制作的类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| package com.pig.util;
import javax.swing.*; import java.awt.*;
public class UpdateFailed {public static void UpdateFailed() { JFrame frame = new com.pig.util.UpdateFailedFrame(); EventQueue.invokeLater(new Runnable() { public void run() {
frame.setLocationRelativeTo(null); frame.setTitle("初始化失败"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } );
} }
class UpdateFailedFrame extends JFrame{
public UpdateFailedFrame() { add(new com.pig.util.UpdateFailedComponent()); pack(); }
}
class UpdateFailedComponent extends JComponent{ public static final int MESSAGE_X = 75; public static final int MESSAGE_Y = 100;
private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 200;
public void paintComponent(Graphics g) { g.drawString("资源下载失败,请检查网络", MESSAGE_X, MESSAGE_Y); }
public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT); }
}
|
DownloadFinish类代表下载完成
弹窗下载完成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| package com.pig.util;
import java.awt.*; import javax.swing.*;
public class DownloadFinish { public static void Finish() { final JFrame frame = new FinishFrame(); EventQueue.invokeLater(new Runnable() {
public void run() { frame.setLocationRelativeTo((Component) null); frame.setTitle("完成"); frame.setDefaultCloseOperation(3); frame.setVisible(true); } }); try { Thread.sleep(3000); } catch (InterruptedException e) { System.out.println("出现异常"); } frame.dispose(); frame.setVisible(false); } }
class FinishFrame extends JFrame{
public FinishFrame() { add(new FinishComponent()); pack(); }
}
class FinishComponent extends JComponent{ public static final int MESSAGE_X = 75; public static final int MESSAGE_Y = 100;
private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 200;
public void paintComponent(Graphics g) { g.drawString("资源加载完毕,3秒后开始游戏", MESSAGE_X, MESSAGE_Y); }
public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT); }
}
|
代码打包下载
![代码文件]()
下载地址:https://i-nmb.cn/pig/cone.zip