Java编程!基本输入输出
答案:2 悬赏:40 手机版
解决时间 2021-03-24 08:43
- 提问者网友:容嬷嬷拿针来
- 2021-03-23 09:10
Java编程!基本输入输出
最佳答案
- 五星知识达人网友:猎心人
- 2021-03-23 10:05
import java.util.Scanner;
public class ASMD {
public static void main(String[] args) {
//以整数为例
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
add(a,b);
substract(a,b);
multiply(a,b);
divide(a,b);
}
public static void add(int a, int b) {
System.out.println(a + "+" + b + "=" + (a+b));
}
public static void substract(int a, int b) {
System.out.println(a + "-" + b + "=" + (a-b));
}
public static void multiply(int a, int b) {
System.out.println(a + "*" + b + "=" + (a*b));
}
public static void divide(int a, int b) {
//未涉及除不尽的情况
System.out.println(a + "/" + b + "=" + (a/b));
}
}
public class ASMD {
public static void main(String[] args) {
//以整数为例
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
add(a,b);
substract(a,b);
multiply(a,b);
divide(a,b);
}
public static void add(int a, int b) {
System.out.println(a + "+" + b + "=" + (a+b));
}
public static void substract(int a, int b) {
System.out.println(a + "-" + b + "=" + (a-b));
}
public static void multiply(int a, int b) {
System.out.println(a + "*" + b + "=" + (a*b));
}
public static void divide(int a, int b) {
//未涉及除不尽的情况
System.out.println(a + "/" + b + "=" + (a/b));
}
}
全部回答
- 1楼网友:封刀令
- 2021-03-23 10:39
使用java 8 ,除了实现需求外,另给你学习如何面向对象的思考方式import java.util.*;
public class InputDemo {
interface Action
R peform(I1 i1, I2 i2);
}
enum ACTION_NAME{
ADD{
public String toString() {
return "加法";
}
},
SUBTRACT{
public String toString() {
return "减法";
}
},
MULTI{
public String toString() {
return "乘法";
}
},
DIV{
public String toString() {
return "除法";
}
}
}
static private Action
switch (action_name){
case ADD:
return (aDouble, aDouble2) -> aDouble + aDouble2;
case SUBTRACT:
return (aDouble, aDouble2) -> aDouble - aDouble2;
case MULTI:
return (aDouble, aDouble2) -> aDouble * aDouble2;
case DIV:
return (aDouble, aDouble2) -> aDouble / aDouble2;
default:return null;
}
}
public static void main(String... args) {
Double f1,f2;
Scanner scanner=new Scanner(System.in);
System.out.printf("参数1:");
f1 = (double) scanner.nextFloat();
System.out.printf("参数2:");
f2 = (double) scanner.nextFloat();
Arrays.asList(ACTION_NAME.values())
.forEach(an->{
double v = createAction(an).peform(f1,f2);
System.out.printf("%s : (参数1: %f , 参数2: %f) = %f ", an, f1, f2, v);
});
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯