重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
class MyClass1
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名申请、网络空间、营销软件、网站建设、弓长岭网站维护、网站推广。
{
private int data;
public int getDate()
{
return data;
}
public void setData(int data)
{
this.data=data;
}
}
import java.util.Scanner;
/**
* @Author: Cool_Wu
* @Date: 2020-12-07 19:03
*/
public class Transport {
private static final int BOX_A = 1;
private static final int BOX_B = 3;
private static int count_A;
private static int count_B;
public static void main(String[] args) {
System.out.println("请输入宝物数量:");
int count = new Scanner(System.in).nextInt();
if (count 1 || count 50){
System.out.println("宝物数量输入有误!");
return;
}
for (count_B = 0; count_B = count / BOX_B; count_B++){
count_A = count - count_B * BOX_B;
System.out.println("盒子A装运宝物数量:" + BOX_A + "x" + count_A + "=" + BOX_A * count_A);
System.out.println("盒子B装运宝物数量:" + BOX_B + "x" + count_B + "=" + BOX_B * count_B);
System.out.println("宝物装运总数:" + BOX_A * count_A + "+" + BOX_B * count_B + "=" + (BOX_A * count_A + BOX_B * count_B));
System.out.println("-------------------------");
}
System.out.println("装运方案数为:" + count_B);
}
}
运行结果
class NoLowerLetterException extends Exception {
public NoLowerLetterException(String msg) {
super(msg);
}
}
class NoDigitException extends Exception {
public NoDigitException(String msg) {
super(msg);
}
}
class People {
void printLetter(char c) {
if (c = 'a' c = 'z') {
System.out.println(c);
} else {
try {
throw new NoLowerLetterException(String.valueOf(c));
} catch (NoLowerLetterException e) {
e.printStackTrace();
}
}
}
void printDigit(char c) {
if (c = '0' c = '9') {
System.out.println(c);
} else {
try {
throw new NoDigitException(String.valueOf(c));
} catch (NoDigitException e) {
e.printStackTrace();
}
}
}
}
public class ExceptionExample {
public static void main(String args[]) {
People people = new People();
for (int i = 0; i 128; i++) {
// 【代码5】
// //将i转换为char类型,执行people.printLetter()方法,如果出现异常则捕获,并输出异常的错误信息!
people.printLetter((char) i);
}
for (int i = 0; i 128; i++) {
// 【代码6】 //将i转换为char类型,执行people. printDigit
// ()方法,如果出现异常则捕获,并输出异常的错误信息!
people.printDigit((char) i);
}
}
}
//第一行,r的值为 r=1 当c=1输出结果:a=3
//解释: r=1那么进入循环打印第一句:r的值为 r=1,接着循环内层循环 c=1 判断 cr结果发现11不成立,那么不进if循环 直接走a+=2 a变为3
//那么打印:当c=1输出结果:a=3
//接着内层循环继续 c=2 判断 cr结果发现21成立 进入if continue loop回到了外层循环 r自增1 变为 2
//=========================================================================================
//第二行,r的值为 r=2 当c=1输出结果:a=5 当c=2输出结果:a=7
//解释: 运行完上面后 r自增1 变为2 那么打印: r的值为 r=2 ,接着进入内层循环 c=1 判断cr结果发现12不成立,那么不进if循环 直接走a+=2 a变为5
//那么打印 当c=1输出结果:a=5
//接着内层循环继续 c=2 判断 cr结果发现22 不成立 那么不进if循环 直接走 a+=2 a变为7
//接着内层循环继续c=3 判断 cr结果发现 32 成立 进入if continue loop回到了外层循环 r自增1变为3
//...
//LZ要注意,a一直是在循环外,这两个循环一直在改变a的值,也就是a的值一直是在增的。
不知道是否理解对了你的意思,大概写了一下:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;
public class FileReadAndWrite {
private static final int DEFAULT_BUFFER_SIZE = 1024;
public static void main(String[] args) {
File file = new File("E:/workspace/FileIOTest/src/a.txt");
String str = file2String(file, "UTF-8");
str = str.replace('d', 'f');
string2File(str,"E:/workspace/FileIOTest/src/b.txt");
System.out.println("处理完毕");
}
/**
* 文本文件转换为指定编码的字符串
*
* @param file
* 文本文件
* @param encoding
* 编码类型
* @return 转换后的字符串
* @throws IOException
*/
public static String file2String(File file, String encoding) {
InputStreamReader reader = null;
StringWriter writer = new StringWriter();
try {
if (encoding == null || "".equals(encoding.trim())) {
reader = new InputStreamReader(new FileInputStream(file),
encoding);
} else {
reader = new InputStreamReader(new FileInputStream(file));
}
// 将输入流写入输出流
char[] buffer = new char[DEFAULT_BUFFER_SIZE];
int n = 0;
while (-1 != (n = reader.read(buffer))) {
writer.write(buffer, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (reader != null)
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 返回转换结果
if (writer != null)
return writer.toString();
else
return null;
}
/**
* 将字符串写入指定文件(当指定的父路径中文件夹不存在时,会最大限度去创建,以保证保存成功!)
*
* @param res
* 原字符串
* @param filePath
* 文件路径
* @return 成功标记
*/
public static boolean string2File(String res, String filePath) {
boolean flag = true;
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
File distFile = new File(filePath);
if (!distFile.getParentFile().exists())
distFile.getParentFile().mkdirs();
bufferedReader = new BufferedReader(new StringReader(res));
bufferedWriter = new BufferedWriter(new FileWriter(distFile));
char buf[] = new char[1024]; // 字符缓冲区
int len;
while ((len = bufferedReader.read(buf)) != -1) {
bufferedWriter.write(buf, 0, len);
}
bufferedWriter.flush();
bufferedReader.close();
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
flag = false;
return flag;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}
}
格式化时间,将你传入的S_POSTDATE时间参数格式化为yyyy-MM-dd格式的,比如格式化成
2014-08-24。