加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java基础复习2

发布时间:2020-12-15 05:35:21 所属栏目:Java 来源:网络整理
导读:三目运算符 语法:条件判断?表达式1:表达式2; 如果条件判断成立则获取值1否则获取值2 public class demo1{? ? public static void main(String[] args){?? ?? ? int a=15,b=25;//定义变量?? ?? ? int c=ab?a:b;//如果ab成立则获取a的值,否则获取b的值?? ??

三目运算符

  1. 语法:条件判断?表达式1:表达式2; 如果条件判断成立则获取值1否则获取值2
public class demo1{
? ? public static void main(String[] args){
?? ?? ? int a=15,b=25;//定义变量
?? ?? ? int c=a>b?a:b;//如果a>b成立则获取a的值,否则获取b的值
?? ?? ? System.out.println(c);
?? ?}
}

练习

public class demo4 {
????public static void main(String[] args) {
????????//通过三木运算符找出a,b,c中最大的数
????????int a = 10;
????????int b = 8;
????????int c = 20;
????????System.out.print(a > b ? (a > c ? a : c) : (b > c ? b : c));
????}
}

控制语句

if else

练习:

public class demo4 {
????public static void main(String[] args) {
????????//输出两个数中的最大值
????????int a = 30;
????????int b = 20;
????????if(a>b) {
???????????System.out.println(a);
????????}else {
???????????System.out.println(b);
????????}
????}
}
import java.util.Scanner;
public class demo4 {
????public static void main(String[] args) {
????????//请输入一个数字,判断这个数字是够可以被7整除且不能被三整除,如果可以把该数输出。
????????Scanner sc = new Scanner(System.in);
????????System.out.print("请输入一个数:");
????????int a = sc.nextInt();
????????if(a%7 == 0 && a%3 != 0) {
???????????<u>System</u>.out.println("您输入的数符合要求");
????????}else {
???????????System.out.println("您输入的数不符合要求");
????????}
????}
}
import java.util.Scanner;
public class demo4 {
????public static void main(String[] args) {
????????//请输入一个成绩,如果成绩大于90,表示优秀,80~90表示良好,60~80表示及格,60以下表示不及格。
????????Scanner sc = new Scanner(System.in);
????????System.out.print("请输入一个成绩:");
????????int a = sc.nextInt();
????????if(a > 100 || a < 0) {
???????????System.out.println("输入了错误的成绩");
????????}else if(a > 90 && a <= 100) {
???????????System.out.println("优秀!");
????????}else if(a >= 80 && a <= 90) {
???????????System.out.println("良好");
????????}else if(a >= 60 && a <= 80){
???????????System.out.println("及格");
????????}else {
???????????System.out.println(" 不及格");
????????}
????}
}
public class demo4 {
????public static void main(String[] args) {
????????//通过代码完成两个数字的交换。比如a=5;b=10 交换后a=10;b=5。(通过中间值)
???????????int a = 15;
???????????int b = 213;
???????????int temp = a;
???????????a = b;
???????????b = temp;
???????????System.out.println("a==="+a);
???????????System.out.println("b==="+b);
????????//不通过中间值
???????????int c = 100;
???????????int d = 200;
???????????c = c + d;
???????????d = c - d;
???????????c = c - d;
???????????System.out.println("c==="+c);
???????????System.out.println("d==="+d);
????}
}
import java.util.Scanner;
public class demo4 {
????public static void main(String[] args) {
????????//甲乙两种出租车有以下定价方式:
????????//????? 起步价?? 3公里以外(每公里)
????????// 甲:10??????????? 2
????????// 乙:8???????????? 2.2
????????//输入公里数,显示哪种方案更便宜。
????????Scanner sc = new Scanner(System.in);
????????System.out.print("请输入公里数:");
????????double a = sc.nextDouble();
????????if(a * 2 + 10 <= a * 2.2 + 8) {
???????????System.out.println("甲方案更便宜");
????????}else if(a < 0) {
???????????System.out.println("输入错误");
????????}else {
???????????System.out.println("乙方案更便宜");
????????}
????}
}
import java.util.Scanner;
public class demo4 {
????public static void main(String[] args) {
????????//编程判断输入的正整数是否既是5又是7的整倍数。若是,则输出yes;否则输出no。
????????Scanner sc = new Scanner(System.in);
????????System.out.print("请输入一个正整数:");
????????int a = sc.nextInt();
????????if(a % 7 == 0 && a % 5 == 0) {
???????????System.out.println("yes");
????????}else {
???????????System.out.println("No");
????????}
????}
}
import java.util.Scanner;
public class demo4 {
????public static void main(String[] args) {
????????//输入一个三位数,判断该三位数是否为水仙花数。所谓水仙花表示:各位数字立方和等于这个数。
????????//例如。153 是水仙花数。1的立方+5的立方+3的立方等于153
????????Scanner sc = new Scanner(System.in);
????????System.out.print("请输入一个三位数:");
????????int a = sc.nextInt();
????????if((a/100%10)*(a/100%10)*(a/100%10)+(a/10%10)*(a/10%10)*(a/10%10)+(a%10)*(a%10)*(a%10) == a) {
???????????System.out.println(a+"是水仙花数。");
????????}else {
???????????System.out.println(a+"不是水仙花数。");
????????}
????}
}
import java.util.Scanner;
public class demo5 {
????public static void main(String[] args) {
//??????//有一个函数:
//??????y=????? x???????? x<1
//??????y=??? 2x-1??????? 1≤x<10
//??????y=??? 3x-11?????? x≥10
//?????????????写一段程序,输入x,可求出y的值并输出。
????????Scanner sc = new Scanner(System.in);
????????System.out.println("请输入x的值:");
????????int x = sc.nextInt();
????????int y = 0;
????????if(x<1) {
???????????y = x;
????????}else if(1<=x && x<10) {
???????????y = 2*x-1;
????????}else {
???????????y = 3*x-11;
????????}
????????System.out.println("y的值为:"+y);
????}
}
if的嵌套。
import java.util.Scanner;
public class demo6 {
????public static void main(String[] args) {
????????//输入一个年份和一个月份,判断这个月有多少天
????????Scanner sc = new Scanner(System.in);
????????System.out.print("请输入年份:");
????????int a = sc.nextInt();
????????System.out.print("请输入月份:");
????????int b = sc.nextInt();
????????if(b==1||b==3||b==5||b==7||b==8||b==10||b==12) {
???????????System.out.println("该月有31天");
????????}else if(b==2) {
???????????if(a%4==0&&a%100!=0||a%400==0) {
???????????????System.out.<u>println</u>("该月有29天");
???????????}else {
???????????????System.out.println("该月有28天");
???????????}
????????}else {
???????????System.out.println("该月有30天");
????????}
????}
}
switch语句

switch(表达式){//该表达式的返回类型必须是int,short,byte,char,String
case 值1:{语句块};break;
case 值2:{语句块};break;
case 值3:{语句块};break;
...
default:语句块;break;
}
case的值不能重复,case没有固定的顺序
break:结束switch语句,如果没有使用break,则会找到匹配的case后,接着执行该case下所有的语句
if else if else 和 switch 的区别
switch适合单值的
if else if else 可以使用某个范围的

import java.util.Scanner;
public class demo7 {
????public static void main(String[] args) {
????????//输入1到7数字,显示对应星期几。
????????Scanner sc = new Scanner(System.in);
????????System.out.print("请输入一个数字:");
????????int a = sc.nextInt();
????????switch(a) {
????????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;
????????default:System.out.println("输入有误");break;
????????}
????}
}

循环语句

for循环。
???????for(初始值;条件判断;变化){
????????????语句块;
???????}
????while循环。
???????while(条件判断){
???????????语句块;
???????}
???????do{
??????????语句块;
???????}while(条件判断);

???????至少执行一次。
for循环:

public class demo8 {
????public static void main(String[] args) {
????????//求1~100的和
????????int a = 0;
????????for(int i=1;i<=100;i++) {
???????????a +=i;
????????}
????????System.out.println(a);
????}
}

while循环

public class demo8 {
????public static void main(String[] args) {
????????//求1~100的和
????????int a = 0;
????????int i = 0;
????????while(i<=100) {
???????????a += i;
???????????i++;
????????}
????????System.out.println(a);
????}
}

do while 循环

do 语句块必被执行一次
如下,虽然a!<0,但是还是执行了一次

public class demo8 {
????public static void main(String[] args) {
????????int a = 0;
????????do {
???????????System.out.println("aaaaa");
????????}while(a<0);
????}
}

break?和 continue:

public class demo8 {
????public static void main(String[] args) {
????????for(int i=1;i<=10;i++) {
???????????if(i==3) {
???????????????System.out.println("退出循环");
???????????????break;//break退出循环
???????????}
???????????System.out.println(i);
????????}
????}
}
public class demo8 {
????public static void main(String[] args) {
????????for(int i=1;i<=10;i++) {
???????????if(i==3) {
???????????????System.out.println("不显示3");
???????????????continue;//结束的是本次循环,后面的输出语句不会执行
???????????}
???????????System.out.println(i);
????????}
????}
}

equals:

public class demo8 {
????public static void main(String[] args) {
????????Scanner <u>sc</u> = new Scanner(System.in);
????????while(true) {
???????????System.out.println("请输入一个数字");
???????????int a = sc.nextInt();
???????????System.out.println("你输入的数字为:"+a);
???????????System.out.println("是否继续?y/n");
???????????String b = sc.next();
???????????if(b.equals("n")) {
???????????????break;//结束while循环
???????????}
????????}
????}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读