练习1:判断输入的值是否是偶数,另外,要处理输入错误
(目的:熟悉输入、输出,特别是Scanner对象的方法)
import java.util.InputMismatchException; import java.util.Scanner; public class test01 { public static void main(String[] args) { /* * 1、加一层循环判断,就不用执行多次 * 2、使用scanner对象的方法进行输入输出使用 * 3、使用try-catch方法用于异常判断,中间用if\else-if判断是否偶数 * */ while (true){ Scanner sc = new Scanner(System.in); System.out.print("请输入数字:"); try { int n=sc.nextInt(); if (n==0){ System.out.println(n+"不是偶数"); } else if (n%2==0) { System.out.println(n+"是偶数"); } else { System.out.println(n+"不是偶数"); } }catch (InputMismatchException e){ System.out.println("你输入的不是数字"); } } } }
练习2:输入并输出姓名、年龄、身高
import java.util.Scanner; public class Test02 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入用户名:"); String name=sc.next(); System.out.print("请输入身高:"); double height = sc.nextDouble(); System.out.print("请输入年龄:"); int age = sc.nextInt(); System.out.println("用户名:"+name+",身高:"+height+",年龄:"+age); } }
练习3:从控制台获取Java、Oracle、HTML三门课程的成绩,计算总分和平均分(平均分保留2位小数,要求四舍五入;按如下格式输出)
import java.util.Scanner; public class Test03 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Java成绩:"); int java= sc.nextInt(); System.out.print("Oracle成绩:"); int oracle= sc.nextInt(); System.out.print("Html成绩:"); int html= sc.nextInt(); System.out.println("---------------------"); System.out.println("Java Oracle Html"); System.out.println(""+java+'\t'+oracle+'\t'+html); System.out.println("---------------------"); System.out.println(); int c=java+oracle+html; System.out.println("总分:"+c); System.out.println("平均分:"+(c/3)); } }
练习4:输入一个数字,反转输出,如:输入123,输出321
import java.util.Scanner; public class Test05 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入一个数:"); String num= sc.next(); for (int i = num.length()-1; i >=0; i--) { System.out.print(num.charAt(i)); } } }
练习5:输入tom和jack的年龄,比较年龄并输出相差多少岁,要求使用条件运算符
import java.util.Scanner; public class Test04 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入tom的年龄:"); int tom=sc.nextInt(); System.out.print("请输入jack的年龄:"); int jack=sc.nextInt(); if (tom>jack){ System.out.println("tom比jack大"+(tom-jack)+"岁"); if (tom>18){ System.out.println("tom是成年人"); } if (tom<70){ System.out.println("tom未到退休年龄"); }else { System.out.println("tom是未成年人"); } }else if (tom==jack){ System.out.println("tom和jack同岁"); }else { System.out.println("tom比jack小" + (jack - tom) + "岁"); } } }
练习6:交换两个数值变量的值(至少两种方法)
public class Test06 { public static void main(String[] args) { //定义临时变量 int v1=1,v2=2; int temp; temp=v1; v1=v2; v2=temp; System.out.println("v1="+v1+",v2="+v2); //运算符 int v3=1,v4=2; v3=v3+v4; v4=v3-v4; v3=v3-v4; System.out.println("v3="+v3+",v4="+v4); //位运算法 int v5=1,v6=2; v5=v5^v6; v6=v5^v6; v5=v5^v6; System.out.println("v5="+v5+",v6="+v6); } }
练习7:下面结果分别是?说明原因
int a = 97; char b = a; System.out.println(b); java: 不兼容的类型: 从int转换到char可能会有损失 char c = 97; System.out.println(c); 结果:a,a的ASCII码是97 int m = 3; int n = m++ + ++m; System.out.println("m:"+m+", n:"+n); m:5, n:8 n=3+5 后面的m经过++m后:m=3+1+1+3,++的意思自增1
练习8:下面结果分别是?说明原因
public class Test { public static void main(String[] args) { short a=1; a = a+1; System.out.println(a); 结果:java: 不兼容的类型: 从int转换到short可能会有损失 short b=1; b+=1; System.out.println(b); 结果:b=2,b+=1===》b=b+1 } }
java基础练习:选择结构(if、switch)
if:if...else...及if嵌套
练习1:提示用户输入三个整数,判断并输出最大值、最小值
import java.util.Scanner; public class lx { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入第1个数:"); int a= sc.nextInt(); System.out.print("请输入第2个数:"); int b= sc.nextInt(); System.out.print("请输入第3个数:"); int c= sc.nextInt(); //a>b>c a>b<c if (a>=b){ if (a>=c){ if (b<=c) { System.out.println("最大值:"+a); System.out.println("最小值:"+b); }else { System.out.println("最大值:"+a); System.out.println("最小值:"+c); } } else { System.out.println("最大值:"+c); System.out.println("最小值:"+b); } } else { if (b>=c){ if (a<=c){ System.out.println("最大值:"+b); System.out.println("最小值:"+a); }else { System.out.println("最大值:"+b); System.out.println("最小值:"+c); } }else { System.out.println("最大值:"+c); System.out.println("最小值:"+a); } } } }
如果今天是周日且晴天,就出去嗨
如果今天是周日但不是晴天,睡懒觉
如果今天不是周日,继续996
说明:天气、星期几的变量,自己定义即可;主要练习if嵌套
import java.util.Scanner; public class Test04 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("今天星期几:"); int today= sc.nextInt(); switch (today){ 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; default: System.out.println("休息"); } } }
练习3:由键盘输入三个整数分别存入变量num1、num2、num3,对它们进行排序(使用 if-else if-else),并且从小到大输出。
import java.util.Scanner; public class lx { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("num1:"); int num1= sc.nextInt(); System.out.print("num2:"); int num2= sc.nextInt(); System.out.print("num3:"); int num3= sc.nextInt(); if (num1>=num2){ if (num1>=num3){ if (num2<=num3) { System.out.println(num1+","+num3+","+num2); }else { System.out.println(num1+","+num2+","+num3); } } else { System.out.println(num3+","+num1+","+num2); } } else { if (num2>=num3){ if (num1<=num3){ System.out.println(num2+","+num3+","+num1); }else { System.out.println(num2+","+num1+","+num3); } }else { System.out.println(num3+","+num2+","+num1); } } } }
练习4:提示用户输入三角形的边a、b、c,范围必须在 [1,100) 之间,判断三角形的类型:1.非三角形;2.等边三角形;3.直角三角形;4.等腰三角形;5.普通三角形
标签:java,Scanner,int,练习,基础,System,sc,println,out From: https://www.cnblogs.com/sasa-python/p/16782941.html