首页 > 编程语言 >JAVA语法基础总结

JAVA语法基础总结

时间:2024-09-25 23:02:59浏览次数:1  
标签:总结 10 JAVA String System 0.05 语法 println out

package com.chunchuner.fourcompute;
import java.util.Random;
public class Arithmatics {
private static Random random = new Random();
private final static int COUNT = 30;

private static boolean getProject() {
    int num1 = random.nextInt(101);
    int num2 = random.nextInt(101);
    int index = random.nextInt(4);
    String str = "";
    boolean flag = false;
    int answer = 0;
    switch (index) {
    case 0:
        answer = num1 + num2;
        if (answer > 100) {
            break;
        }
        str = num1 + "+" + num2 + "=";
        flag = true;
        break;
    case 1:
        if (num1 < num2) {
            break;
        }
        str = num1 + "-" + num2 + "=";
        flag = true;
        break;
    case 2:
        answer = num1 * num2;
        if (answer > 100) {
            break;
        }
        str = num1 + "*" + num2 + "=";
        flag = true;
        break;
    case 3:
        if (num2 == 0) {
            break;
        }
        str = num1 + "÷" + num2 + "=";
        flag = true;
        break;
    }
    if (flag) {
        System.out.println(str);
    }
    return flag;
}
public static void main(String[] args) {
    System.out.println("");
    int count = 0;
    do {
        if (getProject()) {
            count++;
        }
    } while (count < COUNT);
}

}
结果:
17÷39=
99-40=
0+87=
80÷93=
51-40=
59÷21=
4+66=
1÷62=
49+30=
86÷8=
54÷20=
36+29=
53-11=
46-1=
27*1=
12+80=
49-22=
6+23=
37+54=
98÷76=
77-17=
42+35=
64+16=
77+18=
14+14=
79÷8=
95-23=
77+13=
37+16=
43÷86=

// An addition program

import javax.swing.JOptionPane; // import class 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

}
}

public class EnumTest {

public static void main(String[] args) {
	Size s=Size.SMALL;
	Size t=Size.LARGE;
	//s和t引用同一个对象?
	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};

false
false
true
SMALL
MEDIUM
LARGE

/**
@version 1.10 2004-02-10
@author Cay Horstmann
*/

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

}
}

What is your name? 1
How old are you? 32
Hello, 1. Next year, you'll be 33

public class RandomStr
{
public static void main(String[] args)
{
//定义一个空字符串
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);
}
}
cjfkbe

// Drawing shapes
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

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));
}
}
下面使用String作为BigDecimal构造器参数的计算结果:
0.05 + 0.01 = 0.06
0.05 - 0.01 = 0.04
0.05 * 0.01 = 0.0005
0.05 / 0.01 = 5
下面使用double作为BigDecimal构造器参数的计算结果:
0.05 + 0.01 = 0.06000000000000000277555756156289135105907917022705078125
0.05 - 0.01 = 0.04000000000000000277555756156289135105907917022705078125
0.05 * 0.01 = 0.0005000000000000000277555756156289135105907917022705078125
0.05 / 0.01 = 5.000000000000000277555756156289135105907917022705078125

标签:总结,10,JAVA,String,System,0.05,语法,println,out
From: https://www.cnblogs.com/wsh-wsh-/p/18432483

相关文章

  • 06 常用内置模块总结
    -其他需背会len获取长度openrange随机生成数id是比较内存地址is/==是进行比较type获取数据类型输入输出printinput强制转换dict()list()tuple()int()str()bool()set()数学相关abs,绝对值v=abs(-1)print(v)float,转换成浮点型(小数)v=55v1=......
  • 精通Java并发锁机制:24种锁技巧+业务锁匹配方案(第一部分)
    在Java并发编程中,锁是确保线程安全、协调多线程访问共享资源的关键机制。从基本的synchronized同步关键字到高级的ReentrantLock、读写锁ReadWriteLock、无锁设计如AtomicInteger,再到复杂的同步辅助工具如CountDownLatch、CyclicBarrier和Semaphore,每种锁都针对......
  • javascript
    letconst\(let\)局部声明$const$全局声明StringNumberBooleannullundefined字符串,数字,布尔值,空值,未定义console.log(typeofA);//A的类型连接&模板字符串constusername="dzk";constage=20;consthello=`Mynameis${username}andIam${age}......
  • 9.25日总结
    单向链表单向链表是最基本的一种链表形式。每个节点包含一个数据元素和一个指向下一个节点的指针。单向链表的优点在于实现简单,插入和删除操作方便。缺点是只能从头节点开始遍历整个链表,访问效率较低。双向链表双向链表在单向链表的基础上增加了前驱节点指针,使得可以从任意......
  • 精通Java并发锁机制:24种锁技巧+业务锁匹配方案(第一部分)
    在Java并发编程中,锁是确保线程安全、协调多线程访问共享资源的关键机制。从基本的synchronized同步关键字到高级的ReentrantLock、读写锁ReadWriteLock、无锁设计如AtomicInteger,再到复杂的同步辅助工具如CountDownLatch、CyclicBarrier和Semaphore,每种锁都针对特定的......
  • Java BigDecimal 详解
     目录一、BigDecimal简介二、常用方法A、BigDecimal常用构造方法B、BigDecimal常用方法二、代码实现A、加减乘除1.创建两个BigDecimal对象2.BigDecimal相加3.BigDecimal相减4.BigDecimal相乘5.BigDecimal相除B、转换1.定义一个数值2.转换3.java.math.BigDeci......
  • Java中的序列化和反序列化
    Java中序列化和反序列化的区别序列化和反序列化的定义序列化(Serialization)与反序列化(Deserialization)是编程中常见的两个概念,他们主要涉及到将数据结构或对象状态转换为可以存储或传输的格式,以及将存储或传输的格式转换回原始的数据结构或对象状态的过程。这两个过程在数据持久......
  • 进制数知识(2)—— 浮点数在内存中的存储 和 易混淆的二进制知识总结
    目录1.浮点数在内存中的存储1.1浮点数的大V表示法1.2浮点数的存储格式1.3 浮点数的存入规则1.4 浮点数的读取规则1.5补充:移码与掩码1.6 题目解析2. 易错的二进制知识2.0 符号位到底会不会参与运算?2.0.1存储前的编码变化运算2.0.2存储后的数值算术运算2......
  • javaScript 值的比较
    值的比较值的比较是指判断两个数的大小,返回一个布尔值。  比较运算符列表:   大于>  小于<  大于等于>= 小于等于<= 等于== 严格等于===不进行类型转换不等于!= 严格不等于!==不进行类型转换 字符串比较大小字符串间的比较大小遵循以下规则:1比较字符串首字母的大小。......
  • JavaScript中if嵌套 assert
    在JavaScript中,通常我们不会直接使用assert这个词,因为JavaScript标准库中并没有直接提供assert函数(尽管在一些测试框架如Jest、Mocha中经常看到)。但是,我们可以模拟一个assert函数的行为,即当某个条件不满足时抛出一个错误。结合if语句进行嵌套判断时,可以在每个需要断言的地方调用这......