Totuial 01 java

本文精选了多个Java编程实验代码示例,涵盖基本数学运算、枚举类型操作、用户输入处理、随机字符串生成、图形绘制、数值转换及精度处理、小学算术题生成和登录界面验证码实现等,为初学者提供了实用的学习资源。

以下为课件中动手动脑问题的实验代码:

1

import javax.swing.JOptionPane;  

public class Addition {

   public static void main( String args[] )

   {

      String firstNumber,   // first string entered by user

             secondNumber;  // second string entered by user

      int number1,          // first number to add

          number2,          // second number to add

          sum;              // sum of number1 and number2

 

      // read in first number from user as a string

      firstNumber =

         JOptionPane.showInputDialog( "Enter first integer" );

 

      // read in second number from user as a string

      secondNumber =

         JOptionPane.showInputDialog( "Enter second integer" );

 

      // convert numbers from type String to type int

      number1 = Integer.parseInt( firstNumber );

      number2 = Integer.parseInt( secondNumber );

 

      // add the numbers

      sum = number1 + number2;

 

      // display the results

      JOptionPane.showMessageDialog(

         null, "The sum is " + sum, "Results",

         JOptionPane.PLAIN_MESSAGE );

 

      System.exit( 0 );   // terminate the program

   }

}

2

 

public class EnumTest {

 

public static void main(String[] args) {

Size s=Size.SMALL;

Size t=Size.LARGE;

//st引用同一个对象?

System.out.println(s==t);  //

//是原始数据类型吗?

System.out.println(s.getClass().isPrimitive());

//从字符串中转换

Size u=Size.valueOf("SMALL");

System.out.println(s==u);  //true

//列出它的所有值

for(Size value:Size.values()){

System.out.println(value);

}

}

 

}

 enum Size{SMALL,MEDIUM,LARGE};

3

 

import java.util.*;

 

public class InputTest

{  

   public static void main(String[] args)

   {  

      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();

      

      

    /* int i;

     String value="100";

     i=Integer.parseInt(value);

     i=200;

     String s=String.valueOf(i);*/

     

      // display output on console

      System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));

  

      

   }

}

4

 

 

 

public class RandomStr

{

public static void main(String[] args)

{

//定义一个空字符串

String result = "";

//进行6次循环

for(int i = 0 ; i < 6 ; i ++)

{

//生成一个97122int型的整数

int intVal = (int)(Math.random() * 26 + 97);

//intValue强制转换为char后连接到result后面

result = result + (char)intVal;

}

//输出随机字符串

System.out.println(result);

  }

}

 

5

 

import java.awt.Graphics;

import javax.swing.*;

 

public class SwitchTest extends JApplet {

   int choice;   

 

   public void init()

   {

      String input;

 

      input = JOptionPane.showInputDialog(

                 "Enter 1 to draw lines\n" +

                 "Enter 2 to draw rectangles\n" +

                 "Enter 3 to draw ovals\n" );

 

      choice = Integer.parseInt( input );

   }

 

   public void paint( Graphics g )

   {

      for ( int i = 0; i < 10; i++ ) {

         switch( choice ) {

            case 1:

               g.drawLine( 10, 10, 250, 10 + i * 10 );

               break;

            case 2:

               g.drawRect( 10 + i * 10, 10 + i * 10,

                           50 + i * 10, 50 + i * 10 );

               break;

            case 3:

               g.drawOval( 10 + i * 10, 10 + i * 10,

                           50 + i * 10, 50 + i * 10 );

               break;

            default:

               JOptionPane.showMessageDialog(

                  null, "Invalid value entered" );

         } // end switch

      } // end for

   } // end paint()

} // end class SwitchTest

 

 

6

 

public class Test {

public static void main(String[] args) {

int intValue=100;

long longValue=intValue;

double doubleValue=1234567890;

float floatValue=(float)doubleValue;

System.out.println(floatValue);//1.23456794E9

 

int X=100;

int Y=200;

System.out.println("X+Y="+X+Y);

System.out.println(X+Y+"=X+Y");

doNotRunme();

 

String string="";

double d1=1000.123;

double d2=1000.123;

if(Math.abs(d2-d1)<1e-10){

 

}

//System.out.println(string);

 

}

 

public static void doNotRunme()

{

doNotRunme();

}

}

 

7

 

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));

}

}

 

8

 

 

 

public class TestDouble {

 

    public static void main(String args[]) {

        System.out.println("0.05 + 0.01 = " + (0.05 + 0.01));

        System.out.println("1.0 - 0.42 = " + (1.0 - 0.42));

        System.out.println("4.015 * 100 = " + (4.015 * 100));

        System.out.println("123.3 / 100 = " + (123.3 / 100));

    }

}

 

二、以下为课后作业中的实验代码:

 

1、以下为随机生成30道的小学计算题的源代码

//1705-2   何伟豪       20173629

package 小学计算题;

 

import java.util.Random;   //随机生成数字的头文件

import java.util.Scanner;  //输入整型数据的头文件

 

public class count {

 

public static void main(String args[]) {

Scanner cin=new Scanner(System.in);

Random random = new Random();

for(int i=0;i<30;i++)    //输出30个数的循环   开始1

{

double sum=0;

int a=random.nextInt(101);            //随机生成100以内的一个数

int b=random.nextInt(101);            //再随机生成100以内的一个数

System.out.print(a);                  //输出a

int tatted;                           //存储代表运算符的数

tatted=random.nextInt(3);             //随机生成代表运算符的数

if(tatted==0) {                       //将代表运算符的数转化为运算符   开始2

System.out.print("+");

sum=a+b;

}else if(tatted==1) {

System.out.print("-");

sum=a-b;

}else if(tatted==2) {

System.out.print("*");

sum=a*b;

}else if(tatted==3) {

System.out.print("/");

sum=a/b;

}                                     //数转化为运算符   结束2

System.out.print(b);//输出b

System.out.print("=");                //输出等于号

double sum0 = cin.nextInt();              //输入得数

if(sum0==sum) {

System.out.println("答案正确");

 

}else {

System.out.println("答案错误");

}

}     //输出30个数的循环   结束1

cin.close();

 

}

}

 

2、以下为模拟的登录界面(随机生成验证码)的源代码

package 验证码;

 

import javax.swing.JOptionPane;  

import java.util.Random;

 

public class textpass {

   public static void main( String args[] )

   {

  Random random = new Random();

  int a=random.nextInt(10);  //随机生成0-9以内的一个数     

  int b=random.nextInt(10);  //随机生成0-9以内的一个数

  int c=random.nextInt(10);  //随机生成0-9以内的一个数

  int d=random.nextInt(10);  //随机生成0-9以内的一个数

      String name , mima , yan;

      

      //输入登录名

      name =

         JOptionPane.showInputDialog( "登录名:" );

 

     //输入密码

      mima =

         JOptionPane.showInputDialog( "密码:" );

      

     //输入验证码

      yan =

      JOptionPane.showInputDialog( "验证码:" + a + " " + b + " " + c + " " + d );

      

      //最终显示

      JOptionPane.showMessageDialog(

         null, "登录名:" + name + "   密码:" + mima +"   验证码:" +yan, "Results",

         JOptionPane.PLAIN_MESSAGE );

      

      System.exit( 0 );  

   }

}

 

转载于:https://www.cnblogs.com/hwh000/p/9752368.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值