java程序設計
A. JAVA程序設計
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
BufferedOutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream(new File("d:/info.txt")));
String line = "第一行文本\n第二行文本";
out.write(line.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream("d:/info.txt"));
StringBuffer buffer = new StringBuffer();
byte[] buff = new byte[in.available()];
while (in.read(buff) != -1) {
buffer.append(new String(buff));
}
System.out.println(buffer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
in = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
B. Java簡單程序設計
你好提問者,
如果解決了你的問題請採納,若有疑問請追問,謝謝!
packagecom.zyx.cn._show;
/**
0+
1++
2+++
3====
4=====
5======
*@authoryltd
*/
publicclassSanjiao{
publicstaticvoidmain(String[]args){
show(5);
}
publicstaticvoidshow(intnumber){
for(inti=0;i<=number;i++){
System.out.print(i);
if(i>=0&&i<3){
for(intj=i;j>=0;j--){
System.out.print("+");
}
}else{
for(intj=i;j>=0;j--){
System.out.print("=");
}
}
System.out.println();
}
}
}
0+
1++
2+++
3====
4=====
5======
C. JAVA程序設計
public class Student {
private String id;
private String name;
private double store;
public Student(String id, String name, double store) {
this.SetRecord(id, name, store);
}
public void SetRecord(String id, String name, double store) {
this.id = id;
this.name = name;
this.store = store;
}
public String GetRecord() {
return "id=" + id + ",name=" + name + ",store=" + store;
}
public static void main(String[] args) {
Student stuA = new Student("na001", "jack", 98.5);
System.out.println("該學生的信息為:\n" + stuA.GetRecord());
}
}
D. java程序設計
這是一個很大的課題,值得研究,
E. JAVA程序設計
public class Car {
String make;
String model;
int year;
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public Car(String make,String model,int year){
this.make=make;
this.model=model;
this.year=year;
}
public String toString() {
return "Car [make=" + make + ", model=" + model + ", year=" + year
+ "]";
}
public boolean isAntique(int year){
boolean flag=false;
int newYear=2019;
int da;
da=newYear-year;
if(da>45){
flag=true;
return flag;
}
return flag;
}
}
F. Java程序設計
成績我定義成double了,另外平均分我給你保留兩位小數:
public class Student {
private String No; //學號
private String name; //姓名
private String gender; //性別
private int age; //年齡
private double chinese; //語文
private double math; //數學
private double english; //英語
//構造函數
public Student(String no,
String name,
String gender,
int age,
double chinese,
double math,
double english) {
No = no;
this.name = name;
this.gender = gender;
this.age = age;
this.chinese = chinese;
this.math = math;
this.english = english;
}
@Override
public String toString() {
return "Student{" +
"No='" + No + ''' +
", name='" + name + ''' +
", gender='" + gender + ''' +
", age='" + age + ''' +
", chinese=" + chinese +
", math=" + math +
", english=" + english +
'}';
}
//計算總分
public double getSum() {
return this.chinese + this.math + this.english;
}
//計算平均分
public double getAverage() {
return (this.chinese + this.math + this.english) / 3;
}
}
測試類:
class Test {
public static void main(String[] args) {
Student student = new Student(
"20190001",
"張三",
"男",
18,
109.5,
89.0,
110.0);
double sum = student.getSum();
double average = student.getAverage();
System.out.println("該學生的信息為:" + student.toString());
System.out.println("該學生的總分為:" + sum);
System.out.println("該學生的平均分為:" + String.format("%.2f", average));
}
}
運行結果:
G. java的程序設計
直接賦值粘貼
public class Commodity {
private String type;
private String noun;
private double salePrice;
private String unit;
private String size;
private double percent;
public Commodity(String type, String noun, double salePrice, String unit, String size, double percent) {
super();
this.type = type;
this.noun = noun;
this.salePrice = salePrice;
this.unit = unit;
this.size = size;
this.percent = 1;
}
public double getSalePrice() {
return salePrice;
}
public void setSalePrice(double salePrice) {
this.salePrice = salePrice;
}
public double getPercent() {
return percent;
}
public void setPercent(double percent) {
this.percent = percent;
}
@Override
public String toString() {
return "Commodity [type=" + type + ", noun=" + noun + ", salePrice=" + salePrice + ", unit=" + unit + ", size="
+ size + ", percent=" + percent + "]";
}
}
請採納,謝謝
H. java程序設計
在開發工具裡面實現很簡單,怎麼獨立出來變成一個可運行文件我就不會了
I. Java程序設計
您好!java對於異常的處理,可以通過try...cath對異常進行捕捉,對不同的異常類型做對應的錯誤時的處理就可以了。