首页 / JAVA / Java基本语法(选择、循环结构)
Java基本语法(选择、循环结构)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java基本语法(选择、循环结构),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含11394字,纯文字阅读大概需要17分钟。
内容图文
![Java基本语法(选择、循环结构)](/upload/InfoBanner/zyjiaocheng/750/2862c90cc6794696a2705f0a879113f8.jpg)
编程题
第1题
语法点:变量,运算符,if...else
案例:从键盘输入一个整数,判断它是奇数还是偶数(这里把0归为偶数)
代码实现:
public class Test01{ public static void main(String[] args){ java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("请输入一个整数:"); int num = input.nextInt(); if(num % 2 == 0){ System.out.println(num + "是偶数"); }else{ System.out.println(num + "是奇数"); } } }
第2题
语法点:变量,运算符,if...else
案例:从键盘输入一个字符,判断它是字母还是数字,还是其他字符
代码实现:
public class Test02{ public static void main(String[] args){ java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("请输入一个字符:"); char c = input.next().charAt(0); if(c >= '0' && c <= '9'){ System.out.println(c + "是数字."); }else if(c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z'){ System.out.println(c + "是字母."); }else{ System.out.println(c + "非数字非字母的其他字符."); } } }
第3题
语法点:变量,运算符,if...else
按步骤编写代码:
1. 定义变量折扣 discount,初始化为1, 总价totalPrice的值从键盘输入
2. 判断当`totalPrice >=500` ,discount赋值为0.5
3. 判断当`totalPrice >=400` 且`<500`时,discount赋值为0.6
4. 判断当`totalPrice >=300` 且`<400`时,discount赋值为0.7
5. 判断当`totalPrice >=200` 且`<300`时,discount赋值为0.8
6. 判断当`totalPrice >=0` 且`<200`时,discount赋值为1
7. 判断当`totalPrice<0`时,显示输入有误
8. 输出结果import java.util.Scanner; public class IfTest3 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入总价格:"); double discount = 1; double totalPrice = scan.nextDouble(); if(totalPrice >= 500){ discount = 0.5; }else if(totalPrice >= 400){ discount = 0.6; }else if(totalPrice >= 300){ discount = 0.7; }else if(totalPrice >= 200){ discount = 0.8; }else if (totalPrice >= 0){ discount = 1.0; }else{ System.out.println("输出有误"); } System.out.println("总价:"+totalPrice); System.out.println("折扣:"+discount); System.out.println("折扣后总价:"+totalPrice*discount); } }
第4题
语法点:变量,运算符,if...else
案例:从键盘输入生日,判断星座
代码实现:
import java.util.Scanner; class IfTest4 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入您生日的月份:"); int month = scan.nextInt(); System.out.println("请输入您生日的日子:"); int day = scan.nextInt(); if ((month == 1 && day >= 20) || (month == 2 && day <= 18)) { System.out.println("生日" + month + "月" + day + "日是水瓶座"); } else if ((month == 2 && day >= 19) || (month == 3 && day <= 20)) { System.out.println("生日" + month + "月" + day + "日是双鱼座"); }else if ((month == 3 && day >= 21) || (month == 4 && day <= 19)) { System.out.println("生日" + month + "月" + day + "日是白羊座"); }else if ((month == 4 && day >= 20) || (month == 5 && day <= 20)) { System.out.println("生日" + month + "月" + day + "日是金牛座"); }else if ((month == 5 && day >= 21) || (month == 6 && day <= 21)) { System.out.println("生日" + month + "月" + day + "日是双子座"); }else if ((month == 6 && day >= 22) || (month == 7 && day <= 22)) { System.out.println("生日" + month + "月" + day + "日是巨蟹座"); }else if ((month == 7 && day >= 23) || (month == 8 && day <= 22)) { System.out.println("生日" + month + "月" + day + "日是狮子座"); }else if ((month == 8 && day >= 23) || (month == 9 && day <= 22)) { System.out.println("生日" + month + "月" + day + "日是处女座"); }else if ((month == 9 && day >= 23) || (month == 10 && day <= 23)) { System.out.println("生日" + month + "月" + day + "日是天平座"); }else if ((month == 10 && day >= 24) || (month == 11 && day <= 22)) { System.out.println("生日" + month + "月" + day + "日是天蝎座"); }else if ((month == 11 && day >= 23) || (month == 12 && day <= 21)) { System.out.println("生日" + month + "月" + day + "日是射手座"); }else if ((month == 12 && day >= 22) || (month == 1 && day <= 19)) { System.out.println("生日" + month + "月" + day + "日是摩羯座"); } } }
第5题
语法点:变量,运算符,switch...case
案例需求:编写一个程序,为一个给定的年份找出其对应的中国生肖。中国的生肖基于12年一个周期,每年用一个动物代表:rat(鼠)、ox(牛)、tiger(虎)、rabbit(兔)、dragon(龙)、snake(蛇)、
horse(马)、sheep(羊)、monkey(候)、rooster(鸡)、dog(狗)、pig(猪)。
提示:2017年:鸡 2017 % 12 == 1
代码实现:
import java.util.Scanner; class IfTest5 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入一个年份:"); int year = scan.nextInt(); switch(year % 12){ case 1: System.out.println("鸡"); break; case 2: System.out.println("狗"); break; case 3: System.out.println("猪"); break; case 4: System.out.println("鼠"); break; case 5: System.out.println("牛"); break; case 6: System.out.println("虎"); break; case 7: System.out.println("兔"); break; case 8: System.out.println("龙"); break; case 9: System.out.println("蛇"); break; case 10: System.out.println("马"); break; case 11: System.out.println("羊"); break; case 0: System.out.println("候"); break; } } }
第6题
语法点:变量,运算符,if..else
案例:
代码实现:
lass IfTest6 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("请输入方程的参数a:"); double a = scan.nextDouble(); System.out.print("请输入方程的参数b:"); double b = scan.nextDouble(); System.out.print("请输入方程的参数c:"); double c = scan.nextDouble(); double x1,x2; if(a != 0){ if(b*b - 4*a*c > 0){ x1 = (-b + Math.sqrt(b*b - 4*a*c))/(2*a); x2 = (-b - Math.sqrt(b*b - 4*a*c))/(2*a); System.out.println("此方程有两个解:x1 = "+x1+",x2 = "+x2); }else if(b*b - 4*a*c == 0){ x1 = -b/(2*a); System.out.println("此方程有一个解:x1 = "+x1); }else{ System.out.println("此方程无解"); } }else if(a==0 && b!=0){ x1 = -c/b; System.out.println("一个解:x1 = "+x1); }else{ System.out.println("不是方程!"); } } }
第7题
语法点:变量,运算符,if和switch...case
案例:已知2019年1月1日是星期二,从键盘输入2019年的任意一天,请判断它是星期几
代码实现:
public class Test07 { public static void main(String[] args){ //1、从键盘分别输入年、月、日 java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("月:"); int month = input.nextInt(); System.out.print("日:"); int day = input.nextInt(); //判断这一天是当年的第几天==>从1月1日开始,累加到xx月xx日这一天 //(1)[1,month-1]个月满月天数 //(2)第month个月的day天 //(3)单独考虑2月份是否是29天(依据是看year是否是闰年) //2、声明一个变量days,用来存储总天数 //int days = 0; //累加第month个月的day天 //days += day; //修改上面的代码,直接把days初始化为day int days = day; //3、累加[1,month-1]个月满月天数 switch(month){ case 12: //累加的1-11月 days += 30;//这个30是代表11月份的满月天数 //这里没有break,继续往下走 case 11: //累加的1-10月 days += 31;//这个31是代表10月的满月天数 //这里没有break,继续往下走 case 10: days += 30;//9月 case 9: days += 31;//8月 case 8: days += 31;//7月 case 7: days += 30;//6月 case 6: days += 31;//5月 case 5: days += 30;//4月 case 4: days += 31;//3月 case 3: days += 28;//2月,因为2019年的2月是28天 case 2: days += 31;//1月 } //days 里面存的是这一天是这一年的第几天 //已知2019年1月1日是星期二 //假设我输入的就是1月1日,那么days中就是1 int week = 1;//2018年12月31日的星期 week += days; week %= 7; System.out.print(month+"月" + day +"日是这一年的第"+days+"天,是星期" + (week==0?"天":week)) ; } }
第8题
语法点:运算符,while,if
按步骤编写代码,效果如图所示:
代码实现:
public class Test08 { public static void main(String[] args) { int i = 0; int i2 = 10; while(i<5){ i++; i2++; if(i>=2 && i2<15){ System.out.println("i:" + i + ",i2:" + i2); } } System.out.println("----------------------"); while(i2<20){ i++; i2++; if(i>8 || i2<=18){ System.out.println("i:" + i + ",i2:" + i2); } } } }
第10题
语法点:运算符,for,if
案例需求:从键盘分别输入年、月、日,使用循环for+if实现,判断这一天是当年的第几天
代码实现:
public class Test10 { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("请输入年:"); int year = input.nextInt(); System.out.print("请输入月:"); int month = input.nextInt(); System.out.print("请输入日:"); int day = input.nextInt(); int days = day; for (int i = 1; i < month; i++) { if (i == 4 || i == 6 || i == 9 || i == 11) { days += 30; } else if (i == 2) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { days += 29; } else { days += 28; } } else { days += 31; } } System.out.println(year + "年" + month + "月" + day + "日是这一年的第" + days + "天"); } }
第11题
案例需求:假设从2000年1月1日开始三天打鱼,两天晒网,从键盘输入今天的日期年、月、日,显示今天是打鱼还是晒网?
代码实现:
public class Test11{ public static void main(String[] args){ //1、从键盘分别输入年、月、日 java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("年:"); int year = input.nextInt(); System.out.print("月:"); int month = input.nextInt(); System.out.print("日:"); int day = input.nextInt(); /* (1)先算出,这一天距离2000年1月1日是第几天 ①第month月的day天 ②第year年的[1,month-1]的满月 ③从[2000,year-1]的满年天数 (2)用总天数%5,看余数,余数是1,2,3是打鱼,4和0是晒网 */ int days = day;//①第month月的day天 //②累加[1,month-1]的满月天数 for(int i=1; i<month; i++){//这个i代表月份 if(i==4 || i==6 || i==9 || i==11){ days+=30; }else if(i==2){ if(year%4==0 && year%100!=0 || year%400==0){ days+=29; }else{ days+=28; } }else{ days+=31; } } //③从[2000,year-1]的满年天数 for(int i=2000; i<year; i++){//这个i代表年份 if(i%4==0 && i%100!=0 || i%400==0){ days+=366; }else{ days+=365; } } //判断 if(days%5==1 || days%5==2 || days%5==3){ System.out.println("打鱼"); }else{ System.out.println("晒网"); } } }
第12题
打印倒三角形,效果如图所示:
代码实现:class Test12 { public static void main(String[] args) { for(int i = 5;i >= 1;i--){ for(int j = i;j < 5; j++){ System.out.print(" "); } for(int j = 1;j <= i; j++){ System.out.print("* "); } System.out.println(); } } }
第13题
打印『X』对称图形,效果所示:
*O***O*
**O*O**
***O***
**O*O**
*O***O*
O*****O
代码实现:class Test13 { public static void main(String[] args) { for(int i = 1; i <= 7; i++){ for(int j = 1; j <= 7;j++){ if(i == j || i+j == 8){ System.out.print("O"); }else{ System.out.print("*"); } } System.out.println(); } } }
第14题
打印菱形
或
代码实现:
class Test14 { public static void main(String[] args) { //上半部分 for(int i = 1; i <= 9; i += 2){ for(int j = i; j < 9; j += 2){ System.out.print(" "); } for(int j = 1; j <= i; j++){ System.out.print("* "); } System.out.println(); } //下半部分 for(int i = 7; i > 0; i -= 2){ for(int j = i; j < 9; j += 2){ System.out.print(" "); } for(int j = 1; j <= i; j++){ System.out.print("* "); } System.out.println(); } System.out.println("----------------------"); //空心上半部分 for(int i = 1; i <= 9; i += 2){ for(int j = i; j < 9; j += 2){ System.out.print(" "); } for(int j = 1; j <= i; j++){ if(j == 1 || j == i){ System.out.print("* "); }else{ System.out.print(" "); } } System.out.println(); } //下半部分 for(int i = 7; i > 0; i -= 2){ for(int j = i; j < 9; j += 2){ System.out.print(" "); } for(int j = 1; j <= i; j++){ if(j == 1 || j == i){ System.out.print("* "); }else{ System.out.print(" "); } } System.out.println(); } } }
简述题
第1题
switch是否能作用在byte上,是否能作用在long上,是否能作用在String上?
可以作用在byte上,
不能作用在long上,
可以作用在String上。
第2题
谈谈你对三元运算符、if-else和switch-case结构使用场景的理解?
三元运算符适用于一些做一些简单的判断,代码量不是太多的,可以嵌套但不常用于嵌套。
if-else适用于绝大数场景。
switch-case用于判断比较多的选择,比如季节,星期,天数。
第3题
循环结构是如何最后退出循环的,有哪些不同的情况请说明?
① 循环条件返回false。
② 在循环体内,一旦执行到break,跳出循环。
第4题
说明break和continue使用上的相同点和不同点
相同点:都能退出循环,两者都能使用标签定义位置;
不同点:break直接退出当前循环,continue退出当前这一层循环;
内容总结
以上是互联网集市为您收集整理的Java基本语法(选择、循环结构)全部内容,希望文章能够帮你解决Java基本语法(选择、循环结构)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。