zoukankan      html  css  js  c++  java
  • 课后作业

    信1403-2班20142976蒋保宁

    一:编写一个程序,用户输入两个数,求出其加减乘除,并用消息框显示计算结果。

    源代码如下:

    package demo;
    import javax.swing.JOptionPane;
    public class Test {
    
    	public static void main(String[] args) {
    		
    		// TODO 自动生成的方法存根
    		String n1,n2;
    		int a,b;
    		n1=JOptionPane.showInputDialog("请输入第一个数:");
    		n2=JOptionPane.showInputDialog
    				("请输入第二个数:");
    		a=Integer.parseInt(n1);
    		b=Integer.parseInt(n2);
    		double he,cha,ji,shang;
    		he=a+b;
    		cha=a-b;
    		ji=a*b;
    		shang=a/b;
    		JOptionPane.showMessageDialog(null,"两数的和是:"+he+"\n"+"两数 的差为:"+cha+"\n"+"两数的乘积为:"+ji
    				+"\n"+"两数的商为:"+shang,"结果",
    				JOptionPane.PLAIN_MESSAGE);
    				System.exit(0);
    		}
    
    }
    

      

    分析:在程序中调用Integer类的方法将string类型变量转化为int类,所以如果我在输入框中输入不是int类型的数,系统就会报错。

    通过JOptionPane类的两个method,即showInputDialog和showMessageDialog的简单调用,示范了对话框的基本用法。

    鉴于对eclipse并不是很熟练的掌握,对于其内部的一些函数以及接口之类的并不是很清楚,不知道里面有什么函数和接口,以及哪些函数需要给哪些参数,功能是什么,返回值是什么等等,所以需要上网查JDK 1.8 API。

    二:解决课程中的所有动手动脑的问题以及课后实验性的问题

    1、Addition.java

    package demo;
    
    import javax.swing.JOptionPane;  
    public class Addition {
       public static void main( String args[] )
       {
          String firstNumber,   
                 secondNumber;  
          int number1,          
              number2,          
              sum;              
    
         
          firstNumber =
             JOptionPane.showInputDialog( "请输入第一个数" );
    
         
          secondNumber =
             JOptionPane.showInputDialog( "请输入第二个数" );
    
         
          number1 = Integer.parseInt( firstNumber ); 
          number2 = Integer.parseInt( secondNumber );
    
          
          sum = number1 + number2;
    
        
          JOptionPane.showMessageDialog(
             null, "两数相加的和是 " + sum, "Results",
             JOptionPane.PLAIN_MESSAGE );
    
          System.exit( 0 );   
       }
    }

    截图:

    2、InputTest

    package demo;
    import java.util.*;
    public class InputTest {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		 Scanner in = new Scanner(System.in);
    
    	      // get first input
    	      System.out.print("What is your name? ");
    	      String name = in.nextLine();
    
    	      // get second input
    	      System.out.print("How old are you? ");
    	      int age = in.nextInt();
    	      
    	      
    	   
    	      System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));
    	  
    	      
    	   }
    	}
    

      截图:

    3、RandomStr

    源代码:

    package demo;
    
    public class RandomStr {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    
    		//定义一个空字符串
    		String result = "";
    		//进行6次循环
    		for(int i = 0 ; i < 6 ; i ++)
    		{
    			//生成一个97~122的int型的整数
    			int intVal = (int)(Math.random() * 26 + 97);
    			//将intValue强制转换为char后连接到result后面
    			result = result + (char)intVal;
    		}
    		//输出随机字符串
    		System.out.println(result);
     	}
    }
    

      截图:

    4、Plus

    源代码:

    package demo;
    
    
    public class Plus {
    
        public static void main(String[] args)
    
        {
    
    int X=100;
    
    int Y=200;
    
    System.out.println("X+Y="+X+Y);
    
    System.out.println(X+Y+"=X+Y");
    
        }
    
    }
    

     截图:

    分析:在System.out.println()中,如果在string字符串后面是+和变量,会把变量转换成string类型,加号起连接作用,然后把两个字符串连接成一个新的字符串输出;如果先有变量的加减运算再有字符串,那么会从左到右先计算变量的加减,然后再与后面的string结合成一个新的字符串。也就是说加号只有在两个string类型或者其中一个是string类型的时候才起到连接作用,否则仍然是运算符。

    4、RandomStr

    源代码:

    package demo;
    import javax.swing.JOptionPane;
    public class RandomStr {
    	public static void main(String[] args) {
        //生成一个6位的由小写字母组成的随机字符串    
            int weishu=6;
            String str = "";
            for(int i=1;i<=weishu;i++)
            {
                //生成一个表示a~z的ASCII的随机数
                int intValue = (int)(Math.random()*26+97);
                //将此随机数转化为其对应的字母并连接在str后面
                str = str + (char)intValue;
            }
        //随机字符串生成完毕
            String inputStr;
            inputStr = JOptionPane.showInputDialog("请输入以下验证码\n"+str);
            if(inputStr.equals(str))
            {
                JOptionPane.showMessageDialog(null, "验证成功");
            }
            else
            {
                JOptionPane.showMessageDialog(null, "抱歉,验证失败");
            }
        }
    
    }
    

      截图:

    5、TestBigDecimal

    源代码:

    package demo;
    import java.math.BigDecimal;
    
    public class TestBigDecimal
    
    {
    
    public static void main(String[] args) 
    
    {
    
    BigDecimal f1 = new BigDecimal("0.05");
    
    BigDecimal f2 = BigDecimal.valueOf(0.01);
    
    BigDecimal f3 = new BigDecimal(0.05);
    
    System.out.println("下面使用String作为BigDecimal构造器参数的计算结果:");
    
    System.out.println("0.05 + 0.01 = " + f1.add(f2));
    
    System.out.println("0.05 - 0.01 = " + f1.subtract(f2));
    
    System.out.println("0.05 * 0.01 = " + f1.multiply(f2));
    
    System.out.println("0.05 / 0.01 = " + f1.divide(f2));
    
    System.out.println("下面使用double作为BigDecimal构造器参数的计算结果:");
    
    System.out.println("0.05 + 0.01 = " + f3.add(f2));
    
    System.out.println("0.05 - 0.01 = " + f3.subtract(f2));
    
    System.out.println("0.05 * 0.01 = " + f3.multiply(f2));
    
    System.out.println("0.05 / 0.01 = " + f3.divide(f2));
    
    }
    
    }
    

      截图:

  • 相关阅读:
    等额本息还款法、等额本金
    数学集合:N Z Q R C
    Xshell配置SSH免密码登录
    【转】Microsoft .Net Remoting之Marshal、Disconnect与生命周期以及跟踪服务
    【转】Microsoft .Net Remoting 之.Net Remoting基础篇
    【转】C#调用WebService实例和开发
    【MYSQL】存储过程示例
    【转】【C++】C++ 中的线程、锁和条件变量
    【C++】线程_beginthreadex参数说明
    【转】【c++】指针参数是如何传递内存的
  • 原文地址:https://www.cnblogs.com/sist1437493141/p/4858222.html
Copyright © 2011-2022 走看看