本文目录一览:
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/n/239473.html