1.编写程序, 输入变量x的值,如果是1,输出x=1,如果是5,输出x=5,如果是 10,输出 x=10,除了以上几个值,都输出x=none。(知识点:if条件语句)
package a;
import java.util.*;
public class aa {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("输入x的值:");
int i = sc.nextInt();
if (i == 1) {
System.out.println("x=" + i);
} else if (i == 5) {
System.out.println("x=" + i);
} else if (i == 10) {
System.out.println("x=" + i);
} else {
System.out.println("x=none");
}
}
}
2.用switch结构实现第1题
package a;
import java.util.*;
public class aa {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("输入x的值:");
int i = sc.nextInt();
switch (i) {
case 1:
System.out.println("x=" + i);
break;
case 5:
System.out.println("x=" + i);
break;
case 10:
System.out.println("x=" + i);
break;
default:
System.out.println("x=none");
break;
}
}
}
3.判断一个数字是否能被5和6同时整除(打印能被5和6整除),或只能被5整除(打印能被5整 除),或只能被6整除,(打印能被6整除),不能被5或6整除,(打印不能被5或6整除)
package a;
import java.util.*;
public class aa {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("输入x的值:");
int i = sc.nextInt();
if (i%5==0&&i%6==0) {
System.out.println("能被5和6整除");
}else if (i%5==0) {
System.out.println("能被5整除");
}else if (i%6==0) {
System.out.println("能被6整除");
}else{
System.out.println("不能被5和6整除");
}
}
}
4.输入一个0~100的分数,如果不是0~100之间,打印分数无效,根据分数等级打印 A(90-100),B(80-89),C,D,E(知识点:条件语句if elseif)
package a;
import java.util.*;
public class aa {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("输入0~100的成绩:");
double i = sc.nextDouble();
if (i <= 100 && i > 90) {
System.out.println("成绩为A");
} else if (i < 90 && i > 80) {
System.out.println("成绩为B");
} else if (i < 80 && i > 70) {
System.out.println("成绩为C");
} else if (i < 70 && i > 60) {
System.out.println("成绩为D");
} else if (i < 60) {
System.out.println("成绩为E");
} else {
System.out.println("输入成绩不符合规定");
}
}
}
5.输入三个整数x,y,z,请把这三个数由小到大输出(知识点:条件语句)
package a;
import java.util.*;
public class aa {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("输入三个数字");
double a, b, c, t;
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
if (a > b) {
t = b;
b = a;
a = t;
}
if (a > c) {
t = c;
c = a;
a = t;
}
if (b > c) {
t = c;
c = b;
b = t;
}
System.out.println(a+" "+b+" "+c);
}
}