package com.svse;
/**
* (运算符):编写一个Java程序,
* 定义2个变量,分别输入2个整数,
* 然后交换2个整数的值(分别使用第3个变量跟不使用第3个变量)。
* 例如int a=3 ;int b=2 ; 最后的结果变成a=2;b=3 ;
* */
public class Demo01 {
public static void main(String[] args) {
int a = 3;
int b = 2;
int c = a;
a=b;
b=c;
System.out.println(a);
System.out.println(b);
}
}
/**
* 输入一个三位整数(首末位不包含0),要求反向输出。比如输入123,输出321.
* */
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
System.out.println("请输入一个三位整数(首末位不包含0):");
Scanner input = new Scanner(System.in);
int number = input.nextInt();
//百位跟个位调换 321--》123
int a = number/100;
int b = number/10%10;
int c = number%10;
System.out.println(c*100+b*10+a);
}
}
/**
* 定义一个数,判断该数是不是3的倍数,并将结果输出。
* */
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
System.out.println("请输入一个数:");
Scanner input = new Scanner(System.in);
int number = input.nextInt();
System.out.println(number%3==0?number:"输入的数字不是3的倍数");
}
}
import java.util.Scanner;
/**
* 判断用户输入的年份是不是闰年,
* 接受用户输入的年份后,
* 首先要判断数字是不是正确的年份,
* 年份是四位数,
* 闰年的条件是符合下面二者之一:
* ①能被4整除,但不能被100整除;
* ②能被400整除。
* */
public class Demo04 {
public static void main(String[] args) {
System.out.println("请输入年份:");
Scanner input = new Scanner (System.in);
int year = input.nextInt();
if(year<1000||year>9999){
System.out.println("年份输入错误...");
}else if(year%400==0){
System.out.println("输入的年份是闰年");
}else if(year%4==0&&year%100!=0){
System.out.println("输入的年份是闰年");
}else{
System.out.println("输入的年份不是闰年");
}
}
}
import java.util.Scanner;
/**
* 接受用户输入的四个数字a,b,c,d,将四个数中的最大值求出来。 ab比较,大的放n cd比较,大的放m 再比较nm可知最大值
*/
public class Demo05 {
public static void main(String[] args) {
// 6、(条件分支):接受用户输入的四个数字a,b,c,d,将四个数中的最大值求出来。
System.out.println("请输入四个数字:");
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
int d = input.nextInt();
// 判断
if (a > b && a > c && a > d) {
System.out.println("最大的数字为:" + a);
} else if (b > a && b > c && b > d) {
System.out.println("最大的数字为:" + b);
} else if (c > a && c > b && c > d) {
System.out.println("最大的数字为:" + c);
} else {
System.out.println("最大的数字为:" + d);
}
}
import java.util.Scanner;
/**
* 计算出租车的计费。
* 车费包括:公里数+等候时间
* 公里数的钱:前3公里10元,4-15公里每公里2元,15公里以上每公里3元。
* 等候时间就是2分半(150秒)收1元,不足的部分不收费。
* 要求输入公里数和等候秒数,输出车费。比如:输入16公里,299秒。
* 车费 = 10 + (15-3)*2 + (16-15)* 3 + 1
* */
public class Demo07 {
public static void main(String[] args) {
System.out.println("请输入公里数:");
Scanner input = new Scanner (System.in);
int number = input.nextInt();
// 等候时间就是2分半(150秒)收1元,不足的部分不收费。150 300 450 ...
System.out.println("请输入等候秒数:");
int second = input.nextInt();
// 前3公里10元 4-15公里每公里2元 15公里以上每公里3元
if(number<=3){
System.out.println("车费:"+(10+(second/150)*1));//前3公里10元
}else if(number>3&&number<=15){
System.out.println("车费:"+(10+(number-3)*2+(second/150)*1));//4-15公里每公里2元
}else{
System.out.println("车费:"+(34+(number-15)*3+(second/150)*1));//15公里以上每公里3元
}
}
}
import java.util.Scanner;
/**
* 某超市进行促销活动, 规则如下: 如果是本超市的会员,购买的商品总价在100元以上,八折优惠; 如果是会员但商品总价在100元以下9折优惠;
* 如果非会员商品总价在100以上九折优惠; 如果非会员商品总价在100以下,不打折;
* 询问用户是否是会员(Y和N代表),和商品的总价,根据答案判断折扣额以.最终要付款的金额.
*/
public class Demo08 {
public static void main(String[] args) {
System.out.println("请问是否为会员?(Y代表是,N代表不是。)");
Scanner input = new Scanner(System.in);
String number = input.next();
char number1 = number.charAt(0);
System.out.println("请输入总价:");
double money = input.nextDouble();
if (number1 == 'Y') {
if (money >= 100) {
System.out.println("金额为:" + (money * 0.8));
} else {
System.out.println("金额为:" + (money * 0.9));
}
}
if (number1 == 'N') {
if (money >= 100) {
System.out.println("金额为:" + (money * 0.9));
} else {
System.out.println("金额为:" + money);
}
}
}
}
package com.svse;
import java.util.Scanner;
/**
* 某公司即将为员工买保险, 要求如下: 已婚的男、女性都可以买保险, 未婚男性25岁以下不提供保险,25岁以上(包括)提供保险,
* 未婚女性22岁以下不提供保险,22岁以上(包括)提供保险, 请用程序实现以上功能
* 接受员工的信息,显示公司是否为他提供保险(已未婚用Y和N表示,性别用男和女表示) 代码提示: 接受char类型数据char sex =
* input.next().charAt(0);
*/
public class Demo09 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请问是否已婚?(Y代表是,N代表不是。)");
char marry = input.next().charAt(0);// 结婚
System.out.println("请输入性别(男和女表示):");
char sex = input.next().charAt(0);// 男女
System.out.println("请输入年龄(数字如:23):");
int age = input.nextInt();// 年龄
if (marry == 'Y') {
System.out.println("提供保险");
}
else if (marry == 'N') {
if (age < 25 && sex == '男') {
System.out.println("不提供保险");
} else if(age <=25 && sex == '男'){
System.out.println("提供保险");
}
if (age < 22 && sex == '女') {
System.out.println("不提供保险");
} else if(age >= 22 && sex == '女'){
System.out.println("提供保险");
}
}
}
}
package com.svse;
import java.util.Scanner;
/**
* 用代码实现以下的本周食谱:
* 周一:鸡蛋
* 周二:鱼
* 周三:青菜
* 周四:牛肉
* 周五:虾
* 周六:汉堡
* 周天:鸡翅,
* 先询问用户今天是星期几,接受用户的答案后,判断输出今天的食谱
* */
public class Demo10 {
public static void main(String[] args) {
System.out.println("今天是星期几?(回答数字:1代表星期一,2代表星期二...)");
Scanner input = new Scanner(System.in);
int number = input.nextInt();
switch (number) {
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;
default:
System.out.println("今天吃鸡翅");
break;
}
}
}
package com.svse;
import java.util.Scanner;
/**
* 让用户输入矩形的两个边长(int数据类型),
* 用条件表达式判断:
* 如果两个边长相等,则为正方形,不等为长方形,最后并将其面积输出。
* 例如:用户输入的长和宽是:4和5,则输出结果是:该图形是长方形,面积是20
* */
public class Demo11 {
public static void main(String[] args) {
System.out.println("请输入矩形的两个边长:(整数类型)");
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
System.out.println(a==b?"该图形是正方形 ,面积是:"+(a*b):"该图形是长方形 ,面积是:"+(a*b));
// if(a==b){
// System.out.println("该图形是正方形 ,面积是:"+(a*b));
// }else{
// System.out.println("该图形是长方形 ,面积是:"+(a*b));
// }
}
}
package com.svse;
/**
* 商场为员工提供了基本工资(3000元)、
* 物价津贴
* 及房租津贴,
* 其中物价津贴为基本工资的40%,房租津贴为基本工资的25%。
* 编程计算实领工资
* */
public class Demo12 {
public static void main(String[] args) {
double salary = 3000.0;
double good = salary *0.4;
double home = salary *0.25;
System.out.println("实领工资:"+(salary+good+home));
}
}
package com.svse;
import java.util.Scanner;
/**
* 要求用户输入一个四位数,如果用户输入不是四位数,将提示错误信息,
* 如果是四位数,
* 将这个四位数的个,十,百,千位的数字单独输出来,并将他们的和求出来!
* 例:用户输入2045,输出结果为:个位:5,十位:4,百位:0,千位:2,和为:11
* */
public class Demo13 {
public static void main(String[] args) {
System.out.println("请输入一个四位数:");
Scanner input = new Scanner(System.in);
int number = input.nextInt();
if(number<1000||number>9999){
System.out.println("输入的不是四位数...");
}else{
int a =number/1000;//千
int b =number/100%10;//百
int c =number/10%10;//十
int d =number%10;//个
System.out.print("个位:"+d+",\t");
System.out.print("十位:"+c+",\t");
System.out.print("百位:"+b+",\t");
System.out.print("千位:"+a+",\t");
System.out.print("和为:"+(a+b+c+d));
}
}
}
package com.svse;
import java.util.Scanner;
/**
* 输入3个数,按大小输出这3个数(例如:用户输入的是:10 5 20 ,排序后为:5 10 20)
* */
public class Demo14 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入第一个整数");
int a = input.nextInt();
System.out.println("请输入第二个整数");
int b = input.nextInt();
System.out.println("请输入第三个整数");
int c = input.nextInt();
int i = 0;
while(true){
if(a>b){
i=b;
b=a;
a=i;
}
if(b>c){
i=c;
c=b;
b=i;
}
if(a<b&&b<c){
break;
}
}
System.out.println(a+"<"+b+"<"+c);
}
}
package com.svse;
/**
* (循环):用2种思路打印1-100的所有奇数。
*
* */
public class Demo01 {
public static void main(String[] args) {
//第一种
// for(int i =1;i<=100;i++){
// if(i%2==1){
// System.out.println(i);
// }
// }
//第二种
int i = 1;
while(i<100){
if(i%2==1){
System.out.println(i);
}
i++;
}
}
}
package com.svse;
/**
* (循环):用程序分别计算1-50的总和1-10的乘积,并输出。
* */
public class Demo02 {
public static void main(String[] args) {
//计算1-50的总和
int sum=0;
for(int i=1;i<=50;i++){
sum+=i;
}
System.out.println("1-50的总和:"+sum+"\n");
//计算1-10的乘积(50太大了)
int ji = 1;
for(int i=1;i<=10;i++){
ji *= i;
}
System.out.println("1-10的乘积:"+ji);
}
}
package com.svse;
/**
* 求出所有的水仙花数,水仙化数为3位数,并且每位上的数字的立方和等于该数本身。
*
* 什么叫做水仙花数?
*
* 水仙花数就是一个三位数它的各个数的立方和等于该数本身。
*
* 就好比说153他就是一个水仙花数,
*
* 1的立方加上5的立方再加上3的立方就等于153。
*
* 1*1*1+5*5*5+3*3*3=153 ---> 1+125+27=153
* */
public class Demo03 {
public static void main(String[] args) {
int a,a1,a2,a3;
for(a=100;a<1000;a++){
a1=a%10;//个
a2=a/10%10;//十
a3=a/100;//百
//判断
if(a==a1*a1*a1+a2*a2*a2+a3*a3*a3){
System.out.println(a);
}
}
}
}
package com.svse;
/**
* (逢7过)从1到200遍历,凡是遇到任何7的倍数,如14、21或含7的数字如17、27均以直接跳过。
*/
public class Demo04 {
public static void main(String[] args) {
//从1到200遍历
for (int i = 1; i <= 200; i++) {
// 遇到任何7的倍数,如14、21直接跳过
if (i % 7 == 0) {
continue;
}
//含7的数字如17、27均以直接跳过。
int a=i%10;//个
int b=i/10%10;//十
if(a==7||b==7){
continue;
}
System.out.println(i);
}
}
}
package com.svse;
import java.util.Scanner;
/**
* (非常6+1)
* 定义一个200以内的整数(比如85)。
* 接收用户控制台输入的整数,
* 当比这个数(85)大的时候就提示“大了”,
* 反之比这个数(85)小的时候就提示“小了”。
* 反复循环,
* 如果猜中了就提示“恭喜您中奖了!”
*/
public class Demo05 {
public static void main(String[] args) {
// 直接给定一个数:85
int number = 85;
Scanner input = new Scanner(System.in);
// 循环次数不确定
while (true) {
System.out.println("请输入一个整数:");
int price = input.nextInt();
// 写循环
if (price > number) {
System.out.println("大了");
} else if (price < number) {
System.out.println("小了");
} else {
System.out.println("恭喜您中奖了!");
break;// 记得break!!!!!
}
}
input.close();// 要放到循环体外!!!
}
}
package com.svse;
/**
* 有36个人,有36个砖,
* 男人每次搬4块,
* 女人每次搬3块,
* 小孩每2人搬1块,
* !!!每个人都正好一次搬完。!!! 【36个人 每一个人都要搬一次 小孩子两个人搬一次 小孩子居多】
* 问几男几女几小孩?
* (不能使用数学公式计算)
* */
public class Demo06 {
public static void main(String[] args) {
//男人三个 12块 女人三个 9块 小孩子30个 15块 ————> 36人 36块
//m 男人 w女人 36-m-n 小孩子
for(int m=1;m<=9;m++){//m最大是9 36/4=9
for(int w=1;w<=12;w++){// w最大12
double x =m*4+w*3+0.5*(36-m-w);
if(x==36){
System.out.println("需要"+m+"个男人");
System.out.println("需要"+w+"个女人");
System.out.println("需要"+(36-m-w)+"个小孩子");
}
}
}
}
}
package com.svse;
/**
* 打印2-200的所有的质素。(只能被1和它自身整除的数就是素数。)
*/
public class Demo07 {
public static void main(String[] args) {
// int i, j = 0;
// for (j = 2; j <= 200; j++) {
// i = 2;
// while (i < j) {
// if (j % i == 0) {
// break;
// }
// i++;
// if (i == j) {
// System.out.println(j);
// }
// }
// }
//假设这个数是质数为真的思想
for(int b=2;b<=200;b++){
//假设这个数是质数
boolean flag =true;
for(int a=2;a<b;a++){
if(b%a==0){
flag=false;
break;
}
}
if(flag){
System.out.println(b+"是质数");
}else{
System.out.println(b+"不是质数");
}
}
}
}
【!!!!!身份证乱写的!!!!】如有巧合!对不起 会删除!!!
package com.svse;
/**
* 生成身份证号的校验位:第18位是校验位,是自动生成的。
*
* 生成规则是前17位数字分别乘以加权因子 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 ,并求和。
*
* 然后这个和对11取余数。结果是0-10,
*
* 然后分别对应的校验码应该是:1 0 X 9 8 7 6 5 4 3 2
* */
public class Demo08 {
public static void main(String[] args) {
//验证身份证:420901 + 19800101 + 123 + 3?【!!!!!身份证乱写的!!!!】
int [] yin={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};//17位 测试第18位
int [] id={4,2,0,9,0,1,1,9,8,0,0,1,0,1,1,2,3};
String [] code={"1","0","X","9","8","7","6","5","4","3","2"};
int number = 0;//和
for(int i=0;i<yin.length;i++){
number = number+(yin[i]*id[i]);//
}
int number1 = number%11;//和取余
System.out.println(code[number1]);
}
}
package com.svse;
/**
* (循环)将1998-2008年之间的闰年年份输出
* 闰年的条件是符合下面二者之一:
* ①能被4整除,但不能被100整除;
* ②能被400整除。
* */
public class Demo09 {
public static void main(String[] args) {
//循环次数确实,不确定再用while循环。
for(int year=1998;year<2009;year++){
//①能被4整除,但不能被100整除;
if(year%4==0&&year%100!=0){
System.out.println(year);
}else if(year%400==0){//②能被400整除。
System.out.println(year);
}
}
}
}
package com.svse;
/**
* (循环)猴子吃桃问题: 猴子第一天摘下若干个桃子,x 当即吃了一半,还不瘾,又多吃了一个 吃了:x/2 +1 剩下: x-(x/2+1)
* 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。 (x-(x/2+1))/2 +1 以后每天早上都吃了前一天剩下的一半零一个。
* 到第10天早上想再吃时,见只剩下一个桃子了。1 求第一天共摘了多少。
*
* 倒着想 第十天 一个桃子 之前每天吃一半多一个 (2*x)+1
* 第一天 1个桃子 第二天吃(2*x)+1个
*/
public class Demo10 {
public static void main(String[] args) {
int peach=1;// 桃子的个数
for(int i=1;i<=9;i++){//i天数
peach=(peach+1)*2;
}
System.out.println(peach);
}
}
package com.svse;
import java.util.Scanner;
/**
* (数组):从键盘接受10个整数,求出其中的最大值和最小值。
*/
public class Demo11 {
public static void main(String[] args) {
//键盘接收10个数
Scanner input = new Scanner(System.in);
int[] number = new int[10];
for (int i = 0; i < 10; i++) {
System.out.println("请输入一个整数:");
number[i] = input.nextInt();
}
//大
int max = number[0];
for (int i = 0; i < number.length; i++) {
if (number[i] > max) {
max = number[i];
}
}
System.out.println("最大值是:" + max);
//小
int min = number[0];
for (int i = 0; i < number.length; i++) {
if (number[i] < min) {
min = number[i];
}
}
System.out.println("最小值是:" + min);
}
}
待续。。。。。。。。。。。