笔试题while(scanner.hasnextline())怎么让程序停止读取

Scanner常用的方法

  • next()
  • nextLine()
  • nextInt()
  • hasNext()

1.在本地的IDE编辑器中

当输入完之后输入:

idea ctrl+d

eclipse ctrl+z

2.在笔试考试中

2.1如果已经告诉了你输入几行几列

如 第一行为输入的几行几列,第二行为对应输入的字符

3 3

kkk

ddd

jjj

这段代码会在你输入完3次后自动结束并完全获取到结果

        Scanner in = new Scanner(System.in);        
        String s = "";
        for(int i = 0 ; i < 3 ; i++){
            s+=in.nextLine();
//            System.out.println(s);
        }
        System.out.println(s);

2.2并没有终止符号 需要自行终止(情况比较少)

public class StandardInput {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        List<Integer> list = new ArrayList<>();
        while(!sc.hasNext("#")){
            int a = sc.nextInt();
            list.add(a);
        }
        System.out.println(list);
    }
}

2.3 输入特殊数字终止

import java.util.Scanner;
 
public class Main{
    public static void main(String[] arg){
        Scanner sc = new Scanner(System.in);
         
        while(sc.hasNext()){
            int a = sc.nextInt();
            int b = sc.nextInt();
            if(a==0 && b==0) break;
            System.out.println(a+b);
        }
    }
}

3.关于数组

 3.1输入输出数组

比如:
1 5
10 20
0 0
输出:
6
30

import java.util.*;
public class Test{
    public static void main(String[] args){
        int a[] = new int[8];
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入数组,并以回车结束:");
        for(int i = 0; i < a.length; i++){
            a[i] = sc.nextInt();
        }
        
        System.out.println("数组a为:" + Arrays.toString(a)); 
    }
}

请输入数组,并以回车结束:
1 2 3 4 8 7 6 5
数组a为:[1, 2, 3, 4, 8, 7, 6, 5]

 注意:

System.out.println(Arrays.toString(nums)); //打印数组
System.out.println(nums.toString()); //直接打印数组a出来的是数组的首地址 

3.2

  • 输入的第一行包括一个正整数t(1 <= t <= 100), 表示数据组数。
    接下来t行, 每行一组数据。
    每行的第一个整数为整数的个数n(1 <= n <= 100)。
    接下来n个正整数, 即需要求和的每个正整数。
  • 输出:求和

比如:
2
4 1 2 3 4
5 1 2 3 4 5
输出:
10
15

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for(int i=0;i<n;i++){
            int m  = sc.nextInt();
            int sum=0;
            for(int j=0;j<m;j++){
                sum += sc.nextInt();
            }
            System.out.println(sum);
        }
    }
}    

4.关于字符串

4.1

  • 输出:对于每组用例输出一行排序后的字符串,用’,'隔开,无结尾空格

比如:
a,c,bb
f,dddd
nowcoder
输出:
a,bb,c
dddd,f
nowcoder

import java.util.*;
 
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()){
            String[] str = sc.nextLine().split(",");
            Arrays.sort(str);
            StringBuilder sb = new StringBuilder();
            for (int i=0;i<str.length;i++){
                sb.append(i==(str.length-1)? str[i]:str[i]+",");
            }
            System.out.println(sb.toString());
        }
        
    }
} 

小问题:

其实第一次我是这样写的,后面测试了一下发现确实是不行,但不是很懂为什么,希望有人能解答

        Scanner in = new Scanner(System.in);
        int a = in.nextInt(); //3
        int b = in.nextInt(); //3
//        int a = 3 , b = 3;
        char[][] res = new char[a][b];
        while(in.hasNext()){
            int i = 0;
            res[i++] = in.nextLine().toCharArray();
//            s += in.nextLine();
        }

        System.out.println(res[0]);
        System.out.println(res[1]);
        System.out.println(res[2]);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kkoneone11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值