亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何在一行中輸入多個不同類型的變量?例如 ->

如何在一行中輸入多個不同類型的變量?例如 ->

臨摹微笑 2022-12-21 10:05:33
使用用 Java 實現的哈希表,我需要獲取 5 個輸入值,其中 3 個是字符串,2 個是整數。用戶必須能夠在一行中寫入所有輸入值。我嘗試了很多不同的方法,主要是使用數組,但我無法在網上找到正確的答案。如果有人可以幫助提供示例代碼或想法,我將不勝感激:)Scanner input = new Scanner(System.in);        Scanner input1 = new Scanner(System.in);        System.out.println("Enter number of people:");        int n = input1.nextInt();        HTable table = new HTable(100);        String a[] = new String[5];        for(int i=0; i<n; i++)        {            a = input.nextLine().split(" ");            table.Insert(a[0], a[1], Integer.parseInt(a[2]), a[3], Integer.parseInt(a[4]));        }這是我嘗試過的最新示例,但它一直在接受輸入并且 FOR() 循環似乎沒有結束。
查看完整描述

1 回答

?
Cats萌萌

TA貢獻1805條經驗 獲得超9個贊

你可以這樣嘗試:


public class Name{

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    System.out.println( "Enter Your Input ");

    while (scanner.hasNext()) 

    { 

        if (scanner.hasNextInt()) {

            System.out.println(" Your Value (Int) : " + scanner.nextInt());

        } else if (scanner.hasNextFloat()) {

            System.out.println(" Your Value (Float) " + scanner.nextFloat());

        } 

        else {

            System.out.println( " String Value(String) " + scanner.next());

        }

    }

查看完整回答
反對 回復 2022-12-21
  • 1 回答
  • 0 關注
  • 204 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號