本文目錄一覽:
初學java 請教高手解題過程
1題:
public class sum {
public static void main(String args[]){
int sum=0;
for(int i=1;i=100;i++){
sum+=i;
}
System.out.println(“sum=”+sum);
}
}
4題:
public class sum {
public static void main(String args[]){
int i,j,sum=0;
for(i=1;i=9;i++)
{
for(j=i;j=9;j++){
sum=i*j;
System.out.print(sum);
System.out.print(” “);
}
System.out.println();
}
}
}
5題:
public class sum {
public int radom(int a,int b){
int r = a + (int)(Math.random()*(b-a+1));
return r;
}
}
6題:values[0]的值是5
7題:
public class MyDate {
int year;
int month;
int day;
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public String getDate(){
return year+”-“+month+”-“+day ;
}
}
9題:
實例變量就是每個對象實例化之後,本身擁有的變量,作用範圍就是該對象實例本身,而靜態變量就象一個全局變量一樣,每個該對象的實例都共同擁有這個變量,一個實例修改該變量之後,其他的實例都能訪問到該改變後的變量。
10題:
在同一個類內 在同一個包內 子類 所有類
public 可以訪問 可以訪問 可以訪問 可以訪問
protect 可以訪問 可以訪問 可以訪問 不可以訪問
default 可以訪問 可以訪問 不可以訪問 不可以訪問
private 可以訪問 不可以訪問 不可以訪問 不可以訪問
java新手求大神解答。主要求代碼,有思路但是寫不出啊。
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
enum Huo {
TX(1, “T恤”, 245), WQX(2, “網球鞋”, 570), WQP(3, “網球拍”, 320);
private int id;
private String name;
private double price;
private Huo(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public static Huo getById(int id) {
for (Huo item : Huo.values()) {
if (item.id == id) {
return item;
}
}
return null;
}
}
public class Test {
private static MapInteger, Integer countMap = new HashMapInteger, Integer();
private static double sum = 0;
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
while (true) {
System.out.println(“**********************************************”);
System.out.println(“請選擇購買的商品編號:”);
for (Huo item : Huo.values()) {
System.out.print(item.getId() + “:” + item.getName() + ” “);
}
System.out.println();
System.out.println(“**********************************************”);
System.out.print(“輸入購買商品編號:”);
int id = in.nextInt();
System.out.print(“輸入購買商品數量:”);
int count = in.nextInt();
Integer tmp = countMap.get(id);
if (tmp == null) {
tmp = 0;
}
countMap.put(id, tmp + count);
show();
while (true) {
System.out.print(“是否繼續(y/n):”);
String jixu = in.next();
if (“Y”.equalsIgnoreCase(jixu)) {
break;
} else if (“N”.equalsIgnoreCase(jixu)) {
System.out.print(“請支付:”);
double zhifu = in.nextDouble();
System.out.println(“應付金額:” + sum);
System.out.println(“客戶支付:” + zhifu);
System.out.println(“找錢:” + (zhifu – sum));
System.exit(0);
} else {
System.out.print(“輸入錯誤”);
}
}
}
}
private static void show() {
IteratorInteger it = countMap.keySet().iterator();
while (it.hasNext()) {
Integer key = it.next();
Integer count = countMap.get(key);
Huo huo = Huo.getById(key);
double heji = huo.getPrice() * count;
System.out.println(huo.getName() + “¥” + huo.getPrice() + ” 數量:” + count + ” 合計:” + heji);
sum += heji;
}
}
}
java編程題 本人新手,求詳解。
先看下最終的結果吧,是不是你想要的?
項目結構如下圖所示:
其中,Student是父類,PostGraduate是子類,繼承自父類Student,Main是主類,用於創建對象以及把這些對象的功能調用起來。
—————————Student代碼如下:——————————
/**
* 學生類
* @author 逍遙
*
*/
public class Student {
//學號
private int sId;
//姓名
private String sName;
//數學成績
private double mathScore;
//計算機成績
private double computerScore;
/**
* 獲取學號
* @return
*/
public int getsId() {
return sId;
}
/**
* 設置學號
* @param sId
*/
public void setsId(int sId) {
this.sId = sId;
}
/**
* 獲取姓名
* @return
*/
public String getsName() {
return sName;
}
/**
* 設置姓名
* @param sName
*/
public void setsName(String sName) {
this.sName = sName;
}
/**
* 獲取數學成績
* @return
*/
public double getMathScore() {
return mathScore;
}
/**
* 設置數學成績
* @param mathScore
*/
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
/**
* 獲取計算機成績
* @return
*/
public double getComputerScore() {
return computerScore;
}
/**
* 設置計算機成績
* @param computerScore
*/
public void setComputerScore(double computerScore) {
this.computerScore = computerScore;
}
/**
* 輸出成員變量(4個成員變量)的信息。
*/
public void print(){
System.out.println(“學號:”+sId);
System.out.println(“姓名:”+sName);
System.out.println(“計算機成績:”+mathScore);
System.out.println(“數學成績:”+computerScore);
}
}
—————————Student代碼結束——————————
—————————PostGraduate代碼如下:——————————
/**
* 研究生類
* @author 逍遙
*
*/
public class PostGraduate extends Student{
//導師姓名
private String tName;
//研究方向
private String ResearchDirection;
/**
* 獲取導師姓名
* @return
*/
public String gettName() {
return tName;
}
/**
* 設置導師姓名
* @param tName
*/
public void settName(String tName) {
this.tName = tName;
}
/**
* 獲取研究方向
* @return
*/
public String getResearchDirection() {
return ResearchDirection;
}
/**
* 設置研究方向
* @param researchDirection
*/
public void setResearchDirection(String researchDirection) {
ResearchDirection = researchDirection;
}
/**
* 研究生類重寫父類的void print()方法,功能是輸出成員變量(6個成員變量)的信息
*/
@Override
public void print() {
// TODO Auto-generated method stub
super.print();
System.out.println(“導師姓名:”+tName);
System.out.println(“研究方向:”+ResearchDirection);
}
}
—————————PostGraduate代碼結束——————————
—————————Main代碼如下:——————————
import java.util.Scanner;
/**
* 主類
* @author 逍遙
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//用於獲取從鍵盤上輸入的信息
Scanner input=new Scanner(System.in);
//創建一個Student類的對象
Student student=new Student();
//從鍵盤上輸入其屬性信息
System.out.print(“請輸入學生的學號:”);
student.setsId(input.nextInt());
System.out.print(“請輸入學生的姓名:”);
student.setsName(input.next());
System.out.print(“請輸入學生的數學成績:”);
student.setMathScore(input.nextDouble());
System.out.print(“請輸入學生的計算機成績:”);
student.setComputerScore(input.nextDouble());
//並且通過其print方法輸出這些信息;
student.print();
//創建一個PostGraduate類的對象
PostGraduate postGraduate=new PostGraduate();
//從鍵盤上輸入其屬性信息
System.out.print(“請輸入研究生的學號:”);
postGraduate.setsId(input.nextInt());
System.out.print(“請輸入研究生的姓名:”);
postGraduate.setsName(input.next());
System.out.print(“請輸入研究生的數學成績:”);
postGraduate.setMathScore(input.nextDouble());
System.out.print(“請輸入研究生的計算機成績:”);
postGraduate.setComputerScore(input.nextDouble());
System.out.print(“請輸入研究生的導師姓名:”);
postGraduate.settName(input.next());
System.out.print(“請輸入研究生的研究方向:”);
postGraduate.setResearchDirection(input.next());
//並且通過其print方法輸出這些信息。
postGraduate.print();
}
}
—————————Main代碼結束——————————
=================知識點的簡單總結=================
本題考察的知識點是面向對象的三大特性之一:繼承。
Student為父類,包含了學號、姓名、數學成績和計算機成績4個屬性,以及一個print()方法。
PostGraduate 繼承父類的時候,繼承了父類中的所有方法,因為方法我都是用的public,而屬性繼承不了,因為我在父類中用了封裝,所有屬性都用private修飾了,想訪問屬性的話,必須通過get、set方法,這裡,我重寫了父類中的print方法,通過super.print();調用了父類中的print()方法。
最後就是Main類,提供了main方法作為入口函數,用於按要求聲明這些對象以及去調用對象中的方法。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/234080.html