国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Java 輸入多行字符串或者多個int數值的方法

2024-07-14 08:41:41
字體:
來源:轉載
供稿:網友

控制臺輸入

以下輸入方法用于在控制臺中進行輸入,當輸入當個換行符時,結束輸入。但不適合在做算法題目是使用,可能是因為算法題目中用的是流的方式進行輸入,最后不會輸入多一個換行符,因而無法正確的結束輸入而導致答案錯誤。

import java.util.Scanner;public class Main { public static void main(String[] args) {  inputStr();  inputInteger();  inputIntInLine(); } //每行輸入一個數值,輸入多個數值 public static void inputInteger() {  Scanner scanner = new Scanner(System.in);  String nextLine = scanner.nextLine();  int sum = 0;  while (nextLine != null && !nextLine.equals("")) {   sum += Integer.parseInt(nextLine);   System.out.println(sum);   nextLine = scanner.nextLine();  }  System.out.println("end of input integer"); } // 每行輸入一個字符串,輸入多個字符串 public static void inputStr() {  Scanner scanner = new Scanner(System.in);  String nextLine = scanner.nextLine();  while (nextLine != null && !nextLine.equals("")) {   System.out.println(nextLine);   nextLine = scanner.nextLine();  }  System.out.println("end of input string"); } //輸入多個數值,用空格隔開 public static void inputIntInLine() {  Scanner scanner = new Scanner(System.in);  String str = scanner.nextLine();  String[] numstr = str.split(" ");  int[] nums = new int[numstr.length];  for(int i = 0; i < numstr.length; i ++) {   nums[i] = Integer.parseInt(numstr[i]);  }  for(int n: nums) {   System.out.println(n);  }  System.out.println("end of input int in a line"); }}

做算法題目時用的輸入

以下方法在做算法題目時能夠正確的結束輸入。

import java.util.Scanner;public class Main{ public static void main(String[] args){  Scanner in = new Scanner(System.in);//  while(in.hasNextLine()){   while(in.hasNext()){   String str = in.nextLine();   System.out.println(str);  } }}

以上這篇Java 輸入多行字符串或者多個int數值的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 樟树市| 瑞丽市| 长泰县| 伊春市| 自治县| 正安县| 翁源县| 新郑市| 开平市| 宁陕县| 鹰潭市| 全州县| 洪江市| 临清市| 呈贡县| 门源| 合川市| 蓝山县| 嵩明县| 花莲县| 萍乡市| 宣威市| 三穗县| 沿河| 巴林右旗| 嘉禾县| 利川市| 永川市| 宁化县| 安溪县| 贡山| 谢通门县| 铜川市| 昔阳县| 蓬溪县| 江城| 凤冈县| 茌平县| 缙云县| 莱州市| 兴城市|