Java数组&排序作业

1.考试成绩已经保存在数组scores中,依次为 89,23,64,91,119,52,73,-23


  要求根据通过自定义方法来找出其中前三名,将数组成绩作为参数传入
  要求判断成绩的有效性(0-100),如果成绩无效,则忽略此成绩.

public class Homework1{

public static void main(String[] args ){
int [] scores = {89,23,64,91,119,52,73,-23};
int i = 0;
int j = 0;
//对数组进行有效化选择
for( i = 0;i < scores.length;i++){
if(scores[i] <= 100 && scores[i] >= 0){
scores[j] = scores[i];
j++;
}
}
//找出前三名
for(int m = 0;m <= 2;m++){
for(int n = m+1;n < scores.length;n++){
if(scores[m] < scores[n]){
int temp = scores[m];
scores[m] = scores[n];
scores[n] = temp;

}

}
System.out.println( +scores[m]);


}

}

}

**************

2. 用数组来实现, 定义并初始化一个(1--100)保存100个数的数组,从第一个元素开始,依次数(1,2,3 循环往复),每次数到到3的元素淘汰掉.当到达数组

末尾的时候再从头开始,直到最后剩余一个元素,写出算法,并输出最后保留的元素所在的最初的位置.

public class Homework2{
public static void main(String[] args ){
int [] a = new int [100];
for(int i = 0;i <100;i++){
a[i] = i+1;
}
int i = 0;
int times = 0;
int over = 0;
int num = 0;
while(times != 100){
if(a[i] >= 0){
if(num == 2){
a[i] = -1;
times++;
num = 0;
over = i;
System.out.println(+over);
}else{
num++;
}
}
i++;
if(i == 100){
i = 0;
}
System.out.println(+over);
}
}
}

*******************

3. 用数组来实现对于一个整形数组, 分别通过冒泡排序和 快速排序,实现对于任意一个数组进行由小到大的排列。

public class Homework3{
public static void main(String [] args){
int [] a = new int [] {1,2,8,5,6,7,4,9,3,0};

for(int i = 0;i < a.length;i++){
for(int j = i+1;j < a.length;j++){
if(a[i] < a[j]){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for(int i = 0;i < a.length;i++){
System.out.println(+a[i]);
}
System.out.println("********************");
for(int i = 1 ; i < a.length; i++){
for(int j = 0 ;j < a.length-i;j++){
//交换两数位置
if(a[j] < a[j+1]){
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for(int i = 0;i < a.length;i++){
System.out.println(+a[i]);
}
}
}

******************

4.判断101-200之间有多少个素数,并输出所有素数。

public class Homework4{
public static void main(String[] args ){
int i;
int j;
for(i = 100;i <=200;i++){
int t = 1;
for(j = 2;j < i;j++){
if(i%j == 0){
t = 0;
break;
}
}
if(                                                                                                                                                                                               ){
System.out.println(+i);
}
}
}
}

***************

5.题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

public class Homework5{
public static void main(String[] args ){
int letter = 0;
int space = 0;
int number = 0;
int other = 0;
char [] a = new char []{'I',' ','l','o','v','e',' ','y','o','u'};
for(int i = 0;i < a.length;i++){
if(a[i] >= 'A' && a[i] <= 'Z' || a[i] >= 'a' && a[i] <= 'z'){
letter++; 
}else if(a[i] >= '0'&&a[i] <= '9'){
number++;
}else if(a[i] == ' '){
space++;
}else{
other++;
}
}
System.out.println("英文单词:"+letter);
System.out.println("数字:"+number);
System.out.println("空格:"+space);
System.out.println("其他:"+other);
}
}

6.题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,
高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,
高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润,求应发放奖金总数?


7.分别定义用户类,订单类,产品类,其中订单类至少要具有下订单的行为(入参为一个产品名称),
  产品类中至少有一个成员变量为产品名称,至少有一个方法用来获取产品的名称。
  用户类中持有一个产品类型的成员变量。
  用户类中持有一个订单类型的成员变量。
  
  在我们的用户类中定义main函数,当执行的时候,构造一个用户类的对象,
  并且通过手段可以拿到产品成员变量中的产品名称,并且调用成员变量订单的对象,进行下单。
  最后输出下单成功。

public class Homework7{
Product product=new Product();
Oder oder=new Oder();


public static void main(String[] args){
User user=new User();
user.product.getInputName();
user.oder.getOder();
}
}
class Product{
String productName;
public String getInputName(){
Scanner sc=new Scanner(System.in);
System.out.println(" 输入商品名称:");
productName=sc.next();
return productName;
}


}
class Oder{
String oderName;
void getOder(){
System.out.println("下单成功");
}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章