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("下單成功");
}
}


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