扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
按照你的要求编写的Java程序如下
创新互联建站是一家集网站建设,班戈企业网站建设,班戈品牌网站建设,网站定制,班戈网站建设报价,网络营销,网络优化,班戈网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
public class CC {
public static void main(String[] args) {
int N=5;
for(int i=0;iN;i++){
for(int j=0;jN-i;j++){
System.out.print(" ");
}
for(int j=0;j=i;j++){
System.out.print((char)('a'+i)+" ");
}
System.out.println();
}
}
}
运行结果
public class JavaPractise {
public static void main(String[] args) {
JavaPractise o = new JavaPractise();
String str = "1342973451345";
String subStr = o.getMaxLengthAddPart(str);
System.out.println(subStr);
String binaryFlow = "000011110000011111";
String sameSubStr = o.getMaxSameSubStr(binaryFlow);
System.out.println(sameSubStr);
String letterFlow = "aaBBBcCCCCddddddEEEE";
String sameLettersSubStr = o.getMaxSameLettersSubStr(letterFlow);
System.out.println(sameLettersSubStr);
}
/**
* 校验字符串是否符合数字规范
* */
private boolean isNum(String str){
boolean result = true;
if(str.length() 1)
return false;
for(int i = 0 ; i str.length(); i ++){
if(str.charAt(i) '0' || str.charAt(i) '9')
return false;
}
return result;
}
private boolean isBinaryFlow(String binaryFlow){
boolean result = true;
if(binaryFlow.length() 1)
return false;
for(int i = 0 ; i binaryFlow.length(); i ++){
if(binaryFlow.charAt(i) != '0' binaryFlow.charAt(i) != '1')
return false;
}
return result;
}
private boolean isLetterFlow(String letterFlow){
boolean result = true;
if(letterFlow.length() 1)
return false;
for(int i = 0 ; i letterFlow.length(); i ++){
char cur = letterFlow.charAt(i);
if(!( ('z'= cur cur = 'a') || ('Z' = cur cur 'A') ) )
return false;
}
return result;
}
/**
* 获取最长增长数字的子串
* */
private String getMaxLengthAddPart(String str){
str = str.trim();
if(!isNum(str))
return "";
String temp = ""+str.charAt(0);
String maxSubStr = temp;
for(int i = 1; i str.length(); i++){
char curChar = str.charAt(i);
char lastChar = str.charAt(i-1);
if( curChar lastChar){
temp += curChar;
if(i != (str.length()-1))
continue;//如果当前节点为最后一个节点,那么强制进入下面环节,进行
}
maxSubStr = maxSubStr.length() temp.length() ? maxSubStr : temp;
temp = ""+curChar; //清空原数据,并从当前节点记录
}
return maxSubStr;
}
/**
* 二进制流最长字符串判定
* */
private String getMaxSameSubStr(String str){
str = str.trim();
if(!isBinaryFlow(str))
return "";
String temp = ""+str.charAt(0);
String maxSubStr = temp;
for(int i = 1; i str.length(); i++){
char curChar = str.charAt(i);
char lastChar = str.charAt(i-1);
if( curChar == lastChar){
temp += curChar;
if(i != (str.length()-1))
continue;//如果当前节点为最后一个节点,那么强制进入下面环节,进行
}
maxSubStr = maxSubStr.length() temp.length() ? maxSubStr : temp;
temp = ""+curChar; //清空原数据,并从当前节点记录
}
return maxSubStr;
}
/**
* 字符串的最长相同部分截取
* */
private String getMaxSameLettersSubStr(String str){
str = str.trim();
if(!isLetterFlow(str))
return "";
String temp = ""+str.charAt(0);
String maxSubStr = temp;
for(int i = 1; i str.length(); i++){
char curChar = str.charAt(i);
char lastChar = str.charAt(i-1);
if( curChar == lastChar){
temp += curChar;
if(i != (str.length()-1))
continue;//如果当前节点为最后一个节点,那么强制进入下面环节,进行
}
maxSubStr = maxSubStr.length() temp.length() ? maxSubStr : temp;
temp = ""+curChar; //清空原数据,并从当前节点记录
}
return maxSubStr;
}
}
运行结果如下:
我使用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;
}
}
仅仅是给窗口添加背景的话是很简单的,添加上以下语句(自己去添加变量哈):\x0d\x0a\x0d\x0alabel = new JLabel(background); //background为ImageIcon\x0d\x0a// 把标签的大小位置设置为图片刚好填充整个面板 \x0d\x0alabel.setBounds(0, 0, this.getWidth(), this.getHeight());\x0d\x0a//添加图片到frame的第二层(把背景图片添加到分层窗格的最底层作为背景)\x0d\x0athis.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));\x0d\x0a//把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明\x0d\x0ajPanel=(JPanel)this.getContentPane();\x0d\x0a//设置透明\x0d\x0ajPanel.setOpaque(false);\x0d\x0a\x0d\x0a然后你上面那个JPanel p也设置成透明就可以了
(a)It is insert sort algorithm of the above code segment.上面的代码是插入排序算法
(b)
EXAMPLE
AEXMPLE
AEMXPLE
AEMPXLE
AELMPXE
AEELMPX
(c)
Require 6 sorting steps.
补充完整的插入排序算法的Java程序如下
public class A{
public static void main(String[] args){
char[] a={'E','X','A','M','P','L','E'};
char v;
int i,j,n=a.length,count=0;
for (i =1; i n; i++){
v = a[i];
j = i;
while(j 0 a[j-1] v) {
a[j] = a[j-1];
j--;
}
a[j] = v;
count++;
System.out.println(new String(a));
}
System.out.println("Require "+count+" sorting steps.");
}
}
StackExtension类我测试过了,没有问题.
完整的StackExtension类如下
import dataStructures.LinkedStack;
public class StackExtension extends LinkedStack{
public int sum(){
int n=0;
int size=super.size();
for(int i=0;isize;i++){
n=n+Integer.parseInt(String.valueOf(super.pop()));
}
return n;
}
public String displayPalindromes(){
String s="";
int size=super.size();
for(int i=0;isize;i++){
s=s+String.valueOf(super.get(i))+" ";
}
for(int i=0;isize;i++){
s=s+String.valueOf(super.pop())+" ";
}
return s;
}
}
运行结果
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流