package com.zpp;public class Charge {
创新互联服务项目包括宝山网站建设、宝山网站制作、宝山网页制作以及宝山网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,宝山网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到宝山省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
public static void main(String [] args) {
if(args.length ==0) {
System.out.println("parameter error!");
System.out.println("java com.zpp.Charge [int]");
return;
}
int min = Integer.parseInt(args[0]);
double money = 0.0;
if (min = 0) {
money =0.0;
System.out.println("not money");
} else if (min = 60) {
money = 2.0;
} else {
money = 2.0 + (min - 60) * 0.01;
}
System.out.println("please pay: " + money);
}
} 编译:javac -d . Charge.java运行:java com.zpp.Charge 111
import java.awt.*;//计算器实例
import java.awt.event.*;
public class calculator
{
public static void main(String args[])
{
MyWindow my=new MyWindow("计算器");
}
}
class MyWindow extends Frame implements ActionListener
{ StringBuffer m=new StringBuffer();
int p;
TextField tex;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,jia,jian,cheng,chu,deng,dian,qingling,kaifang;
MyWindow(String s)
{
super(s);
//StringBuffer s2=new StringBuffer();
//String s;
tex=new TextField(18);
b0=new Button(" 0 ");
b1=new Button(" 1 ");
b2=new Button(" 2 ");
b3=new Button(" 3 ");
b4=new Button(" 4 ");
b5=new Button(" 5 ");
b6=new Button(" 6 ");
b7=new Button(" 7 ");
b8=new Button(" 8 ");
b9=new Button(" 9 ");
dian=new Button(" . ");
jia=new Button(" + ");
jian=new Button(" - ");
cheng=new Button(" × ");
chu=new Button(" / ");
deng=new Button(" = ");
qingling=new Button(" 清零 ");
kaifang=new Button(" √ ");
setLayout(new FlowLayout());
add(tex);
add(b0);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(dian);
add(jia);
add(jian);
add(cheng);
add(chu);
add(kaifang);
add(qingling);
add(deng);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
jia.addActionListener(this);
jian.addActionListener(this);
cheng.addActionListener(this);
chu.addActionListener(this);
dian.addActionListener(this);
deng.addActionListener(this);
qingling.addActionListener(this);
kaifang.addActionListener(this);
setBounds(200,200,160,280);
setResizable(false);//不可改变大小
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent ee)
{ System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b0)
{
m=m.append("0");
tex.setText(String.valueOf(m));
}
if(e.getSource()==b1)
{
m=m.append("1"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b2)
{
m=m.append("2"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b3)
{
m=m.append("3"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b4)
{
m=m.append("4"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b5)
{
m=m.append("5"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b6)
{
m=m.append("6"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b7)
{
m=m.append("7"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b8)
{
m=m.append("8"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b9)
{
m=m.append("9"); tex.setText(String.valueOf(m));
}
if(e.getSource()==jia)
{
m=m.append("+"); tex.setText(String.valueOf(m));
}
if(e.getSource()==jian)
{
m=m.append("-"); tex.setText(String.valueOf(m));
}
if(e.getSource()==cheng)
{
m=m.append("*"); tex.setText(String.valueOf(m));
}
if(e.getSource()==chu)
{
m=m.append("/"); tex.setText(String.valueOf(m));
}
if(e.getSource()==dian)
{
m=m.append("."); tex.setText(String.valueOf(m));
}
String mm=String.valueOf(m);
int p1=mm.indexOf("+");
int p2=mm.indexOf("-");
int p3=mm.indexOf("*");
int p4=mm.indexOf("/");
if(p1!=-1)
{
p=p1;
}
else if(p3!=-1)
{
p=p3;
}
else if(p2!=-1)
{
p=p2;
}
else if(p4!=-1)
{
p=p4;
}
if(e.getSource()==deng)
{
String m1=mm.substring(0,p);
String m2=mm.substring(p+1);
String ch=mm.substring(p,p+1);
//System.out.println(m1);
//System.out.println(m2);
//System.out.println(ch);
if(ch.equals("+"))
{
float n1=Float.parseFloat(m1);
float n2=Float.parseFloat(m2);
float sum=n1+n2;
String su=String.valueOf(sum);
tex.setText(su);
}
if(ch.equals("-"))
{
float n1=Float.parseFloat(m1);
float n2=Float.parseFloat(m2);
float sum=n1-n2;
String su=String.valueOf(sum);
tex.setText(su);
}
if(ch.equals("*"))
{
float n1=Float.parseFloat(m1);
float n2=Float.parseFloat(m2);
float sum=n1*n2;
String su=String.valueOf(sum);
tex.setText(su);
}
if(ch.equals("/"))
{
float n1=Float.parseFloat(m1);
float n2=Float.parseFloat(m2);
float sum=n1/n2;
String su=String.valueOf(sum);
tex.setText(su);
}
}
if(e.getSource()==qingling)
{StringBuffer kk=new StringBuffer();
m=kk;
tex.setText("0");
// System.out.println(mm);
}
if(e.getSource()==kaifang)
{
String t=tex.getText();
float num=Float.parseFloat(t);
double nub=Math.sqrt(num);
tex.setText(String.valueOf(nub));
}
}
}
基本格式:修饰符 class 类名(程序代码)
意义:Java中的程序代码都必须放在一个类中,对于类初学者可以简单地把它理解为一个java程序;类需要使用class作为关键字定义;而在class的前面可以有一些修饰符。
扩展资料
编写java时,特别需要注意的几个书写格式常见的错误:
1.java中的程序代码中功能执行语句的最后都必须用(;)结束。
这里需要注意的是,在程序中不要将英文的分号(;)误写成中文的分号(;) ,如果写成了中文的分号,编译器会报告“Invalid character”(无效字符)这样的错误信息。
2.Java语言是严格区分大小写的。在定义类时,不能将class写成Class,否则编译会报错。程序中定义一个computer的同时,还可以定义一个Computer,computer和Computer是两个全完不同的符号,在使用的时候需要注意。
3. 在编写java代码的时候为了便于阅读,通常会使用一种良好的格式进行排版,但这并不是必须的,我们也可以在两个单词或者符号之间任意换行。
参考资料:百度百科-java
意思是把x和y转换成字符串输出。
当x=0,y=0时,输出
0 0(空格)
当x=1,y=0时,输出
0 0 0 1(空格)
所以,最后输出就是:
0 0 0 1 0 2 0 3 0 4(最后还有一个空格)
我使用eclipse写的这个练习,首先给你截个图,抹掉的地方不是这个题的范畴,光标挡住的地方是ModelScore
然后,先是ModelScore代码:
package Models;
public class ModelScore {
private double mathScore;
private double englishScore;
private double javaScore;
private static final int type_Math = 0, type_English = 1, type_Java = 2;
public ModelScore(){
}
public int getNumberOfField(){
return 3;
}
public ModelScore(double mathScore, double englishScore, double javaScore){
this.mathScore = mathScore;
this.englishScore = englishScore;
this.javaScore = javaScore;
}
public double getMathScore(){
return this.mathScore;
}
public double getEnglishScore(){
return this.englishScore;
}
public double getJavaScore(){
return this.javaScore;
}
public void setMathScore(double mathScore){
this.mathScore = mathScore;
}
public void setEnglishScore(double englishScore){
this.englishScore = englishScore;
}
public void setJavaScore(double javaScore){
this.javaScore = javaScore;
}
public double getAverageScore(){
return (double)Math.round((this.mathScore + this.englishScore + this.javaScore) / 3 * 100) / 100;
}
public String toString(){
return "Scores:[Math: " + getMathScore() +
", English: " + getEnglishScore() + ", Java: " + getJavaScore() +
", AverageScore: " + getAverageScore() + "]";
}
public ModelScore clone(){
ModelScore score = new ModelScore();
score.setMathScore(this.getMathScore());
score.setEnglishScore(this.getEnglishScore());
score.setJavaScore(this.getJavaScore());
return score;
}
public double getScore(int type){
if(type == type_Math){
return mathScore;
}
if(type == type_English){
return englishScore;
}
if(type == type_Java){
return javaScore;
}
return 0;
}
}
然后是ModelStudent代码:
package Models;
public class ModelStudent {
private String studentID;
private String studentName;
private ModelScore score;
public ModelStudent(){
}
public ModelStudent(String studentID, String studentName, ModelScore score){
this.studentID = studentID;
this.studentName = studentName;
this.score = score.clone();
}
public String getStudentID(){
return this.studentID;
}
public String getStudentName(){
return this.studentName;
}
public ModelScore getStudentScore(){
return this.score.clone();
}
public void setStudentID(String studentID){
this.studentID = studentID;
}
public void setStudentName(String studentName){
this.studentName = studentName;
}
public void setStudentScore(ModelScore score){
this.score = score.clone();
}
public double getAverageScore(){
return score.getAverageScore();
}
public ModelStudent clone(){
ModelStudent student = new ModelStudent();
student.setStudentID(studentID);
student.setStudentName(studentName);
student.setStudentScore(score.clone());
return student;
}
public String toString(){
return "Student:[ ID: " + getStudentID() + ", Name: "
+ getStudentName() + ", " + getStudentScore() + "]";
}
public String getNameAndEverayScore(){
return "Name: " + studentName + ", average score: " + getAverageScore();
}
}
最后是主函数所在的类StudentScoreTester代码:
import java.util.ArrayList;
import java.util.Scanner;
import Models.ModelScore;
import Models.ModelStudent;
public class StudentScoreTester {
static final int type_Math = 0, type_English = 1, type_Java = 2 ;
public static void main(String[] args) throws NumberFormatException{
Scanner input = new Scanner(System.in);
String studentID = " ", studentName, searchedStudentName;
double studentMathScore, studentEnglishScore, studentJavaScore;
ModelScore studentScore;
ModelStudent student;
ArrayListModelStudent students = new ArrayListModelStudent();
ArrayListModelStudent studentsWithMaxMath, studentsWithMinMath;
ArrayListModelStudent studentsWithMaxEnglish, studentsWithMinEnglish;
ArrayListModelStudent studentsWithMaxJava, studentsWithMinJava;
ArrayListModelStudent searchedStudents;
double[] averageScores;
double averageScoreForMath, averageScoreForEnglish, averageScoreForJava;
double maxScoreForMath, maxScoreForEnglish, maxScoreForJava;
double minScoreForMath, minScoreForEnglish, minScoreForJava;
while(!studentID.equalsIgnoreCase("end")){
System.out.print("输入若干学生的学号,姓名,以及三科成绩(数学,英语,Java), 以输入end作为结束输入:");
studentID = input.next();
if(studentID.equalsIgnoreCase("end")){
break;
}
studentName = input.next();
studentMathScore = Double.valueOf(input.next());
studentEnglishScore = Double.valueOf(input.next());
studentJavaScore = Double.valueOf(input.next());
studentScore = new ModelScore(studentMathScore, studentEnglishScore, studentJavaScore);
student = new ModelStudent(studentID, studentName, studentScore);
students.add(student);
}
System.out.println("*******************************");
System.out.println("计算出所有学生平均成绩,并以降序显示......");
sortByAverageScore(students);
for(ModelStudent everyStudent: students){
System.out.println(everyStudent.getNameAndEverayScore());
}
System.out.println("*******************************");
System.out.println("全组每科平均成绩......");
averageScores = calculateAverageScore(students);
averageScoreForMath = averageScores[type_Math];
averageScoreForEnglish = averageScores[type_English];
averageScoreForJava = averageScores[type_Java];
System.out.println("数学: " + averageScoreForMath);
System.out.println("英语: " + averageScoreForEnglish);
System.out.println("Java: " + averageScoreForJava);
System.out.println("*******************************");
System.out.println("每科最好以及最差成绩的学生......");
studentsWithMaxMath = findStudentWithMaxScore(students, type_Math);
studentsWithMinMath = findStudentWithMinScore(students, type_Math);
studentsWithMaxEnglish = findStudentWithMaxScore(students, type_English);
studentsWithMinEnglish = findStudentWithMinScore(students, type_English);
studentsWithMaxJava = findStudentWithMaxScore(students, type_Java);
studentsWithMinJava = findStudentWithMinScore(students, type_Java);
System.out.println(" #########################");
System.out.println("数学最高成绩......");
for(ModelStudent studentWithMaxMath: studentsWithMaxMath){
System.out.println(studentWithMaxMath.getStudentName() + ", " + studentWithMaxMath.getStudentScore().getScore(type_Math));
}
System.out.println("\n数学最低成绩......");
for(ModelStudent studentWithMinMath: studentsWithMinMath){
System.out.println(studentWithMinMath.getStudentName() + ", " + studentWithMinMath.getStudentScore().getScore(type_Math));
}
System.out.println(" #########################");
System.out.println("英语最高成绩......");
for(ModelStudent studentWithMaxEnglish: studentsWithMaxEnglish){
System.out.println(studentWithMaxEnglish.getStudentName() + ", " + studentWithMaxEnglish.getStudentScore().getScore(type_English));
}
System.out.println("\n英语最低成绩......");
for(ModelStudent studentWithMinEnglish: studentsWithMinEnglish){
System.out.println(studentWithMinEnglish.getStudentName() + ", " + studentWithMinEnglish.getStudentScore().getScore(type_English));
}
System.out.println(" #########################");
System.out.println("Java最高成绩......");
for(ModelStudent studentWithMaxJava: studentsWithMaxJava){
System.out.println(studentWithMaxJava.getStudentName() + ", " + studentWithMaxJava.getStudentScore().getScore(type_Java));
}
System.out.println("\nJava最低成绩......");
for(ModelStudent studentWithMinJava: studentsWithMinJava){
System.out.println(studentWithMinJava.getStudentName() + ", " + studentWithMinJava.getStudentScore().getScore(type_Java));
}
System.out.println("*******************************");
System.out.print("输入要查找的学生姓名: ");
input.nextLine();
searchedStudentName = input.nextLine();
searchedStudents = searchStudentsByName(students, searchedStudentName);
System.out.println("查找结果是......");
if(searchedStudents.size() != 0){
for(ModelStudent everyStudent: searchedStudents){
System.out.println(everyStudent);
}
}
else{
System.out.println("查无此学生");
}
}
public static void sortByAverageScore(ArrayListModelStudent students){
for(int i = 0; i students.size() - 1; i++){
for(int j = i; j students.size(); j++){
if(students.get(i).getAverageScore() students.get(j).getAverageScore()){
ModelStudent tempStudent = students.get(i);
students.set(i, students.get(j));
students.set(j, tempStudent);
}
}
}
}
public static double[] calculateAverageScore(ArrayListModelStudent students){
double[] averageScore = new double[students.get(0).getStudentScore().getNumberOfField()];
for(int i = 0; i students.size(); i++){
ModelStudent currentStudent = students.get(i);
averageScore[type_Math] += currentStudent.getStudentScore().getMathScore();
averageScore[type_English] += currentStudent.getStudentScore().getEnglishScore();
averageScore[type_Java] += currentStudent.getStudentScore().getJavaScore();
}
averageScore[type_Math] = (double)Math.round(averageScore[type_Math]/ students.size() * 100) / 100;
averageScore[type_English] = (double)Math.round(averageScore[type_English]/ students.size() * 100) / 100;
averageScore[type_Java] = (double)Math.round(averageScore[type_Java]/ students.size() * 100) / 100;
return averageScore;
}
public static ArrayListModelStudent findStudentWithMaxScore(ArrayListModelStudent students, int type){
ArrayListModelStudent studentsWithMaxScore = new ArrayListModelStudent();
ModelStudent studentWithMaxScore = students.get(0);
for(ModelStudent everyStudent: students){
if(studentWithMaxScore.getStudentScore().getScore(type) everyStudent.getStudentScore().getScore(type)){
studentWithMaxScore = everyStudent.clone();
}
}
for(ModelStudent everyStudent: students){
if(studentWithMaxScore.getStudentScore().getScore(type) == everyStudent.getStudentScore().getScore(type)){
studentsWithMaxScore.add(everyStudent);
}
}
return studentsWithMaxScore;
}
public static ArrayListModelStudent findStudentWithMinScore(ArrayListModelStudent students, int type){
ArrayListModelStudent studentsWithMaxScore = new ArrayListModelStudent();
ModelStudent studentWithMaxScore = students.get(0);
for(ModelStudent everyStudent: students){
if(studentWithMaxScore.getStudentScore().getScore(type) everyStudent.getStudentScore().getScore(type)){
studentWithMaxScore = everyStudent.clone();
}
}
for(ModelStudent everyStudent: students){
if(studentWithMaxScore.getStudentScore().getScore(type) == everyStudent.getStudentScore().getScore(type)){
studentsWithMaxScore.add(everyStudent);
}
}
return studentsWithMaxScore;
}
public static ArrayListModelStudent searchStudentsByName(ArrayListModelStudent students, String studentName){
ArrayListModelStudent resultStudents = new ArrayListModelStudent();
for(ModelStudent everyStudent: students){
if(everyStudent.getStudentName().equals(studentName)){
resultStudents.add(everyStudent);
}
}
return resultStudents;
}
}