java scanner输入数组_java基础- scanner/方法/数组

1.用户交互scanner

Next()

18ba175460ea401cc5936c65e3342444.png

10aff0b3e54ab69ec0f198ecd3b89d6d.png

public class demo1 {

public static void main(String[] args) {

//创建一个scanner对象

Scanner scanner = new Scanner(System.in);

System.out.println("请使用next方式进行接收:");

//判断有无输入字符

if(scanner.hasNext()){

String str=scanner.next();

System.out.println("输入的内容是"+str);

}

//关闭scanner,否则一只占用资源

scanner.close();

}

}

/*输出结果是:

请使用next方式进行接收:

happy newyear

输入的内容是happy

*/

Nextline()

d5689a70ef43e0aa8fc65c00a6e0faa6.png

7ecab3ce7ffb0ac1355ea2328f13f5e8.png

public class demo2 {

public static void main(String[] args) {

//创建一个scanner对象

Scanner scanner = new Scanner(System.in);

System.out.println("请使用nextline方式进行接收:");

//判断有无输入字符

if(scanner.hasNextLine()){

String str=scanner.nextLine();

System.out.println("输入的内容是"+str);

}

//关闭scanner,否则一只占用资源

scanner.close();

}

}

/*

请使用nextline方式进行接收:

happy newyear

输入的内容是happy newyear

*/

一定注意close掉对象,否则一直占用

2.scanner进阶使用

在while中使用scanner

public class demo2 {

public static void main(String[] args) {

//创建一个scanner对象

Scanner scanner = new Scanner(System.in);

//判断有无输入字符

double sum=0;

int m=0;

while(scanner.hasNextDouble())

{

double x=scanner.nextDouble();

sum+=x;

m++;

}

System.out.println(""+sum);

System.out.println(""+m);

//关闭scanner,否则一只占用资源

scanner.close();

}

}

***跳过顺序结构,if,switch,while,dowhile,for, break,coutinue,goto

3.for循环

System.out.println();

System.out.print();

打印99乘法表

for(int i=1;i<10;i++)

{

for(int j=1;j<=i;j++)

{

System.out.print(""+i+"*"+j+"="+(i*j)+"\t");

}

System.out.println();

}

快捷方式:5.for

4.增强for

用于遍历数组

int[] numbers={1,2,3};

for(int x:numbers){

System.out.print(x+"\t");

}

debug

5.方法

c6aaf3e8232167305fb262e9efdd12ee.png

方法名

· 函数名用首字母大写的英文单词组合表示(如用动词+名词的方法),其中至少有一个动词

· 应该避免的命名方式

§ 和继承来的函数名一样。即使函数的参数不一样,也尽量不要这么做,除非想要重载它

§ 只由一个动词组成,如:Save、Update。改成如:SaveValue、UpdateDataSet则比较好

· 函数参数的命名规则

§ 函数参数应该具有自我描述性,应该能够做到见其名而知其意

§ 用匈牙利命名法命名

public static void main(String[] args) {

int sum=add(1,2);

System.out.println(sum);

}

public static int add(int a,int b)

{

return a+b;

}

6.方法的重载

要求

042c418fdf8b7f2c065c613e80126e7d.png

7.命令行传参

先用javac进行编译

再回到代码的包的上层,开始执行

public static void main(String[] args) {

for (int i = 0; i < args.length; i++) {

System.out.println("args["+i+"]"+args[i]);

}

}

4.

642f566e73118163b6c43b0df0b63159.png

8.可变参数

多个同类型的参数,数量可以不确定

只能有一个,且放在参数的最后

public class demo2 {

public static void main(String[] args) {

demo2 demo=new demo2();

demo2.test(1,2,34);

}

public static void test(double d,int ... x)

{

System.out.println(x[1]);

}

}

递归调用,没看

9.数组

申明数组

int[] nums1;//首选

int nums2[];

创建数组

dataType[] arrayRefVar =new dataType[arraySize];

nums1=new int[10];

给数组赋值

获取数组长度

nums1.length

声明数组

放在栈中,并不实际存在

创建数组

放在堆中

静态初始化

int[] a={1,2,3,4,5};

动态初始化

int[] b;

b[0]=1;

...

11.arrays类详细

在idea中输入Arrays

选择goto,选择deceleration and usages,选择左下角structure,可以查看arrays类的方法目录

Arrays.sort()等

12.稀疏数组

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值