Java 学习(04)--运算符/循环常见问题
1:输入年份和月份,输出是平年还是闰年,输出该月份的天数
import java.util.Scanner;
public class runnian{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("请输入年份");
int year = s.nextInt();
System.out.println("请输入月份");
int month = s.nextInt();
// flag:0 --平年 flag:1--闰年
int flag = 0 ;
if((year%4==0 && year%100!=0) || year%400==0){
//闰年条件:1.能整除4且不能整除100 2.能整除400
flag = 1;
System.out.println("该年是闰年");
}else{
System.out.println("该年是平年");
}
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println("31天");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println("30天");
break;
case 2:
if (flag == 0){
System.out.println("28天");
}else{
System.out.println("29天");
}
break;
default:
System.out.println("您输入月份有误");
}
}
}
运行结果:
2:键盘录入一个数字(1-7),输出对应的星期几。
import java.util.Scanner;
public class task2{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("请输入数字");
int num = s.nextInt();
switch(num){
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("您输入月份有误");
}
}
}
运行:
3:输入性别、身高、体重,判断身材(规则自己定义)
import java.util.Scanner;
public class task3 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("请输性别:(女--0 男--1)");
int sex = s.nextInt();
System.out.print("请输身高(厘米):");
int height = s.nextInt();
System.out.print("请输体重(千克):");
int weight = s.nextInt();
int sta = height - 110;
int dis = weight - sta;
if (sex == 0){
if (dis > 5) {
System.out.println("老姐:你有点胖,管住嘴啊");
} else if (dis < -5) {
System.out.println("美女:你有点瘦,多吃点啊");
} else {
System.out.println("小姐姐:你的身材太棒啦,美美哒");
}
} else if (sex == 1){
if (dis > 5) {
System.out.println("大哥:你有点胖,管住嘴啊");
} else if (dis < -5) {
System.out.println("兄弟:你有点瘦,多吃点啊");
} else {
System.out.println("小哥哥:你的身材太棒啦,帅帅哒");
}
} else{
System.out.println("你是外星人哈~~~性别选错啦");
}
}
}
运行:
4:输入一个整数,如果此数为0,则输出”石头”,如果此数为1,则输出”剪刀”,如果此数为2,则输出”布”,如果为其它,则输出”错误”
import java.util.Scanner;
public class task4{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("请输入数字");
int num = s.nextInt();
switch(num){
case 0:
System.out.println("石头");
break;
case 1:
System.out.println("剪刀");
break;
case 2:
System.out.println("布");
break;
default:
System.out.println("错误");
}
}
}
运行:
5:输入一个int型的数据,判断这个数是否能被2整除,如果能被2整除,那么输出“这个数是偶数”
import java.util.Scanner;
public class task5{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("请输入数字");
int num = s.nextInt();
if (num%2 == 0){
System.out.println("这个数是偶数");
}else{
System.out.println("这个数是奇数");
}
}
}
运行:
6:输入三个int型的数据,放入到a,b,c三个变量中去,使用条件结构与交换逻辑将这三个变量中的值从小到大排列。
import java.util.Scanner;
public class task6 {
static int a,b,c,max,middle,min;
public static void main(String[] args) {
System.out.println("输入三个整数:");
Scanner s = new Scanner(System.in);
a = s.nextInt();
b = s.nextInt();
c = s.nextInt();
max = a;
if(b>a&&b>c){
max = b;
}
if(c>a&&c>b){
max = c;
}
min = a;
if(b<a&&b<c){
min = b;
}
if(c<a&&c<b){
min = c;
}
middle = (a+b+c)-(max+min);
System.out.println(min+","+middle+","+max);
}
}
运行:
7: 输入一个三位整数,判其是不是降序数如:531是降序数 百位>十位>个位
import java.util.Scanner;
public class task7 {
static int number;
public static void main(String[] args) {
int bai,shi,ge;
System.out.println("输入一个三位数:");
Scanner s = new Scanner(System.in);
number = s.nextInt();
bai = number/100;
shi = (number%100)/10;
ge = (number%100)%10;
//得到百、十、个位
if(bai>shi&&shi>ge){
System.out.println(number+"是降序数");
}else{
System.out.println(number+"不是降序数");
}
}
}
运行:
8.求1-100之间偶数和
import java.util.Scanner;
public class task4{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入开始的数字");
int start = sc.nextInt();
System.out.println("请输入结束的数字");
int end = sc.nextInt();
int sum = 0;
//判断开始是否为偶数
for(; start%2 == 1; ){
start += 1;
}
for(int i = start;i <= end;i+=2){
sum += i;
}
System.out.println("偶数和是"+ sum);
}
}
运行:
9.求1000以内水仙花数 (153=1*1*1 + 5*5*5 + 3*3*3)
public class task5{
public static void main(String[] args){
for(int i = 100;i < 1000;i++){
int bai = i / 100;
int shi = (i %100)/10;
int ge = i % 10;
if((bai*bai*bai + shi*shi*shi +ge*ge*ge == i )){
System.out.println("水仙花数为:"+ i);
}
}
}
}
运行:
10.输出图形
*
**
***
****
*****
import java.util.Scanner;
public class task6{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("请输入行数");
int row = sc.nextInt();
if(row == -1){
break;
}
for(int i = 0;i < row;i++){
//控制行数
for(int j = 0;j <= i;j++){
System.out.print("*");
}
System.out.println();
}
}
}
}
当 row = -1 时,跳出循环
运行:
10.打印九九乘法表
运行:
11.
public class test{
public static void main(String[] args){
// 图形
for (int i = 1; i <= 5; i++) {
// 先输出空格
for (int j = 1; j <= 5 - i; j++) {
System.out.print(" ");
}
// 打印 *
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
运行:
12.小芳的妈妈每天给她2.5元钱,她都会存起来,但是,每当这一天是存钱的第5天或者5的倍数的话,她都会花去6元钱,请问,经过多少天,小芳才可以存到100元钱。
public class xiaofang{
public static void main(String[] args){
float count = 2.5f;
for(int i=1;i<100;i++){
//小芳每天存2.5块
count=count+2.5f;
//每到第5天或者5 的倍数的天数就用掉6块钱
if(i%5==0){
count=count-6;
}
//直到存到100块钱,输出这一天
if(count==100){
System.out.println(i);
return ;//结束for循环
}
}
}
}
运行:
方法二:
方法三