1.输入一个字母,如果是大写,输出其小写格式,反之,如果是小写,输出其大写格式。
package com.itheima;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char c =sc.next().charAt(0);
if(c >= 'A' && c <= 'Z')
c += 32;
else if(c >= 'a' && c <= 'z')
c -= 32;
System.out.println(c);
}
}
2.输出两个数中最大的数
package com.itheima;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a,b;
a = sc.nextInt();
b = sc.nextInt();
if(a > b)
System.out.println(a);
else if(a < b)
System.out.println(b);
else
System.out.println("两数相等");
}
}
3.输出三个数中最大的数。
package com.itheima;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a,b,c,max;
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
if(a > b){
if(a > c)
max = a;
else
max = c;
}
else{
if(b > c)
max = b;
else
max = c;
}
System.out.println(max);
}
}
使用条件表达式,更简明、清晰。
package com.itheima;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a,b,c,mid,max;
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
mid = a > b?a:b;
max = mid > c ? mid:c;
System.out.println(c);
}
}
4.输入一个年份,判断其是否为闰年。
package com.itheima;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year;
year =sc.nextInt();
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
System.out.println(year+"年是闰年");
else
System.out.println(year+"年不是闰年");
}
}
5.分段函数,如果x < 1,y = x; 如果1<= x < 10,y = 2x - 1;如果x >= 10,y = 3x - 1 .
package com.itheima;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double x,y;
x = sc.nextDouble();
if(x < 1)
y = x;
else if(x < 10)
y = 2*x - 1;
else
y = 3*x - 1;
System.out.println(y);
}
}
6.给出一个百分制成绩,要求输出成绩等级’A’,‘B’,‘C’,‘D’,‘E’。90分以上为’A’,80~89分为’B’, 70-79分为’C’, 60-69分为’D’, 60以下为’E’。
package com.itheima;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int score;
score = sc.nextInt();
char grade;
if( score < 60 )
grade = 'E';
else if(score < 70)
grade = 'D';
else if(score < 80)
grade = 'C';
else if(score < 90)
grade = 'B';
else
grade = 'A';
System.out.println(grade);
}
}
7.根据给出的年份、月份、输出该月份所含的天数。
package com.itheima;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year, month;
int days = 0;
year = sc.nextInt();
month = sc.nextInt();
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 11:
days = 31;
break;
case 4:
case 6:
case 9:
case 12:
days = 30;
break;
case 2:
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0 )
days = 29;
else
days = 28;
break;
}
System.out.println(year+"年"+month+"月有"+days+"天");
}
}