【小技巧】【牛客网】【JAVA】在线输入输出练习
【總結】
1. 一直輸入模板
import java.util.*; public class Main{public static void main(String[] args) {Scanner sc = new Scanner(System.in);while (sc.hasNext()) {//操作}} }2. 有組數或者輸入個數
import java.util.Scanner; public class Main{public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n=sc.nextInt();while(n>0) {//操作n--;}} }3. 一行有多個信息 split切分
// a c bb 一直輸入 import java.util.*;import java.util.Scanner; public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);while(sc.hasNext()) {String[] strs = sc.nextLine().split(" ");//操作}} }4.含結束標志
/**4. 數組求和 0為結束* 4 1 2 3 4(4個數 和為1+2+3+4 ) 5 1 2 3 4 5 0* 10 15*/import java.util.Scanner; public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);while(sc.hasNext()){int n = sc.nextInt();if (n == 0) {return;}int sum = 0;//while (n-- > 0){// sum += input.nextInt();//}for (int i = 0; i < n; i++) {sum += sc.nextInt();}System.out.println(sum);}}}5. 小技巧 /函數
5.1 拼接
String[] strs = sc.nextLine().split(" ");Arrays.sort(strs);// System.out.println(String.join(" ",s));String res = "";for(String s : strs)res += s + " ";System.out.println(res.trim());5.2 強轉型 字符串數組轉整數
// sc 0 0 0 0 0 String[] str = sc.nextLine().split(" ");for(int i=0;i<str.length;i++){sum+=Integer.parseInt(str[i]);}//for(String str:int1){// i+=Integer.valueOf(str);//}[小技巧][JAVA][轉換]字符數組與字符串之間互相轉換
[小技巧][JAVA][轉換]整型與字符串相互轉換
6.next() 與 nextLine() 區別
next():
1、一定要讀取到有效字符后才可以結束輸入。
2、對輸入有效字符之前遇到的空白,next() 方法會自動將其去掉。
3、只有輸入有效字符后才將其后面輸入的空白作為分隔符或者結束符。
next() 不能得到帶有空格的字符串。
nextLine():
1、以Enter為結束符,也就是說 nextLine()方法返回的是輸入回車之前的所有字符。
2、可以獲得空白。
鏈接:https://www.runoob.com/java/java-scanner-class.html
【牛客網想對你說】
每年前幾場在線筆試編程題的時候,總有同學詢問為什么我本地測試通過,自測也通過,提交代碼系統卻返回通過率0。
這不是系統的錯,可能是因為
1.你對題目理解錯了,你的代碼只過了樣例或你自己的數據
2.你的代碼邏輯有問題,你的代碼只過了樣例或你自己的數據
總之就是你的代碼只是過了樣例和自測數據,后臺的測試數據你根本不可見,要多自己思考。
這個題目如果你提交后通過率為0,又覺得自己代碼是正確的,可以 點擊查看 通過的代碼
謹記:
當你筆試的時候懷疑系統或者題目數據有問題的時候請務必先懷疑自己的代碼!
當你筆試的時候懷疑系統或者題目數據有問題的時候請務必先懷疑自己的代碼!
鏈接:https://ac.nowcoder.com/acm/contest/5647/K
來源:牛客網
【練習答案匯總】
/**1.數組求和 一直輸入 * 1 5 10 20 * * 630 * */ import java.util.*; public class Main{public static void main(String[] args) {Scanner sc = new Scanner(System.in);while (sc.hasNext()) {int a = sc.nextInt();// long a=sc.nextLong();int b = sc.nextInt();// long b=sc.nextLong();System.out.println(a + b);}} }/** 2.數組求和 有組數* 2(組數) 1 5 10 20* 6 30* */ import java.util.Scanner; public class Main{public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n=sc.nextInt();while(n>0) {int a=sc.nextInt();int b = sc.nextInt();int sum=a+b;System.out.println(sum);n--;}} }/** 3.數組 0 0 為結束* 1 5 10 200 0* 6 30* */ import java.util.Scanner; public class Main {public static void main(String[] args) {Scanner input = new Scanner(System.in);while (input.hasNext()){int a = input.nextInt();int b = input.nextInt();//!if (a == 0 && b == 0){break;}System.out.println(a + b);}} }/**4. 數組求和 0為結束* 4 1 2 3 4(4個數 和為1+2+3+4 ) 5 1 2 3 4 5 0* 10 15*/import java.util.Scanner; public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);while(sc.hasNext()){int n = sc.nextInt();if (n == 0) {return;}int sum = 0;//while (n-- > 0){// sum += input.nextInt();//}for (int i = 0; i < n; i++) {sum += sc.nextInt();}System.out.println(sum);}}}/** 5. 數組求和 有組數* 2(組數) 4 1 2 3 4 (4個數 和為1+2+3+4 ) 5 1 2 3 4 5**10 15*/import java.util.*;public class Main{public static void main(String[] args){Scanner sc=new Scanner(System.in);int t=sc.nextInt();for(int i=0;i<t;i++){int num=sc.nextInt();int sum=0;for(int j=0;j<num;j++){sum=sum+sc.nextInt();}System.out.println(sum);}} } /**6. 數組求和 一直輸入4 1 2 3 4 (4個數 和為1+2+3+4 )5 1 2 3 4 5*1015 */ import java.util.Scanner; public class Main{public static void main(String [] args){Scanner sc = new Scanner(System.in);while(sc.hasNext()){int n = sc.nextInt();int sum = 0;for(int i =0;i<n;i++){sum += sc.nextInt();}System.out.println(sum);}}}/** 7,數組求和 直接求 一直輸入* 1 2 3 4 5 0 0 0 0 0* 6 9 0*/ import java.util.Scanner; public class Main{public static void main(String [] args){Scanner sc = new Scanner(System.in);while(sc.hasNext()){int sum = 0;String[] str = sc.nextLine().split(" ");for(int i=0;i<str.length;i++){sum+=Integer.parseInt(str[i]);}//for(String str:int1){// i+=Integer.valueOf(str);//}System.out.println(sum);}} }/** 字符串1 有個數*5(個數)c d a bb e*a bb c d e*/ import java.util.Scanner; import java.util.Arrays; import java.util.*;public class Main{public static void main(String[] args){Scanner sc = new Scanner(System.in);while (sc.hasNext()) {int num = sc.nextInt();String[] s = new String[num];for(int i = 0;i < num; i++){s[i] = sc.next();}Arrays.sort(s);System.out.println(String.join(" ",s));}}}/** 字符串2 一直輸入*a c bbf ddddnowcoder**a bb cdddd fnowcoder*/ import java.util.*;import java.util.Scanner; public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);while(in.hasNext()) {String[] strs = in.nextLine().split(" ");Arrays.sort(strs);// System.out.println(String.join(" ",s));String res = "";for(String s : strs)res += s + " ";System.out.println(res.trim());}} }/** 字符串3 一直輸入 ,號間隔 a,c,bbf,ddddnowcoder*a,bb,cdddd,fnowcoder*/import java.util.*; import java.util.Scanner; public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);while(in.hasNext()) {String[] strs = in.nextLine().split(",");Arrays.sort(strs);// System.out.println(String.join(",",s));String res = "";for(String s : strs)res += s + ",";System.out.println(res.substring(0, res.length() - 1));}} }總結
以上是生活随笔為你收集整理的【小技巧】【牛客网】【JAVA】在线输入输出练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 中文 api (72)
- 下一篇: 常用正则表达式,持续更新