- 企業(yè)發(fā)放的獎(jiǎng)金根據(jù)利潤提成殴胧。利潤低于或等于10萬元時(shí)牙躺,獎(jiǎng)金可提10%扬霜;利潤高于10萬元,低于20萬元時(shí)啥寇,低于10萬元的部分按10%提成,高于10萬元的部分洒扎,可提成7.5%辑甜;20萬到40萬之間時(shí),高于20萬元的部分袍冷,可提成5%磷醋;40萬到60萬之間時(shí)高于40萬元的部分,可提成3%胡诗;60萬到100萬之間時(shí)邓线,高于60萬元的部分,可提成1.5%煌恢,高于100萬元時(shí)骇陈,超過100萬元的部分按1%提成,從鍵盤輸入當(dāng)月利潤瑰抵,求應(yīng)發(fā)放獎(jiǎng)金總數(shù)你雌?
public class Answer {
public static void main(String[] args) {
double x = 0,y = 0;
System.out.print("輸入當(dāng)月利潤(萬):");
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)該提取的獎(jiǎng)金是 " + y + "萬");
}
}
- 給定一個(gè)成績a婿崭,使用switch結(jié)構(gòu)求出a的等級(jí)。A:90-100氓栈,B:80-89,C:70-79婿着,D:60-69颤绕,E:0~59(知識(shí)點(diǎn):條件語句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("請輸入一個(gè)成績: ");
x = s.nextInt();
if (x > 0 && x <= 100) {//判斷成績是否合法,如果合法祟身,進(jìn)行比較
grade = x/10;
switch(grade){
case 10:
case 9:System.out.println("等級(jí)為A");break;
case 8:System.out.println("等級(jí)為B");break;
case 7:System.out.println("等級(jí)為C");break;
case 6:System.out.println("等級(jí)為D");break;
default:System.out.println("等級(jí)為E");break;
}
} else {//判斷成績是否合法奥务,如果非法,進(jìn)行提示用戶
System.out.println("輸入的成績必須在0-100之間" );
}
}
}
- 假設(shè)某員工今年的年薪是30000元袜硫,年薪的年增長率6%氯葬。編寫一個(gè)Java應(yīng)用程序計(jì)算該員工10年后的年薪,并統(tǒng)計(jì)未來10年(從今年算起)總收入婉陷。
public class Exercise_03_03 {
public static void main(String[] args) {
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);
}
}
- 猴子吃桃問題:猴子第一天摘下若干個(gè)桃子帚称,當(dāng)即吃了一半官研,還不癮,又多吃了一個(gè) 第二天早上又將剩下的桃子吃掉一半闯睹,又多吃了一個(gè)戏羽。以后每天早上都吃了前一天剩下 的一半零一個(gè)。到第10天早上想再吃時(shí)楼吃,見只剩下一個(gè)桃子了始花。求第一天共摘了多少。程序分析:采取逆向思維的方法孩锡,從后往前推斷酷宵。
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 + " 個(gè)桃子");
}
}
- 輸入一個(gè)數(shù)字,判斷是一個(gè)奇數(shù)還是偶數(shù)
public class Exercise_03_05 {
public static void main(String[] args) {
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ù)");
}
}
}
- 編寫程序躬窜, 判斷一個(gè)變量x的值浇垦,如果是1,輸出x=1荣挨,如果是5男韧,輸出x=5,如果是 10默垄,輸出x=10煌抒,除了以上幾個(gè)值,都輸出x=none厕倍。
public class Exercise_03_05 {
public static void main(String[] args) {
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;
}
}
}
}
- Switch說明
? 表達(dá)式的值只可以接受int寡壮、byte、char讹弯、short 型况既,不接受其他類型的值
?不允許有重復(fù)的case取值
? switch一旦碰到第一次case匹配,程序就會(huì)跳轉(zhuǎn)到這個(gè)標(biāo)簽位置组民,開始順序執(zhí)行以后所有的程序代碼臭胜,而不管后面的case條件是否匹配耸三,直到碰到break語句為止
- 判斷一個(gè)數(shù)字是否能被5和6同時(shí)整除(打印能被5和6整除),或只能被5整除(打印能被5整除)憨颠,或只能被6整除爽彤,(打印能被6整除)适篙,不能被5或6整除,(打印不能被5或6整除)
public class Exercise_03_06 {
public static void main(String[] args) {
System.out.println("***********請輸入一個(gè)整數(shù)*********");
Scanner scanner = new Scanner(System.in);
int value = scanner.nextInt();
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整除");
}
}
}
- 輸入一個(gè)年份,判斷這個(gè)年份是否是閏年
public class Exercise_03_07 {
public static void main(String[] args) {
int year=2012;
if(year%4==0&&year%100!=0||year%400==0){
System.out.println("閏年");
}else{
System.out.println("不是閏年");
}
}
}
- 輸入一個(gè)0~100的分?jǐn)?shù),如果不是0~100之間翁都,打印分?jǐn)?shù)無效柄慰,根據(jù)分?jǐn)?shù)等級(jí)打印A,B,C,D,E
public class Exercise_03_08 {
public static void main(String[] args) {
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("分?jǐn)?shù)無效");
}
}
- 試寫一個(gè)三位數(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("輸入第一個(gè)數(shù)字:");
x = fnc.input();
System.out.print("輸入第二個(gè)數(shù)字:");
y = fnc.input();
System.out.print("輸入第三個(gè)數(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( "三個(gè)數(shù)字由小到大排列為: "+x + " " + y + " " + z);
}
}
class input{
public int input() {
int value = 0;
Scanner s = new Scanner(System.in);
value = s.nextInt();
return value;
}
}
- 有一個(gè)不多于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("請輸入一個(gè)不多于五位的正整數(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;
}
}
- 編寫一個(gè)程序乐设,計(jì)算郵局匯款的匯費(fèi)近尚。如果匯款金額小于100元肿男,匯費(fèi)為一元却嗡,如果金額在100元與5000元之間窗价,按1%收取匯費(fèi)撼港,如果金額大于5000元,匯費(fèi)為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("匯費(fèi)為:" + a);
}
}
- 分別使用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);
}
}
}
- 輸出0-9之間的數(shù)茅主,但是不包括5土榴。
public class Answer {
public static void main(String[] args)
{
for(int i=0;i<10;i++)
{
if(i==5)
{
continue;
}
System.out.println(i);
}
}
}
- 編寫一個(gè)程序玷禽,求整數(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);
}
}
- 編寫一個(gè)程序撩银,找出大于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;
}
}
}
- 由命令行輸入一個(gè)4位整數(shù),求將該數(shù)反轉(zhuǎn)以后的數(shù)恭应,如原數(shù)為1234耘眨,反轉(zhuǎn)后的數(shù)位4321
public class Answer {
public static void main(String[] args) {
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);
}
}