第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在每行中使用多個輸入的多行

在每行中使用多個輸入的多行

Qyouu 2023-06-04 16:53:43
我的要求是在多行上從用戶那里獲取多個輸入。每行表示輸入的含義為:Power Energy Idea Strength每行僅由一個空格分隔。輸入:2 4 5 73 4 8 1 4 2 3 6輸出包括列表:List<Integer> power = {2,3,4}List<Integer> energy = {4,4,2}List<Integer> idea = {5,8,3}List<Integer> strength = {7,1,6}首選解決方案是 Java,但如果可以提供 Java-8 解決方案,我會喜歡的。到目前為止,我已經(jīng)嘗試過但沒有運氣:        String input = "";        Scanner keyboard = new Scanner(System.in);        String line;        while (keyboard.hasNextLine()) {            line = keyboard.nextLine();            if (line.isEmpty()) {                break;            }            input += line + "\n";        }        System.out.println(input);我能夠讀取多行輸入,但無法將它們作為單獨的類別保存在List.
查看完整描述

2 回答

?
猛跑小豬

TA貢獻1858條經(jīng)驗 獲得超8個贊

您可以簡單地拆分行并將值放在單獨的列表中:


? ? line = keyboard.nextLine();

? ? if (line.isEmpty()) {

? ? ? ? break;

? ? }

? ? String arr[] = line.split(" ");// spiting the line based on space

? ? if (arr.length==4) { // check if length is 4 as you are expecting

? ? // use Integer.valueOf() to convert from string to Integer

? ? ? ? power.add(arr[0]);

? ? ? ? energy.add(arr[1]);

? ? ? ? idea.add(arr[2]);

? ? ? ? strength.add(arr[3]);

? ? }else {

? ? ? ? //throw exception or break

? ? }

? ? input += line + "\n";

}

查看完整回答
反對 回復 2023-06-04
?
瀟瀟雨雨

TA貢獻1833條經(jīng)驗 獲得超4個贊

您可以使用此程序輸入任意多的值:


List<Integer> power = new ArrayList<>();

List<Integer> energy = new ArrayList<>();

List<Integer> idea = new ArrayList<>();

List<Integer> strength = new ArrayList<>();


Scanner sc = new Scanner(System.in);

while(true) {

  String s = sc.nextLine();

  if(s.equals("exit")) break;

  String args = s.split(" ");

  if(args.length >= 4) {

    power.add(Integer.valueOf(args[0]));

    energy.add(Integer.valueOf(args[1]));

    idea.add(Integer.valueOf(args[2]));

    strength.add(Integer.valueOf(args[3]));

  }

}


查看完整回答
反對 回復 2023-06-04
  • 2 回答
  • 0 關(guān)注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號