当前位置: 首页 > 面试题库 >

如何在Java中读取多行输入

饶元章
2023-03-14
问题内容

我们的教授让我们用Java进行一些基本的编程,他提供了一个网站以及所有可以注册和提交问题的内容,因为今天我需要做一个例子,我觉得自己处在正确的轨道上,但是我无法找出其余的。这是实际的问题:

**Sample Input:**
10 12
10 14
100 200

**Sample Output:**
2
4
100

这是到目前为止我得到的:

public class Practice {

    public static int calculateAnswer(String a, String b) {
        return (Integer.parseInt(b) - Integer.parseInt(a));
    }

    public static void main(String[] args) {
        System.out.println(calculateAnswer(args[0], args[1]));
    }
}

现在,我总是会得到答案,2因为我正在阅读单行,如何考虑所有行?谢谢

由于某些奇怪的原因,每次我要执行时都会出现此错误:

C:\sonic>java Practice.class 10 12
Exception in thread "main" java.lang.NoClassDefFoundError: Fact
Caused by: java.lang.ClassNotFoundException: Fact.class
        at java.net.URLClassLoader$1.run(URLClassLoader.java:20
        at java.security.AccessController.doPrivileged(Native M
        at java.net.URLClassLoader.findClass(URLClassLoader.jav
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248
Could not find the main class: Practice.class.  Program will exit.

无论我使用什么版本的答案,都会出现此错误,该怎么办?

但是,如果我在Eclipse中以“运行方式>运行配置->程序参数”运行它

10 12
10 14
100 200

我没有输出

编辑

我已经取得了一些进展,一开始我遇到了编译错误,然后是运行时错误,现在我得到了错误的答案,所以任何人都可以帮我解决一下这是怎么回事:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class Practice {

    public static BigInteger calculateAnswer(String a, String b) {
        BigInteger ab = new BigInteger(a);
        BigInteger bc = new BigInteger(b);
        return bc.subtract(ab);
    }

    public static void main(String[] args) throws IOException {
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); 
        String line;

        while ((line = stdin.readLine()) != null && line.length()!= 0) { 
            String[] input = line.split(" "); 
            if (input.length == 2) { 
                System.out.println(calculateAnswer(input[0], input[1])); 
            } 
        } 
    }
}

问题答案:

我终于得到了它,无论出于何种原因都被提交了13次,第14位“法官”接受了我的回答,这是:

import java.io.BufferedInputStream;
import java.util.Scanner;

public class HashmatWarrior {

    public static void main(String args[]) {
        Scanner stdin = new Scanner(new BufferedInputStream(System.in));
        while (stdin.hasNext()) {
            System.out.println(Math.abs(stdin.nextLong() - stdin.nextLong()));
        }
    }
}


 类似资料:
  • 问题内容: 我想创建一个Python程序,该程序需要多行用户输入。例如: 如何接收多行原始输入? 问题答案: 要将每一行作为字符串获取,你可以执行以下操作: Python 3:

  • 我正在处理一个程序,我想允许用户在提示时输入多个整数。我曾尝试使用扫描仪,但发现它只存储用户输入的第一个整数。例如: 输入多个整数:1 3 5 扫描仪将仅获取第一个整数1。是否有可能从一行中获取所有3个不同的整数,并在以后使用它们?这些整数是我需要根据用户输入操作的链表中数据的位置。我无法发布我的源代码,但我想知道这是否可行。

  • 我如何使它能够提示用户在一行中输入多个由空格分隔的整数。如果第一个整数为0或小于0,当输入所有整数并按下回车键时,它将打印出“不良输入”。此外,我如何使它,当用户输入一个负数在行的末尾,它将停止输入数字,并使乘法所有他们在一起。 这是我到目前为止所拥有的,但我不确定我做得对不对。

  • 任何帮助都是非常感谢的

  • 问题内容: 我有一个Java应用程序,该应用程序存在一些性能问题,有人建议我以verbose:gc模式运行它。这已经完成,但是我不知道如何解释日志记录。是否可以向我解释这一切的含义,或者为我提供提高性能的建议? 可以在以下位置找到输出日志:http : //pastebin.com/uDNPEGcd 在此先感谢您,马尔滕 问题答案: 在每个gc集合之后立即打印,并打印有关每个世代内存详细信息的详细