2.3.4章課后習(xí)題

第二章課后習(xí)題:

1、 已知a,b均是整型變量,寫出將a,b兩個變量中的值互換的程序台颠。(知識點:變量和運算符綜合應(yīng)用)

public class Exercise_02_01 {
    public static void main(String[] args) {
        int a = 100;
        int b = 200;
        int tmp;
        //進行數(shù)據(jù)交換
        tmp = a;
        a = b;
        b = tmp;
        System.out.println("交換后的a:"+ a + ",b:" + b);
    }

}

2、 給定一個0~1000的整數(shù)勒庄,求各位數(shù)的和串前,例如345的結(jié)果是3+4+5=12注:分解數(shù)字既可以先除后模也可以先模后除(知識點:變量和運算符綜合應(yīng)用)

public class Exercise_02_02 {
    public static void main(String[] args) {
        //Scanner類是java.util包中提供的一個操作類,使用此類可以方便的完成輸入流的輸入操作实蔽。
        Scanner scan = new Scanner(System.in);
        //此行代碼會阻塞荡碾,等待用戶從鍵盤輸入int類型數(shù)據(jù),并接受數(shù)據(jù)賦值給變量i局装。
        int i = scan.nextInt();
        int x1 = i / 1000;
        int x2 = i / 100 % 10;
        int x3 = i / 10 % 10;
        int x4 = i % 10; 
        System.out.println(x1 + x2 + x3 + x4);
    }
}

3坛吁、 華氏溫度和攝氏溫度互相轉(zhuǎn)換,從華氏度變成攝氏度你只要減去32铐尚,乘以5再除以9就行了拨脉,將攝氏度轉(zhuǎn)成華氏度,直接乘以9宣增,除以5女坑,再加上32即行。

public class Exercise_02_03 {
    public static void main(String[] args) {
        float c = 45.0f;
    float f = 113.0f;
    float cTof = (f-32) * 5/9;
    float fToc = c*9/5+32;
    System.out.println("攝氏轉(zhuǎn)華氏 = "+cTof );
    System.out.println("華氏轉(zhuǎn)攝氏  = "+fToc );

    }
}

4统舀、 給定一個任意的大寫字母A~Z,轉(zhuǎn)換為小寫字母劳景。

public class Exercise_02_04 {
    public static void main(String[] args) {
        char c = 'A';
        //加32即小寫對應(yīng)字母
        System.out.println((char) (c + 32) );


    }
}

第三章課后習(xí)題

1.企業(yè)發(fā)放的獎金根據(jù)利潤提成誉简。利潤低于或等于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%提成,從鍵盤輸入當月利潤纫雁,求應(yīng)發(fā)放獎金總數(shù)煌往?

public class Answer {
    public static void main(String[] args) {
     double x = 0,y = 0;
     System.out.print("輸入當月利潤(萬):");
     Scanner s = new Scanner(System.in);
     x = s.nextInt();
     if(x > 0 && x <= 10) {
     y = x * 0.1;
     } else if(x > 10 && x <= 20) {
      y = 10 * 0.1 + (x - 10) * 0.075;
     } else if(x > 20 && x <= 40) {
      y = 10 * 0.1 + 10 * 0.075 + (x - 20) * 0.05;
     } else if(x > 40 && x <= 60) {
      y = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (x - 40) * 0.03;
     } else if(x > 60 && x <= 100) {
      y = 20 * 0.175 + 20 * 0.05 + 20 * 0.03 + (x - 60) * 0.015; 
     } else if(x > 100) {
      y = 20 * 0.175 + 40 * 0.08 + 40 * 0.015 + (x - 100) * 0.01;
     }
     System.out.println("應(yīng)該提取的獎金是 " + y + "萬");
    }
}

2、 給定一個成績a轧邪,使用switch結(jié)構(gòu)求出a的等級刽脖。A:90-100,B:80-89闲勺,C:70-79曾棕,D:60-69,E:0~59(知識點:條件語句switch)

public class Exercise_03_02 {
    public static void main(String[] args) {
        int x;
        int grade = 0;
        Scanner s = new Scanner(System.in);
        System.out.print("請輸入一個成績: ");
        x = s.nextInt();
                
        if (x > 0 && x <= 100) {//判斷成績是否合法菜循,如果合法翘地,進行比較
            grade = x/10;
            switch(grade){
                case 10:
                case 9:System.out.println("等級為A");break;
                case 8:System.out.println("等級為B");break;
                case 7:System.out.println("等級為C");break;
                case 6:System.out.println("等級為D");break;
                default:System.out.println("等級為E");break;
                
            }
                        
        } else {//判斷成績是否合法,如果非法癌幕,進行提示用戶
            System.out.println("輸入的成績必須在0-100之間" );
        }
        
        
        
    }
}

3. 輸入一個數(shù)字衙耕,判斷是一個奇數(shù)還是偶數(shù)

    int i = new Scanner(System.in).nextInt();
if(i!=0){
            System.out.println("0");
        }
        else if(i%2==1){
            System.out.println("奇數(shù)");
        }else if(i%2==0){
            System.out.println("偶數(shù)");
        }
}

4. 編寫程序, 判斷一個變量x的值勺远,如果是1橙喘,輸出x=1,如果是5胶逢,輸出x=5厅瞎,如果是 10,輸出x=10初坠,除了以上幾個值和簸,都輸出x=none。(答案SwitchDemo.java)

    int x=1;

switch(x)
{
case 1:
{
System.out.println("x=1");
break;
}
case 5:
{
System.out.println("x=5");
break;
}
case 10:
{
System.out.println("x=10");
break;
}
default:
{
System.out.println("none");
break;
}
}

5. 判斷一個隨機整數(shù)是否能被5和6同時整除(打印能被5和6整除)碟刺,或只能被5整除(打印能被5整除)锁保,或只能被6整除,(打印能被6整除)半沽,不能被5或6整除爽柒,(打印不能被5或6整除)

    int value = new Random().nextInt(100);
    if (value % 5 == 0 && value % 6 == 0) {
     System.out.println("輸入的數(shù)字" + value + "能被5和6整除");
    } else if (value % 5 == 0) {
     System.out.println("輸入的數(shù)字" + value + "能被5整除");
    } else if (value % 6 == 0) {
     System.out.println("輸入的數(shù)字" + value + "能被6整除");
    } else {
     System.out.println("輸入的數(shù)字不能被5或者6整除");
    }

6.輸入一個年份,判斷這個年份是否是閏年

    int year = new Scanner(System.in).nextInt();
    if(year%4==0&&year%100!=0||year%400==0){
        System.out.println("閏年"); 
    }else{
        System.out.println("不是閏年");
    } 

7.輸入一個0~100的分數(shù)者填,如果不是0~100之間浩村,打印分數(shù)無效,根據(jù)分數(shù)等級打印A,B,C,D,E

    int score = 999;
    if(score<=100&&score>=90)
        System.out.println("A"); 
    else if(score<90&&score>=80)
        System.out.println("B");
    else if(score<80&&score>=70)
        System.out.println("C");
    else if(score<70&&score>=60)
        System.out.println("D");
    else if(score<=70&&score>60)    
        System.out.println("E");
    else
        System.out.println("分數(shù)無效");

8.試寫一個三位數(shù)占哟,從小到大排列穴亏,然后再從大到小排列蜂挪。

import java.util.Scanner;

public class Answer {
    public static void main(String[] args) {
     input fnc = new input();
     int x=0, y=0, z=0;
     System.out.print("輸入第一個數(shù)字:");
      x = fnc.input();
     System.out.print("輸入第二個數(shù)字:");
      y = fnc.input();
     System.out.print("輸入第三個數(shù)字:");
      z = fnc.input();   
    if(x > y) {
      int t = x;
      x = y;
      y = t;
     }
    if(x > z) {
      int t = x;
      x = z;
      z = t;
     }
    if(y > z) {
      int t = y;
      y = z;
      z = t;
     }
    System.out.println( "三個數(shù)字由小到大排列為: "+x + " " + y + " " + z);
    }
    }
    class input{
    public int input() {
     int value = 0;
     Scanner s = new Scanner(System.in);
     value = s.nextInt();
     return value;
    }

}

9.有一個不多于5位的正整數(shù),求它是幾位數(shù)嗓化,分別打印出每一位數(shù)字棠涮。

import java.util.Scanner;
public class Answer {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
// TODO code application logic here
//int num=12345;
System.out.println("請輸入一個不多于五位的正整數(shù):");
int num = input();
String str = String.valueOf(num);
System.out.println(num + "的位數(shù)為:" + str.length());
System.out.println("它的各位數(shù)分別為:");
for (int i = 0; i < str.length(); i++) {
//System.out.println(str.charAt(i));
System.out.print(str.charAt(i) + " ");
}
System.out.println();
System.out.println("它的各位數(shù)逆序分別為:");
for (int i = str.length() - 1; i >= 0; i--) {
//System.out.println(str.charAt(i));
System.out.print(str.charAt(i) + " ");
}
System.out.println();
}

private static int input() {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
return n;
}
}

1.假設(shè)某員工今年的年薪是30000元,年薪的年增長率6%刺覆。編寫一個Java應(yīng)用程序計算該員工10年后的年薪严肪,并統(tǒng)計未來10年(從今年算起)總收入。

double nianxin=30000;
        long sum = 0;
        
        for(int i=1;i<=10;i++){
            nianxin = nianxin*(1+0.06);
            sum+=nianxin;
        }
        System.out.println("年薪為"+nianxin+"總工資為"+sum);

2.猴子吃桃問題:猴子第一天摘下若干個桃子谦屑,當即吃了一半驳糯,還不癮,又多吃了一個   第二天早上又將剩下的桃子吃掉一半氢橙,又多吃了一個酝枢。以后每天早上都吃了前一天剩下   的一半零一個。到第10天早上想再吃時悍手,見只剩下一個桃子了帘睦。求第一天共摘了多少。程序分析:采取逆向思維的方法坦康,從后往前推斷竣付。

public class Answer {
    public static void main(String[] args) {
     int x = 1;
     for(int i=2; i<=10; i++) {
      x = (x+1)*2;
     }
     System.out.println("猴子第一天摘了 " + x + " 個桃子");
    }


}

3. 編寫一個程序,計算郵局匯款的匯費滞欠。如果匯款金額小于100元古胆,匯費為一元,如果金額在100元與5000元之間筛璧,按1%收取匯費逸绎,如果金額大于5000元,匯費為50元夭谤。匯款金額由命令行輸入桶良。

public class Answer {

    public static void main(String[] args) {
        double A = Integer.parseInt(args[0]);
        double a = 0;
        if (A > 5000) {
            a = 50;
        } else if (100 <= A) {
            a = A * 0.01;
        } else if (100 > A) {
            a = 1.0;
        }
        System.out.println("匯費為:" + a);
    }

}

4. 分別使用for循環(huán),while循環(huán)沮翔,do循環(huán)求1到100之間所有能被3整除的整數(shù)的和。

public class Answer {
    public static void main(String[] args) {
        //for部分
        {
            int i, a = 0;
            for (i = 1; i <= 100; i++) {
                if (i % 3 == 0)
                    a = a + i;
            }
            System.out.println(a);
        }
        //while 部分
        {
            int i = 1, a = 0;
            while (i < 101) {
                if (i % 3 == 0) {
                    a = a + i;
                }
                i++;
            }
            System.out.println(a);
        }
//do while部分
        {
            int i = 0, a = 0;
            do {
                if (i % 3 == 0) {
                    a = a + i;
                }
                i++;
            } while (i < 101);
            System.out.println(a);
        }
    }
}

5. 輸出0-9之間的數(shù)曲秉,但是不包括5采蚀。
public static void main(String[] args)
{
for(int i=0;i<10;i++)
{
if(i==5)
{
continue;
}
System.out.println(i);
}
}
6. 編寫一個程序,求整數(shù)n的階乘承二,例如5的階乘是12345

import java.util.Scanner;
public class Answer {
    public static void main(String[] args) {
        System.out.println("input:");
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int i = 0;
        int m = 1;
        for (i = 1; i <= n; i++) {
            m = m * i;
        }
        System.out.println("n的階乘為:"+m);
    }
}

7. 編寫一個程序榆鼠,找出大于200的最小的質(zhì)數(shù)

public class Answer {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        for (int i = 200; i < 300; i++) {
            boolean b = true;
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                    b = false;
                    break;
                }
            }
            if (!b) {
                continue;
            }
            System.out.println(i);
            break;
        }
    }
}

8. 由命令行輸入一個4位整數(shù),求將該數(shù)反轉(zhuǎn)以后的數(shù)亥鸠,如原數(shù)為1234妆够,反轉(zhuǎn)后的數(shù)位4321

int a, b, c, d, s;
        int n = Integer.parseInt(args[0]);
        a = n / 1000;
        b = n / 100 % 10;
        c = n / 10 % 10;
        d = n % 10;
        s = d * 1000 + c * 100 + b * 10 + a;
        System.out.println("反轉(zhuǎn)后數(shù)為:" + s);

第四章練習(xí)題

1. 編寫一個簡單程序识啦,要求數(shù)組長度為5,分別賦值10神妹,20颓哮,30,40鸵荠,50冕茅,在控制臺輸出該數(shù)組的值。
/*例5-1
*數(shù)組使用范例
*/

public class ArrayDemo
{
public static void main(String[] args)
{
int[] buffer=new int[5];

buffer[0]=10;
buffer[1]=20;
buffer[2]=30;
buffer[3]=40;
buffer[4]=50;

for(int i=0;i<5;i++)
{
System.out.println(buffer[i]);
}
}
}

2.將一個字符數(shù)組的值(neusofteducation)考貝到另一個字符數(shù)組中蛹找。

public class ArrayCopyDemo {
public static void main(String[ ] args) {
//定義源字符數(shù)組 
char[ ]  copyFrom = {'n', 'e', 'u', 's', 'o', 'f', 't', 'e', 'd', 'u', 'c', 'a', 't', 'i', 'o', 'n'};
char[ ] copyTo = new char[7];

System.arraycopy(copyFrom, 2, copyTo, 0, 7);
System.out.println(new String(copyTo));

}
}

3. 給定一個有9個整數(shù)(1,6,2,3,9,4,5,7,8})的數(shù)組姨伤,先排序,然后輸出排序后的數(shù)組的值庸疾。

public class ArraySortDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] point = {1,6,2,3,9,4,5,7,8};
        
        java.util.Arrays.sort( point );
        
        for(int i=0;i<point.length;i++)
        {
        System.out.println(point[i]);
        }

    }

}

4. 輸出一個double型二維數(shù)組(長度分別為5乍楚、4,值自己設(shè)定)的值届慈。
/*例5-3
*多維數(shù)組范例
*/

public class ArrayTwoDimension
{
public static void main(String[] args)
{
double[][] buffer=new double[5][4];

for(int i=0;i<buffer.length;i++)
{
for(int j=0;j<buffer[0].length;j++)
{
System.out.print(buffer[i][j]);
}
System.out.println();
}
}
}

5. 在一個有8個整數(shù)(18徒溪,25,7拧篮,36词渤,13,2串绩,89缺虐,63)的數(shù)組中找出其中最大的數(shù)及其下標。

public class Arraymax {

    /**
     * @param args
     */
    public static void main(String[] args) {
        int[] a = {18,25,7,36,13,2,89,63};
        int max = a[0];
        int maxIndex = 0;
        for(int i=1;i<a.length;i++)
        {
            if(max<=a[i]){
                max = a[i];
                maxIndex = i;
            }
        }
        System.out.println("最大值為:"+max+" 最大值下標為:"+maxIndex);
    }

}

1礁凡、有2個多維數(shù)組分別是 2 3 4 和 1 5 2 8
4 6 8 5 9 10 -3
2 7 -5 -18
按照如下方式進行運算高氮。生成一個2行4列的數(shù)組。此數(shù)組的第1行1列是21+35+42
第1行2列是2
5+39+47 第2行1列是41+65+8*2 依次類推顷牌。

package com.neusoft.javaTest;

public class Array2 {

    /**
     * @param args
     */
    
    public static void main(String[] args) {
            int a[][] = { { 2, 3, 4 }, { 4, 6, 8 } };
            int b[][] = { { 1, 5, 2, 8 }, { 5, 9, 10, -3 }, { 2, 7, -5, -18 } };    
            for(int k=0;k<a.length;k++){                
                for(int i=0;i<b[0].length;i++){
                    int num = 0;
                    for(int j=0;j<b.length;j++){
                        num += a[k][j]*b[j][i];
                    }
                    System.out.print(num+"  ");
                }   
                System.out.println("");          
            }
            
    }

}

2. 將一個數(shù)組中的元素逆序存放

public class Answer {
public static void main(String[] args) {
       Scanner s = new Scanner(System.in);
       int a[] = new int[20];
    System.out.println("請輸入多個正整數(shù)(輸入-1表示結(jié)束):");
       int i=0,j;
       do{
      a[i]=s.nextInt();
      i++;
       }while (a[i-1]!=-1);
       System.out.println("你輸入的數(shù)組為:");
       for( j=0; j<i-1; j++) {
    System.out.print(a[j]+"   ");
    }
       System.out.println("\n數(shù)組逆序輸出為:");
       for( j=i-2; j>=0; j=j-1) {
    System.out.print(a[j]+"   ");
    }
    }

}


}

4剪芍、給定一維數(shù)組{ -10,2窟蓝,3罪裹,246,-100运挫,0状共,5} ,計算出數(shù)組中的平均值谁帕、最大值峡继、最小值。

    int a[] = new int[]{ -10,23,246,-100,0,5};
     int max = a[0];
     int min = a[0];
     int add = a[0];
     for(int i =1;i<a.length;i++){
         if(a[i]< min){
             min = a[i];
         }else if(a[i]>max){
             max = a[i];
         }
         add = add+a[i];
     }
    System.out.println("最小值:"+min);
    System.out.println("最大值:"+max);
System.out.println("平均值:"+add/a.length);
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末匈挖,一起剝皮案震驚了整個濱河市碾牌,隨后出現(xiàn)的幾起案子康愤,更是在濱河造成了極大的恐慌,老刑警劉巖舶吗,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件征冷,死亡現(xiàn)場離奇詭異,居然都是意外死亡裤翩,警方通過查閱死者的電腦和手機资盅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來踊赠,“玉大人呵扛,你說我怎么就攤上這事】鸫” “怎么了今穿?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長伦籍。 經(jīng)常有香客問我蓝晒,道長,這世上最難降的妖魔是什么帖鸦? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任芝薇,我火速辦了婚禮,結(jié)果婚禮上作儿,老公的妹妹穿的比我還像新娘洛二。我一直安慰自己,他們只是感情好攻锰,可當我...
    茶點故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布晾嘶。 她就那樣靜靜地躺著,像睡著了一般娶吞。 火紅的嫁衣襯著肌膚如雪垒迂。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天妒蛇,我揣著相機與錄音机断,去河邊找鬼。 笑死绣夺,一個胖子當著我的面吹牛吏奸,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播乐导,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼浸颓!你這毒婦竟也來了物臂?” 一聲冷哼從身側(cè)響起旺拉,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎棵磷,沒想到半個月后蛾狗,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡仪媒,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年沉桌,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片算吩。...
    茶點故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡留凭,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出偎巢,到底是詐尸還是另有隱情蔼夜,我是刑警寧澤,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布压昼,位于F島的核電站求冷,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏窍霞。R本人自食惡果不足惜匠题,卻給世界環(huán)境...
    茶點故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望但金。 院中可真熱鬧韭山,春花似錦、人聲如沸傲绣。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽秃诵。三九已至续搀,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間菠净,已是汗流浹背禁舷。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留毅往,地道東北人牵咙。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像攀唯,于是被迫代替她去往敵國和親洁桌。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內(nèi)容