本文目錄一覽:
JAVA作業求助
做個練習,僅供參考。
代碼如下:
package com.hello.algorithm;
import java.util.Arrays;
import java.util.Scanner;
public class InsertSort {
public static void main(String[] args) {
// 創建一個數組存儲成績,需要指定長度
int[] scores = new int[6]; //假設存儲6個成績:100 99 85 82 63 60
int index = 0;
while(index scores.length) {
// 控制台接收成績
System.out.print(“請輸入成績(100 99 85 82 63 60):”);
Scanner scanner = new Scanner(System.in); // 隨機插入成績
int s = scanner.nextInt();
// 將接收的成績與數組中的所有成績進行比較,按照從大到小的規則,找到插入位置,然後插入數組
int insertPos = 0;
int temp = 0;
int temp2 = 0;
// 第一遍遍曆數組找到插入位置
for(int i = 0; i scores.length; i++){
if(s scores[i]){
insertPos = i;
temp = scores[i];
scores[i] = s;
break;
}
}
// 第二遍遍歷從插入位置整體後移
for(int i = 0; i scores.length; i++){
// 插入位置後面的元素整體後移
if(i insertPos){
temp2 = scores[i];
scores[i] = temp;
temp = temp2;
}
}
// 列印該成績的插入位置以及列印整個數組
System.out.println(“插入成績的下標:” + insertPos);
System.out.println(Arrays.toString(scores));
index++;
}
}
}
運行結果如下:
請輸入成績(100 99 85 82 63 60):63
插入成績的下標:0
[63, 0, 0, 0, 0, 0]
請輸入成績(100 99 85 82 63 60):60
插入成績的下標:1
[63, 60, 0, 0, 0, 0]
請輸入成績(100 99 85 82 63 60):82
插入成績的下標:0
[82, 63, 60, 0, 0, 0]
請輸入成績(100 99 85 82 63 60):99
插入成績的下標:0
[99, 82, 63, 60, 0, 0]
請輸入成績(100 99 85 82 63 60):85
插入成績的下標:1
[99, 85, 82, 63, 60, 0]
請輸入成績(100 99 85 82 63 60):100
插入成績的下標:0
[100, 99, 85, 82, 63, 60]
Process finished with exit code 0
Java作業
class Student {
private int studentNo;
private String name;
private int[] score;
public Student(int no, String name, int[] score){
this.studentNo = no;
this.name = name;
this.score = score;
}
public int getStudentNo(){
return this.studentNo;
}
public String getName() {
return this.name;
}
public int[] getScore() {
return this.score;
}
}
JAVA作業
1.1、System.out.println(a+b);
1.2、boolean,true;
2、public static intsumDigits(long n){
String tmp = n+””;
int total = 0;
for(int i=0;itmp.length;i++){
total+=Integer.parseInt(tmp.charAt(i)+””);
}
System.out.println(total);
}
3、
public class Calc {
public static void main(String[] args) {
System.out.println(calcNums());
}
public static int calcNums(){
int total = 0;
for(int i=1;i=100;i++){
total+=i;
}
return total;
}
}
4、
public class wendu {
public static void main(String[] args) {
System.err.println(toHuashi(78));
System.err.println(toSheshi(78));
}
public static float toHuashi(float c){
return (c*9/5)+32;
}
public static float toSheshi(float f){
return (f-32)*5/9;
}
}
5、
public static void judge(int score){
if(score60){
System.out.println(“差”);
}else if(score75){
System.out.println(“中”);
}else if(score90){
System.out.println(“良”);
}else{
System.out.println(“優”);
}
}
java簡單作業題
public class MyDate {
private int year ;
private int month ;
private int day ;
public MyDate(){}
public MyDate(int year, int month, int day) {
super();
this.year = year;
this.month = month;
this.day = day;
}
public String toString() {
return “MyDate ==”+year+”-“+month+”-“+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 class MyTime {
public static void main(String[] args) {
MyTime time = new MyTime(14,53,20);
System.out.println(time.toString());
}
private int hour;
private int minute;
private int second;
public MyTime() {
}
public MyTime(int hour, int minute, int second) {
super();
this.hour = hour;
this.minute = minute;
this.second = second;
}
public String toString() {
return “current time==”+hour + “:” + minute + “:” + second;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
this.second = second;
}
}
public class FullTime {
public static void main(String[] args) {
MyDate myDate = new MyDate(2007, 10, 2);
MyTime myTime = new MyTime(14,17,35);
FullTime fullTime = new FullTime(myDate,myTime);
System.out.println(fullTime);
}
private MyDate myDate;
private MyTime myTime;
public FullTime(MyDate myDate, MyTime myTime) {
super();
this.myDate = myDate;
this.myTime = myTime;
}
public String toString() {
String text = myDate.getYear() + “年” + myDate.getMonth() + “月”
+ myDate.getDay() + “日” + myTime.getHour() + “時”
+ myTime.getMinute() + “分” + myTime.getSecond() + “秒”;
return text;
}
public MyDate getMyDate() {
return myDate;
}
public void setMyDate(MyDate myDate) {
this.myDate = myDate;
}
public MyTime getMyTime() {
return myTime;
}
public void setMyTime(MyTime myTime) {
this.myTime = myTime;
}
}
第4題,你自己想辦法吧。主要知識點:
1、繼承
2、super和final,這個只是表面的東西,說到底還是java中overrides(重寫)的要求
3、通過多層間接的繼承,你要知道的是 對象被實例化的順序。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/239473.html