1.单个a+b=c
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);//从键盘中读入
int a,b,c;
a=reader.nextInt();//读入下一个数
b=reader.nextInt();
c=a+b;
System.out.println(c);//输出
reader.close();//节约系统资源
}
}
只能读入一次
1.2 更改,实现可以输入多组
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);//从键盘中读入
int a,b,c;
while(reader.hasNext()) {//多组输入
a=reader.nextInt();//读入下一个数
b=reader.nextInt();
c=a+b;
System.out.println(c);//输出
}
reader.close();//节约系统资源
}
}
标识符的组成
首字符必须是
1. 字母(英文字母或汉字) 2. 下划线 3. 美元符号“$” 4. 区分大小写
判断是否可以为标识符
Character.isJavaIdentifier
char start = str.charAt(0);
public class Main{
public static void main(String[] args) {
char ch='1';
boolean start = Character.isJavaIdentifierStart(ch);
boolean part = Character.isJavaIdentifierPart(ch);
System.out.println(start);
System.out.println(part);
}
}
///
false 说明1不能作为开始
true 1可以作为中间部分
ctrl+shift+f //代码格式标准化
题目
/*************************************************************************************
JAVA判断合法标识符
Description
输入若干行字符串,判断每行字符串是否可以作为JAVA语法的合法标识符。 判断合法标识符的规则:由字母、数字、下划线“_”、美元符号“$”组成,并且首字母不能是数字。
Input
输入有多行,每行一个字符串,字符串长度不超过10个字符,以EOF作为结束。
Output
若该行字符串可以作为JAVA标识符,则输出“true”;否则,输出“false”。
Sample
Input
abc
_test
$test
a 1
a+b+c
a’b
123
变量
Output
true
true
true
false
false
false
false
true
*************************************************************************************/
题解
import java.util.Scanner;
//判断是否为合法标识符
public class Main{
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);//创建读取信息
while (reader.hasNext()) {
String str = reader.nextLine();
char start = str.charAt(0);// 获得字符串的首字符
int len = str.length();
int flag = 1;// 假设为合法标识符
char ch;// 临时读取的字符
if (Character.isJavaIdentifierStart(start)) {//如果首字母合法则继续读取
for (int i = 1; i < len; i++) {
ch = str.charAt(i);
if (!Character.isJavaIdentifierPart(ch)) {
flag = 0;
break;
}
}
} else
flag = 0;
if (flag == 0)
System.out.println("false");
else
System.out.println("true");
}
}
}
常量和变量
- 局部变量
可以改变(重复定义) - 常量
加个关键字final ,只能赋值一次
final double PI=3.141
java的数据类型
byte 8位
short 16位
int 32位
long 64位
char 16位
float 32位
double 64位
boolean bb=true;//4个字节
*****byte -------short(char)-------int ----long---float------double
1 2 4 8 4 8
b=a;//byte类型赋值给short,精度小直接类型转换(由低到高自动进行)
//强制数据转换
int c=100;
d=(long)c;
题目
/*************************************************************************************
计算球体积
Description
根据输入的半径值,计算球的体积。
Input
输入数据有多组,每组占一行,每行包括一个实数,表示球的半径。
Output
输出对应的球的体积,对于每组输入数据,输出一行,计算结果保留三位小数。
Sample
Input
1
1.5
Output
4.189
14.137
Hint
已知 PI = 3.1415927
#define PI 3.1415927
*************************************************************************************/
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double PI=3.1415927;
Scanner reader=new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.000");
while(reader.hasNext()) {
double r=reader.nextDouble();
double v=(4.0*PI*r*r*r)/3;
System.out.println(df.format(v));
}
}
}
相同
public class Main {
public static void main(String[] args) {
Scanner reader =new Scanner(System.in);
final double PI=3.1415927;
//Scanner reader=new Scanner(System.in);
//DecimalFormat df = new DecimalFormat("0.000");
while(reader.hasNext()) {
double r=reader.nextDouble();
double v=(4.0*PI*r*r*r)/3;
System.out.printf("%.3f\n",v);
//System.out.println(df.format(v));
}
}
}
题目
/*****************************************************************************************************
九九乘法表
Description
九九乘法表是数学学习的基础,今天我们就来看看乘法表的相关问题。《九九乘法歌诀》,又常称为“小九九”,如下图所示。你的任务是写一个程序,对于给定的一个正整数 n ,输出“九九乘法表”的前 n 行。例如,输入 n 为 9,你的程序的输出将为下图:
Input
输入包含多组测试数据,以 EOF 结束。每组测试数据只包含一个正整数 n (0 < n < 10)。
Output
对于每组测试数据,输出上图所示“九九乘法表”的前 n 行。
Sample
Input
2
3
Output
11=1
12=2 22=4
11=1
12=2 22=4
13=3 23=6 3*3=9
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner reader =new Scanner(System.in);
while(reader.hasNext()) {
int n=reader.nextInt();
for(int i=1;i<=n;i++) {
int j=1;
for( j=1;j<i;j++) {
System.out.print(j+"*"+i+"="+i*j+" ");
}
System.out.println(j+"*"+i+"="+i*j);
}
}
}
}
求班级男女生比例
Description
新的一学期开始,HH同学在学校认识了很多的同学,但是他却不知道自己本班男生和女生的比例是多少。对于学过编程的你一定简单极了吧。你能帮他算出男生女生的百分比吗?
Input
多组输入,每组一行数据。
每行输入正整数男生和女生的数目n,m(0<n,m<1000)。
Output
输出男生女生所占的比例(保留小数点后两位)。
Sample
Input
20 30
10 10
50 0
Output
40.00% 60.00%
50.00% 50.00%
100.00% 0.00%
*****************************************************************************************************/
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.00");
while(reader.hasNext()) {
int fel=reader.nextInt();
int mel=reader.nextInt();
int total=fel+mel;
double f1=fel*100.00/total;
double f2=mel*100.00/total;
System.out.print(df.format(f1)+'%'+' ');
System.out.print(df.format(f2)+'%'+'\n');
}
}
}
same
public class Main {
public static void main(String[] args) {
Scanner reader =new Scanner(System.in);
while(reader.hasNext()) {
int fel=reader.nextInt();
int mel=reader.nextInt();
int total=fel+mel;
double f1=1.0*fel/total;
double f2=1.0*mel/total;
System.out.printf("%.2f%%\n",f1*100);
System.out.printf("%.2f%%\n",f2*100);
}
}
}
**通过封装类查看属性**
int -----Integer
byte-----Byte
short -----Short
long -----Long
double -----Double
float -----Float
chart -----Character
boolean -----Boolean
System.out.println(Integer.MAX_VALUE);//整型变量最大值
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.SIZE);//在内存中占的位数
System.out.println(Integer.TYPE);
//2147483647
//-2147483648
//32
//int
例如Integer这个类
还有方法: Integer.to
除了int是Integer外,其余的为各自的首字母大写,基本数据类型
Integer.to
输出位数
System.out.println(Integer.toBinaryString(100));//二进制
System.out.println(Integer.toHexString(100));//十六进制
System.out.println(Integer.toOctalString(100));//八进制
System.out.println(Integer.toString(100,6));//转化成6进制
1100100
64
144
244
运算符和表达式
1. 算术运算符
+ - * / % ++ -- -
char 类型在计算时,char会被提升为int 在进行计算
任何数据类型在与字符串进行+时,都会是一个字符串
2.关系运算符
> < >= <= == != instanceof
int a=10,b=20;
System.out.println(a>b);//false
Scanner reader=new Scanner(System.in);
System.out.println(reader instanceof Scanner);
//reader类指向的变量是否是Scanner的一个实例
//输出为布尔值
3.逻辑运算符
&& || !^ & |
^ 相同为false 不同为true
& 两个都要进行验证
&&验证一个为false直接返回
4.位运算
~ & | >> << >>>
右移(把右边的位数移出,>>> 无符号右移,左端补零,但不输出
public class Main {
public static void main(String[] args) {
int a=2;//00000..0010(32位)
int c=-1;//11111..1111(32)
System.out.println(Integer.toBinaryString(a>>1));
System.out.println(Integer.toBinaryString(a<<2));
System.out.println(Integer.toBinaryString(c>>3));//(补充符号位1)
System.out.println(Integer.toBinaryString(c>>>3));//无符号右移(无符号补0)
}
}
1
1000
11111111111111111111111111111111
11111111111111111111111111111
5.赋值运算符
= += -= *= /= %=
6.条件运算符
int flag=a>b?1:2;
题目
/*****************************************************************************************************
C/C++练习7—求某个范围内的所有素数
Description
求小于n的所有素数,按照每行10个显示出来。
Input
输入整数n(n<10000)。
Output
每行10个依次输出n以内(不包括n)的所有素数。如果一行有10个素数,每个素数后面都有一个空格,包括每行最后一个素数。
Sample
Input
100
Output
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97
import java.util.*;
import java.lang.Math;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int count = 0, i, j;
int n = reader.nextInt();
for (i = 2; i < n; i++) {
for (j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0)
break;
}
if (j > Math.sqrt(i)) {
System.out.print(i + " ");
count++;
if (count % 10 == 0)
System.out.println();
}
}
}
}
import java.util.*;
import java.lang.Math;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int count = 0, i;
int n = reader.nextInt();
if (n >2) {
System.out.print("2 ");
count++;
for (i = 3; i < n; i++) {
if (isPrime(i)) {
if (count%10==0) {
System.out.println();
}
System.out.print(i + " ");
count++;
}
}
}
}
private static boolean isPrime(int n) {
boolean flag = true;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
flag = false;
break;
}
}
return flag;
}
}
最小公倍数和最大公约数
Description
从键盘输入两个正整数,求这两个正整数的最小公倍数和最大公约数,并输出。
Input
输入包括一行。
两个以空格分开的正整数。
Output
两个整数的最小公倍数和最大公约数。
Sample
Input
6 8
Output
24 2
import java.util.*;
import java.lang.Math;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int a=reader.nextInt();
int b=reader.nextInt();
int k=gcd(a,b);
int m=a*b/k;
System.out.println(m+" "+k);
}
private static int gcd(int a,int b) {//定义一个方法(辗转相除法)
int r=0;
while((r=a%b)!=0) {
a=b;
b=r;
}
return b;
}
}
一维数组
public class Main {
public static void main(String[] args) {
int []a=new int[10];//定义一个长度为10的数组
System.out.println(a.length);//输出该数组的长度
for(int i=0;i<10;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
System.out.println(a);//输出该数组名称,首地址
}
}
10
0 0 0 0 0 0 0 0 0 0
[I@27716f4//int类型 [一维数组
数组格式声明
int[] a;
int a[];
数组元素的初始值
数据类型 初始值
byte 0
short 0
int 0
long 0
float 0.0f
double 0.0d
char \u0000
boolean false
引用类型 null
输出数组中元素的方式
Arrays方法 ,对a数组进行,
System.out.println(Arrays.toString(a));
[0,0,0,0,0,0,0,0,0,0]
///
foreach 方法
c是你需要访问的数组
for(int d : c)
{
System.out.println(d);
}
int[] c;
c=new int[]{1,2,3,4,5}
二维数组的声明
//声明
int[][] a;
int b[][];
//创建对象
a=new int[3][4];
b=new int[][]{{1,2,3},{4,5,6,7,8},{2,3},{6,7,8,9,10,11}};
输出数组
//传统for循环
for(int i=0;i<a.length,i++)
{
for(int j=0;j<a[i].length,j++)
{
System.out.print(a[i][j]+" ");
}
}
0 0 0 0
0 0 0 0
0 0 0 0
1 2 3
4 5 6 7 8
2 3
6 7 8 9 10 11
//用foreach
for(int[] k:a)
{
for(int l:k)
{
System.out.print(l+" ");
}
}
0 0 0 0
0 0 0 0
0 0 0 0
1 2 3
4 5 6 7 8
2 3
6 7 8 9 10 11
//用Arrays
System.out.println(Arrays.deepToString(a));
[[0,0,0,0], [0,0,0,0],[0,0,0,0]]
[[1,2,3],[4,5,6,7,8],[2,3],[6,7,8,9,10,11]]
int[][] e;
e=new int[2][];
e[0]=new int[2];
e[1]=new int[]{1,2,3};
[[0,0],[1,2,3]]
数组复制
import java.util.Arrays;
import java.lang.*;
public class Main {
public static void main(String[] args) {
//Scanner reader = new Scanner(System.in);
int a[]={1,2,3,4,5,6,7,8};
int b[]= new int [a.length];
//数组的复制
//1//System.arraycopy
System.arraycopy(a, 2, b, 1, 4);
System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(b));
//[1, 2, 3, 4, 5, 6, 7, 8]
// [0, 3, 4, 5, 6, 0, 0, 0]
//2//System.copyOf
int c[]=Arrays.copyOf(a, 3);
System.out.println(Arrays.toString(c));
//[1, 2, 3]
//3//System.copyOfRange
int c[]=Arrays.copyOfRange(a,3,8);
System.out.println(Arrays.toString(c));
//[4, 5, 6, 7, 8]
}
}
几个常用方法
sort() 对数组进行快排
binarySearch() 对已排序数组寻找索引
equals() 两一元数组值是否相同 bool
deepEquals() 二元数组
toString() 一元数组 返回数组内容
deepToStrin() 二元数组内容