運(yùn)行代碼
創(chuàng)新互聯(lián)建站服務(wù)項(xiàng)目包括尉犁網(wǎng)站建設(shè)、尉犁網(wǎng)站制作、尉犁網(wǎng)頁制作以及尉犁網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,尉犁網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到尉犁省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
具體的代碼如下:
import java.util.HashMap;
public class SearchDemo {
public HashMapInteger,Integer countMap(char[] arr){
HashMap countMap = new HashMapInteger, Integer();
for (int i = 0; i arr.length; i++) {
if(!countMap.containsKey(arr[i])){
countMap.put(arr[i],1);
}else{
int value = (int)countMap.get(arr[i]) + 1;
countMap.put(arr[i],value);
}
}
return countMap;
}
public static void main(String[] args) {
char[] arr={'A','1','R','!','e','','A','5','A','g','#','D'};
int count = new SearchDemo().countMap(arr).get('A');
System.out.println("其中'A'字符的個(gè)數(shù)是:" + count);
}
}
運(yùn)行結(jié)果
按照題目要求編寫的用javaBean規(guī)范設(shè)計(jì)的學(xué)生類Student的Java程序如下
需要?jiǎng)?chuàng)建user.java.test包,把Student.java文件和Test.java文件放入包中,編譯Student.java文件并且編譯運(yùn)行Test.java文件得到運(yùn)行結(jié)果
Student.java文件代碼如下
package user.java.test;
import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
private String no;
private String name;
private double score;
public Student(){}
public Student(String no,String name,double score){
this.no=no;
this.name=name;
this.score=score;
}
public String getNo(){ return no;}
public void setNo(String no){ this.no=no;}
public String getName(){ return name;}
public void setName(String name){ this.name=name;}
public double getScore(){ return score;}
public void setScore(double score){ this.score=score;}
public String toString(){
return "學(xué)號(hào):"+no+",姓名:"+name+",成績:"+score;
}
public static double getAvg(Student[] sArray){
double sum=0,avg;
for(int i=0;isArray.length;i++){
sum=sum+sArray[i].getScore();
}
avg=sum/sArray.length;
return avg;
}
}
Test.java文件代碼如下
package user.java.test;
public class Test{
public static void main(String[] args){
Student[] sArray=new Student[5];
sArray[0]=new Student("001","張三",89.5);
sArray[1]=new Student("002","李四",82.5);
sArray[2]=new Student("003","王五",93);
sArray[3]=new Student("004","趙六",73.5);
sArray[4]=new Student("005","孫七",66);
System.out.println("這些學(xué)生的平均分:"+Student.getAvg(sArray));
for(int i=0;isArray.length;i++){
System.out.println(sArray[i].toString());
}
}
}
此題的Employee類設(shè)計(jì)有缺陷,缺少員工薪水的屬性。根據(jù)題意只能寫出下面的程序:
abstract class Employee {
public abstract double earnings();
}
class YearWorker extends Employee {
@Override
public double earnings() {
return 0;
}
}
class MonthWorker extends Employee {
@Override
public double earnings() {
return 0;
}
}
class WeekWorker extends Employee {
@Override
public double earnings() {
return 0;
}
}
class Company {
Employee[] employee;
double salaries = 0;
Company(Employee[] employee) {
this.employee = employee;
}
public double salariesPay() {
salaries = 0;
//計(jì)算salaries
for(int i=0; iemployee.length; i++) {
salaries += employee[i].earnings();
}
return salaries;
}
}
public class HardWork {
public static void main(String[] args) {
Employee[] employee = new Employee[20];
for(int i=0; iemployee.length; i++) {
if(i % 3 == 0) {
employee[i] = new YearWorker();
}
else if(i % 3 == 1) {
employee[i] = new MonthWorker();
}
else if(i % 3 == 2) {
employee[i] = new WeekWorker();
}
}
Company company = new Company(employee);
System.out.println("公司年工資總額:" + company.salariesPay());
}
}
運(yùn)行結(jié)果:
公司年工資總額:0.0
下面是第一題,采納此答案發(fā)第二題代碼
========分割線=========
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Q1 {
public static void main(String[] args) {
try {
Scanner scanner=new Scanner(System.in);
String data = scanner.nextLine();
File file = new File("java\\code");
if (!file.exists()) {
file.mkdirs();
}
File file1=new File(file,"mydata.txt");
if (!file1.exists()) {
file1.createNewFile();
}
FileWriter fw = new FileWriter(file1.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(data);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}