用于鞏固java基礎知識,初學者多練多敲,熟悉代碼,熟悉語法就ok。
練習1、從控制臺獲取Java、ps、HTML三門課程的成績,計算總分和平均分(平均分保留2位小數(shù),要求四舍五入),輸出總分和平均分
import java.util.Scanner;
public class Type3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入Java成績");
double java = sc.nextDouble();
System.out.println("請輸入html成績");
double html = sc.nextDouble();
System.out.println("請輸入ps成績");
double ps = sc.nextDouble();
double totle= java + html + ps;
double avg= totle / 3;
System.out.printf("三門課程的總成績?yōu)椋?.0f 三門課程的平均成績?yōu)?%.2f",totle,avg);
}
}
練習2、控制臺輸入學生信息,學號 姓名 性別 年齡,控制臺展示學生信息如下
import java.util.Scanner;
public class Type2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入學號");
String id= sc.next();
System.out.println("請輸入姓名");
String name = sc.next();
System.out.println("請輸入性別");
char sex = sc.next().charAt(0);
System.out.println("學生信息如下:");
System.out.println("學號 姓名 性別");
System.out.printf("%s %s %s",id,name,sex);
}
}
?練習3、控制臺輸入一個4位的數(shù),求這個數(shù)各個位數(shù)之和并控制臺輸出,如:輸入? 1234,計算出1+2+3+4=10,把10這個結(jié)果在控制臺輸出。(無循環(huán))
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args) {
//輸入對象
Scanner sc = new Scanner(System.in);
System.out.println("請輸入4位數(shù):");
//讀取控制臺 1234
int num = sc.nextInt();
//分別求個、十、百、千、 位置的數(shù)字
int a = num%10;
int b = num/10%10;
int c = num/100%10;
int d = num/1000%10;
//格式化輸出
System.out.printf("%d的各位數(shù)字之和為:%d",num,a+b+c+d);
}
}
練習4
超市周年慶舉行購物滿減活動,編寫Java程序,根據(jù)用戶輸入的會員類型以及購物金額,判斷是否能夠享受活動優(yōu)惠,會員類型的輸入不限制大小寫。具體獲取規(guī)則:若為VIP會員,則直接享受活動優(yōu)惠;若為普通會員,則購物金額滿50元,即可享受活動優(yōu)惠;若為非會員,則購物金額滿100元,才能享受活動優(yōu)惠。
import java.util.Scanner;
public class Demo6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 1輸入會員類型
System.out.println("請輸入會員類型【A VIP會員 B 普通會員 C非會員】");
char type = scanner.next().charAt(0);
// 2輸入消費金額
System.out.println("輸入消費金額");
double money = scanner.nextDouble();
// 3判斷是否優(yōu)惠( A||a ) || (B||b && >50) ||(C||c && >100)
boolean result = ( type=='A'||type=='a' ) || ( ( type=='B'||type=='b' ) && money>50) || ( ( type=='C'||type=='c' ) && money>100) ;
// 4輸出信息 \t是一個tab鍵的空格
System.out.println("會員類型\t購物金額\t是否優(yōu)惠");
System.out.printf("%s \t %s \t %b",type,money,result);
}
}
練習5、輸出 100 內(nèi)能被3整除但是不能被4整除的所有奇數(shù)?
public class Whiledemo {
public static void main(String[] args) {
int i = 1;
while(i<=100){
if(i%3==0 && i%4!=0 && i%2!=0){
System.out.println(i);
}
i++;
}
}
}
練習6、一個循環(huán)求出100內(nèi)的奇數(shù)和、偶數(shù)和 (查看答案)
練習7、while循環(huán)輸出1-1000所有的能被3和4整除的數(shù),輸出在控制臺上
練習8、使用for循環(huán)求1~100之間不能被3整除的數(shù)之和
練習9、在控制臺上輸出如下圖案:
練習10、數(shù)字是有絕對值的,負數(shù)的絕對值是它本身取反,非負數(shù)的絕對值是它本身。請定義一個方法,方法能夠得到小數(shù)類型數(shù)字的絕對值并返回。請定義方法并測試。如:輸入-3.14 ? 輸出得到3.14
練習11、通過鍵盤錄入兩個整數(shù)n和m。n代表行數(shù),m代表列數(shù)。定義一個方法,方法的功能是打印n行m列的*符號。執(zhí)行效果如下:
?請輸入行數(shù):
4
請輸入列數(shù):
3
***
***
***
***
練習12【選做】、定義一個方法,該方法能夠找出三個整數(shù)中的最大值并返回。在主方法中調(diào)用方法測試執(zhí)行。文章來源:http://www.zghlxwxcb.cn/news/detail-429963.html
練習13【選做】、定義一個方法,該方法能夠找出兩個小數(shù)中的較小值并返回。在主方法中調(diào)用方法進行測試。
?文章來源地址http://www.zghlxwxcb.cn/news/detail-429963.html
到了這里,關(guān)于【001-Java基礎練習】-適合初學者的練習的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!