• 作业5 测试与封装 四则运算


    package aaa;
    import java.text.DecimalFormat;
    import java.util.Scanner;
    public class Dada {
    	
    	public static void main(String[] args) {
    		int condition=1 ;
    		Dada dada = new Dada();
    		Scanner scanner = new Scanner(System.in);
    		while(condition==1){
    		System.out.print("请输入第一个数:");
    		double x = scanner.nextDouble();
    		System.out.print("请输入第二个数:");
    		double y = scanner.nextDouble();
    		System.out.print("请输入运算符:");
    		String s = scanner.next();
    		char z = s.charAt(0);
    		Test test=new Test();
    		test.yunsuan(x, y, z);
    		System.out.print("是否继续?1是  其他为否");
    		condition = scanner.nextInt();}
    		}
    	}
    
    
    
    package aaa;
    
    import java.text.DecimalFormat;
    public class Test{
    public static void yunsuan(double x,double y,Character z){
    		DecimalFormat r=new DecimalFormat(); 
    		r.applyPattern("#0.00");
    		if(z.equals('+')){
    			System.out.println(x+"+"+y+"=" + r.format((x+y)));
    		} else if(z.equals('-')){
    			System.out.println(x+"-"+y+"=" + r.format((x-y)));
    		} else if(z.equals('*')){
    			System.out.println(x+"*"+y+"=" + r.format((x*y)));
    		} else if(z.equals('/')){
    			if(y==0){
    				System.out.println("被除数不能为0");
    			} else{
    				System.out
    		
    				.println(x+"/"+y+"=" + r.format((x/y)));
    			}
    	
    		}else{
    			System.out.println("无法识别改运算符");
    		}
    	
    	}}
    	
    

      没有找到小伙伴,因为很多同学都回去了。这是我做的一个小程序,虽然没有很多功能,但是这个程序有很多可以改进以及可以添加很多功能。

  • 相关阅读:
    类和对象
    使用JAVA理解程序逻辑
    类的有参
    1.人机猜拳
    类的无参方法
    类和对象
    Java解析XML
    集合框架一
    Java中的包
    继承
  • 原文地址:https://www.cnblogs.com/xhy1210/p/4468646.html
Copyright © 2020-2023  润新知