if语句:岳小鹏参加Java考试
岳小鹏参加Java考试,他和父亲岳不群达成承诺:
如果:
成绩为100分时,奖励一辆BMW;
成绩为(80,99]时,奖励一台iphone xs max;
当成绩为[60,80]时,奖励一个 iPad;
其它时,什么奖励也没有。
请从键盘输入岳小鹏的期末成绩,并加以判断
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入小鹏的期末成绩:");
Double double1 = input.nextDouble();
if (double1 == 100) {
System.out.println("一辆BMW");
}else if (double1 > 80 && double1 <= 99) {
System.out.println("一台iphone xs max");
}else if (double1 >= 60 && double1 <= 80) {
System.out.println("一个 iPad");
}else {
System.out.println("nothing");
}
}
if:键盘输入三数并排序
编写程序:由键盘输入三个整数分别存入变量num1、num2、num3,
对它们进行排序(使用 if-else if-else),并且从小到大输出。
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入整数1:");
int num1 = scanner.nextInt();
System.out.println("请输入整数2:");
int num2 = scanner.nextInt();
System.out.println("请输入整数3:");
int num3 = scanner.nextInt();
int small = 0;
int bigger = 0;
if (num1 < num2 ){
small = num1;
bigger = num2;
}else{
small = num2;
bigger = num1;
}
if (bigger < num3){
System.out.println(small + " " + bigger + " " + num3);
}else {
if (num3 < small){
System.out.println(num3 + " " + small + " " + bigger);
}else {
System.out.println(small + " " + num3 + " " + bigger);
}
}
}
if练习
- 编写程序,声明2个int型变量并赋值。判断两数之和,如果大于等
于50,打印“hello world!“
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入整数:");
int i = scanner.nextInt();
System.out.println("请输入整数:");
int j = scanner.nextInt();
if (i + j > 50){
System.out.println("hello world!");
}else{
System.out.println("nothing.");
}
}
- 编写程序,声明2个double型变量并赋值。判断第一个数大于10.0,
且第2个数小于20.0,打印两数之和。否则,打印两数的乘积。
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入小数:");
double i = scanner.nextDouble();
System.out.println("请输入小数:");
double j = scanner.nextDouble();
if (i > 10.0 && j < 20.0){
System.out.println(i + " + " + j + " = " + (i + j));
}else{
System.out.println(i + " * " + j + " = " + (i * j));
}
}
- 我家的狗5岁了,5岁的狗相当于人类多大呢?其实,狗的前两年每
一年相当于人类的10.5岁,之后每增加一年就增加四岁。那么5岁的狗
相当于人类多少年龄呢?应该是:10.5 + 10.5 + 4 + 4 + 4 = 33岁。
编写一个程序,获取用户输入的狗的年龄,通过程序显示其相当于人
类的年龄。如果用户输入负数,请显示一个提示信息。
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入小狗的年龄:");
double i = scanner.nextDouble();
if (i < 0){
System.out.println("年龄不能是负数哦!");
}else if (i >= 0 &&i <= 2){
System.out.println("相当于人类" + (10.5 * i) + "岁");
}else if (i > 2 ){
System.out.println("相当于人类" + (10.5 * 2 + 4 * (i - 2)) + "岁");
}
}
- 大家都知道,男大当婚,女大当嫁。那么女方家长要嫁女儿,当然要提出
一定的条件:高:180cm以上;富:财富1千万以上;帅:是。
- 如果这三个条件同时满足,则:“我一定要嫁给他!!!”
- 如果三个条件有为真的情况,则:“嫁吧,比上不足,比下有余。”
- 如果三个条件都不满足,则:“不嫁!”
scanner.nextBoolean(); scanner.next(); “是”.equals(str)
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("身高: (cm)");
int s = scanner.nextInt();
System.out.println("财富: (千万)");
double c = scanner.nextDouble();
System.out.println("帅否: (true/false))");
boolean b = scanner.nextBoolean();
if (s == 180 && c == 1 && b){
System.out.println("我一定要嫁给他!!!");
}else if (s == 180 || c == 1 || b){
System.out.println("嫁吧,比上不足,比下有余");
}else {
System.out.println("不嫁");
}
}
if:两位数的彩票
假设你想开发一个玩彩票的游戏,程序随机地产生一个两位数的彩票,提示用户输入
一个两位数,然后按照下面的规则判定用户是否能赢。
1)如果用户输入的数匹配彩票的实际顺序,奖金10 000美元。
2)如果用户输入的所有数字匹配彩票的所有数字,但顺序不一致,奖金 3 000美元。
3)如果用户输入的一个数字仅满足顺序情况下匹配彩票的一个数字,奖金1 000美元。
4)如果用户输入的一个数字仅满足非顺序情况下匹配彩票的一个数字,奖金500美元。
5)如果用户输入的数字没有匹配任何一个数字,则彩票作废。
提示:使用(int)(Math.random() * 90 + 10)产生随机数。
Math.random() : [0,1) * 90 =[0,90) + 10 =[10,100) = [10,99]
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入两位数:");
int i = scanner.nextInt();
int c= (int) (Math.random() * 90) * 10;
if (i == c){
System.out.println("奖金10 000美元");
}else{
int h = i/10;
int l = i % 10;
int ch = c / 10;
int cl = c % 10;
if ( l * 10 + h == c){
System.out.println("奖金 3 000美元");
}else if (ch == h || cl == l){
System.out.println("奖金1 000美元");
}else if (ch == l || cl == h){
System.out.println("奖金500美元");
}else{
System.out.println("很遗憾");
}
}
}