import java.io.InputStreamReader
public class QuestionOne {
/**
* 编程将从键盘输入文本中的子字符串“word”替换为字符串“world”, 并删除所有的子字符串“this”。
* 序程要求:程序中要包含有注释,对于使用的变量和方法的功能要加以说明。
*/
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in))
String message = null// 存储用户输入的字符串
try {
while ((message = reader.readLine()) != null) {
// 打印处理前的字符串
System.out.println("输入的字符串为:" + message)
// 把 word 替换为 world
message = message.replaceAll("word", "world")
// 把 this 替换为 空
message = message.replaceAll("this", "")
// 打印处理后的字符串
System.out.println("处理后为:" + message)
}
} catch (Exception e) {
e.printStackTrace()
System.out.println("出现异常,程序退出!")
}
}
}
fieldname是java中合法标识符。根据《JAVA程序设计》期末考试复习题,fieldname是java中合法标识符。
标识,同“标志”汉语词汇,意思是标示识别。用来识别的记号。
import java.math.BigDecimalpublic class JavaCourse {
private String sName//学生姓名
private String sId //学号
private int testGrade //测试成绩
private int regularGrade//平时成绩
private int endOfTermGrade//期末成绩
private int totalGrade //总成绩
public final double TR= 0.3
public final double RR= 0.3
public final double ER= 0.4
public JavaCourse() {
}
public JavaCourse(String sName, String sId, int testGrade, int regularGrade, int endOfTermGrade) {
this.sName = sName
this.sId = sId
this.testGrade = testGrade
this.regularGrade = regularGrade
this.endOfTermGrade = endOfTermGrade
}
public String getsName() {
return sName
}
public void setsName(String sName) {
this.sName = sName
}
public String getsId() {
return sId
}
public void setsId(String sId) {
this.sId = sId
}
public int getTestGrade() {
return testGrade
}
public void setTestGrade(int testGrade) {
this.testGrade = testGrade
}
public int getRegularGrade() {
return regularGrade
}
public void setRegularGrade(int regularGrade) {
this.regularGrade = regularGrade
}
public int getEndOfTermGrade() {
return endOfTermGrade
}
public void setEndOfTermGrade(int endOfTermGrade) {
this.endOfTermGrade = endOfTermGrade
}
public int getTotalGrade() {
return totalGrade
}
@Override
public String toString() {
return "JavaCourse{" +
"sName='" + sName + '\'' +
", sId='" + sId + '\'' +
", testGrade=" + testGrade +
", regularGrade=" + regularGrade +
", endOfTermGrade=" + endOfTermGrade +
'}'
}
public void showTotalGrad() {
//double四舍五入(0.5->1)转int类型
totalGrade = Integer.parseInt(new BigDecimal((testGrade * TR + regularGrade * RR + endOfTermGrade * ER)).setScale(0, BigDecimal.ROUND_HALF_UP).toString())
System.out.println("课程的总成绩:" + totalGrade)
}
}
public class DemoTest02 {
public static void main(String[] args) {
JavaCourse student1 = new JavaCourse()
student1.setsName("小张")
student1.setsId("10086")
student1.setTestGrade(85)
student1.setRegularGrade(90)
student1.setEndOfTermGrade(86)
System.out.println(student1.toString())
student1.showTotalGrad()
JavaCourse student2 = new JavaCourse("老王","10010",60,80,45)
System.out.println(student2.toString())
student2.showTotalGrad()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)