用while和switch编写一个计算器
要求:写一个计算器,要求实现加减乘除功能,并且能够循环接收新的数据,通过用户交互实现。
思路推荐:
写4个方法:加减乘除
minus减 除divide
multiply乘 add 加
利用循环while 和 switch进行用户交互
传递需要操作的两个数
//定义加减乘除四种运算
public static double add(double a, double b) {
return a + b;
}
public static double minus(double a, double b) {
return a - b;
}
public static double multiply(double a, double b) {
return a * b;
}
public static double divide(double a, double b) {
return a / b;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int x = 0;//定义x等于0时执行while循环
while (x == 0) {
System.out.println("请输入第一个数字:");
//输出运算的整个过程
double a = scanner.nextDouble();
System.out.println("请输入运算符:");
String str = scanner.next();
System.out.println("请输入第二个数字:");
double b = scanner.nextDouble();
System.out.println(a + "" + str + "" + b);
double result = 0;//定义结果的值
switch (str) {
case "+":
result = add(a, b);//加法运算步骤
break;
case "-":
result = minus(a, b);//减法运算步骤
break;
case "*":
result = multiply(a, b);//乘法运算步骤
break;
case "/":
result = divide(a, b);//除法运算步骤
break;
default:
System.out.println("请重新输入");
}
System.out.println(result);//输出结果
System.out.println("请输入是否继续,输入y或者n");
String ifcontinue = scanner.next();//使用if循环判断是否继续
if (ifcontinue.equals("y")) {
x = 0;
} else {
x = 1;
}
}
}
for循环99乘法表打印
public static void main(String[] args) {
for (int j = 0; j <= 9; j++) {
for (int i=0;i<=j;i++){
System.out.print(j + "*" + i + "=" +(j*i)+" ");
}
System.out.println();
}
for循环在控制台输出一天的小时和分钟
循环嵌套:
循环语句中包含循环语句
需求:
在控制台输出一天的小时和分钟
分钟和小时的范围:
分钟:0 <= minute < 60
小时:0 <= hour < 24
public static void main(String[] args) {
for (int hour = 0; hour <= 24; hour++) {
for (int minute = 0; minute <= 60; minute++) {
System.out.println(hour + "时" + minute + "分");
}
}}
if语句判断成绩是否及格
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩");
int score = scanner.nextInt();
if (score == 100) {
System.out.println("满分");
} else if (score < 100 && score >= 90) {
System.out.println("A");
}else if(score<90&&score>=80){
System.out.println("B");
}else if (score<80&&score>=70){
System.out.println("C");
}else if (score<70&&score>=60) {
System.out.println("D");
}else if (score>0&&score<60){
System.out.println("不及格");
} else {
System.out.println("成绩不合法");
}
scanner.close();
}
if语句判断输入的是不是偶数
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number =scanner.nextInt();
if (number%2==0){
System.out.println(number+"是偶数");
}else {
System.out.println(number+"不是偶数");
}
scanner.close();
}
switch语句判断输入的月份是什么季节
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入月份");
int month = scanner.nextInt();
switch (month){ //switch判断属于哪个季节
case 1:
case 2:
case 12:
System.out.println("冬季");
break;
case 3:
case 4:
case 5:
System.out.println("春季");
break;
case 6:
case 7:
case 8:
System.out.println("夏季");
break;
case 9:
case 10:
case 11:
System.out.println("秋季");
break;
default:
System.out.println("你输入的月份有误");
}
}
打印三角形
public static void main(String[] args) {
//打印三角形
for (int i =1;i<=5;i++){
for (int j =5 ;j>=i;j--){
System.out.print(" ");
}
for (int j =1;j<=i;j++){
System.out.print("*");
}
for (int j =1;j<i;j++){
System.out.print("*");
}
System.out.println();
}
}
while语句计算1到100 的和
public static void main(String[] args) {
int i =0;
int sum =0;
while (i<=100){
sum+=i;
i++;
}
System.out.println(sum);
}