【问题标题】:How would I print out a user input given the certain amount of inputs needed?给定所需的一定数量的输入,我将如何打印出用户输入?
【发布时间】:2021-08-09 21:25:26
【问题描述】:

如果需要一定数量的输入,我将如何打印出用户输入?

import java.util.Scanner;
public class SilentAuction {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int amount = sc.nextInt();
        for (int i = 1; i <= amount; i++) {
            System.out.println();//need to write "int sm = sc.nextInt;" and "String s = sc.nextLine;"
        }
    }
}

【问题讨论】:

  • System.out.println(sc.nextInt());?
  • 专业提示:使用摘要标题而不是很长的标题,并将其余信息放在正文中
  • 当你说,输入量。您是指用户输入的金额还是输入的字符/数字的数量?我看到班级名称是 SilentAuction。那么,您是否尝试将先前输入的金额与当前金额进行比较?还是您想找到最高金额的出价?

标签: java loops


【解决方案1】:

如果你混合使用nextInt()nextLine() 方法,你会遇到common issue。您最好使用nextLine() 并尝试解析内容。

public class SilentAuction {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int amount = Integer.parseInt(sc.nextLine());
        for (int i = 0; i < amount; i++) {
            String s = sc.nextLine();
            int sm = Integer.parseInt(sc.nextLine());
            System.out.println(s + " : " + sm);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 2018-05-19
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多