1.
①
package com.company; public class HomeWork8_19 { public static void main(String[] args) { String name = "小明"; int age = 25; int seniority = 3; int age1 = 5; String subject = "java"; String hobby = "篮球"; System.out.println("这个同学的姓名是:" + name); System.out.println("年龄是:" + age); System.out.println("工作了" + seniority + "年"); System.out.println("做过" + age1 + "个项目"); System.out.println("技术方向是" + subject); System.out.println("兴趣爱好是:" + hobby); } }
②
package com.company; import java.util.Scanner; public class HomeWork8_19 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入4位会员卡号:"); int card = scanner.nextInt(); System.out.print("会员卡号是:" + card); scanner.close(); } }
③
package com.company; import java.util.Scanner; public class HomeWork8_19 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入一个四位卡号:"); Integer idCard = scanner.nextInt(); int ge,shi,bai,qian,sum; ge = idCard % 10; shi = idCard / 10 % 10; bai = idCard /100 % 10; qian = idCard / 1000; sum = ge+shi+bai+qian; System.out.println("个位数为:"+ge); System.out.println("十位数为:"+shi); System.out.println("百位数为:"+bai); System.out.println("千位数为:"+qian); System.out.println("各位数字的和为:"+sum); scanner.close(); } }
④
package com.company; import java.util.Scanner; public class HomeWork8_19 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入一个四位卡号:"); Integer idCard = scanner.nextInt(); int ge,shi,bai,qian,sum; ge = idCard % 10; shi = idCard / 10 % 10; bai = idCard /100 % 10; qian = idCard / 1000; sum = ge+shi+bai+qian; System.out.println("个位数为:"+ge); System.out.println("十位数为:"+shi); System.out.println("百位数为:"+bai); System.out.println("千位数为:"+qian); System.out.println("各位数字的和为:"+sum); System.out.println(sum > 20 ? "中奖了" : "没中奖"); scanner.close(); } }
2、编程实现自我介绍,通过键盘输入姓名[name],年龄[age],工作年限[years],技术方向[skill],兴趣爱好[hobby]。
package com.company; import java.util.Scanner; public class HomeWork8_19 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入您的姓名:"); String name = scanner.next(); System.out.print("请输入您的年龄: "); int age = scanner.nextInt(); System.out.print("请输入工作年限:"); int years = scanner.nextInt(); System.out.print("请输入您的技术方向:"); String skill = scanner.next(); System.out.print("请输入您的兴趣爱好:"); String hobby = scanner.next(); System.out.println("您的姓名是:"+ name +",年龄是:"+age +",工作年限是" + years +",技术方向是" + skill + ",兴趣爱好是" +hobby); scanner.close(); } }
3、编程实现三个数字[a,b,c]的交换,即1,2,3交换后变成2,3,1。
package com.company; public class HomeWork8_19 { public static void main(String[] args) { int a,b,c,d; a = 1; b = 2; c = 3; System.out.println("a的值" + a + ",b的值" + b + ",c的值" + c); d = a; a = b; b = c; c = d; System.out.println("a的值" + a + ",b的值" + b + ",c的值" + c); } }
4、通过键盘输入一个整数[x]求其相反数[opposite],平方[square],立方[cube]。
package com.company; import java.util.Scanner; public class HomeWork8_19 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入一个整数:"); int x = scanner.nextInt(); int opposite = -x; int square = x * x; int cube = x * x * x; System.out.println(x + "的相反数是:" + opposite + ",平方是:" + square + ",立方是:" + cube); scanner.close(); } }
5、通过键盘输入两个整数[x,y]求它们的和[sum]、差[difference]、积[product]、商[quotient]、模[remainder]。
package com.company; import java.util.Scanner; public class HomeWork8_19 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入整数x,y:"); int x = scanner.nextInt(); int y = scanner.nextInt(); int sum = x + y; int difference = x - y; int product = x * y; double quotient = (double) x / (double) y; int remainder = x % y; System.out.println(x +"和"+ y + "的和是:" + sum + ",差是:" + difference + ",积是:" + product + ",商是:" + quotient + ",模是:" + ",商是:" + quotient); scanner.close(); } }
标签:java,scanner,int,练习,数据类型,System,Scanner,println,out From: https://www.cnblogs.com/hsk991213/p/17658070.html