如何在Java中读取这样的输入?
我有一个意见:如何在Java中读取这样的输入?,java,input,inputstream,user-input,Java,Input,Inputstream,User Input,我有一个意见: aN b c a1 a2 a3 ... aN 例如: 4 3 2 2 1 2 1 (I have here 'a' numbers, a = 4) 5 6 3 3 9 5 7 3 (I have here 'a' numbers, a = 5) 0 0 0 当a、b或c等于0时,我想停止读取输入。我试过这个: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStrea
aN b c
a1 a2 a3 ... aN
例如:
4 3 2
2 1 2 1 (I have here 'a' numbers, a = 4)
5 6 3
3 9 5 7 3 (I have here 'a' numbers, a = 5)
0 0 0
当a、b或c等于0时,我想停止读取输入。我试过这个:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
public class Test {
public static void main(String[] args) throws IOException {
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(converter);
String line = "";
int a = -1, b = -1, c = -1;
LinkedList<Integer> list = new LinkedList<>();
while (a != 0 && b != 0 && c != 0)
{
line = in.readLine();
String tmp[] = line.split(" ");
a = Integer.parseInt(tmp[0]);
b = Integer.parseInt(tmp[1]);
c = Integer.parseInt(tmp[2]);
System.out.println("a = " + a + ", b = " + b + ", c = " + c);
line = in.readLine();
list.clear();
tmp = line.split(" ");
for (int i = 0; i < tmp.length; i++) {
list.add(new Integer(Integer.valueOf(tmp[i])));
}
System.out.println("List = 4 3 2" + list);
}
}
}
即使我输入3个零,我的程序仍然等待输入。如何改进
编辑:
你误会的家伙。我需要第二行读取,因为我需要读取第二行(第四行、第六行)输入…在您的程序读取一行0后,它仍在等待另一行,然后才会停止 最简单的修复方法(可能不是最干净的): 您有两行代码:
line = in.readLine();
因此,当在第一个readLine()
读取3个零时,您仍在等待另一个零。
如果a、b、c中至少有一个为零,则可以更改顺序,或在第一次读取后使用中断。while(true)
while (true)
{
line = in.readLine();
String tmp[] = line.split(" ");
a = Integer.parseInt(tmp[0]);
b = Integer.parseInt(tmp[1]);
c = Integer.parseInt(tmp[2]);
if (a == 0 || b == 0 || c == 0) {
break;
}
System.out.println("a = " + a + ", b = " + b + ", c = " + c);
line = in.readLine();
list.clear();
tmp = line.split(" ");
for (int i = 0; i < tmp.length; i++) {
list.add(new Integer(Integer.valueOf(tmp[i])));
}
System.out.println("List = 4 3 2" + list);
}
{
line=in.readLine();
字符串tmp[]=line.split(“”);
a=整数.parseInt(tmp[0]);
b=整数.parseInt(tmp[1]);
c=Integer.parseInt(tmp[2]);
如果(a==0 | | b==0 | | c==0){
打破
}
System.out.println(“a=“+a+”,b=“+b+”,c=“+c”);
line=in.readLine();
list.clear();
tmp=行分割(“”);
for(int i=0;i
试试这个
while((line=in.readLine()) != null){
String tmp[] = line.split(" ");
a = Integer.parseInt(tmp[0]);
b = Integer.parseInt(tmp[1]);
c = Integer.parseInt(tmp[2]);
if (a == 0 || b == 0 || c == 0) {
break;
}
for (int i = 0; i < tmp.length; i++) {
list.add(new Integer(Integer.valueOf(tmp[i])));
}
}
while((line=in.readLine())!=null){
字符串tmp[]=line.split(“”);
a=整数.parseInt(tmp[0]);
b=整数.parseInt(tmp[1]);
c=Integer.parseInt(tmp[2]);
如果(a==0 | | b==0 | | c==0){
打破
}
for(int i=0;i
试试这个
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class MultipleInputs {
public static void main(String[] args) throws IOException {
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(converter);
while(true){
String line = in.readLine();
List<Integer> numbers = getNumbers(line);
if(numbers.get(0) != 0 && numbers.get(1) != 0 && numbers.get(2) != 0){
String line2 = in.readLine();
List<Integer> numbers2 = getNumbers(line2);
//TODO: do something with numbers2
} else {
break;
}
}
}
private static List<Integer> getNumbers(String line){
String [] strs = line.split(" ");
List<Integer> list = new ArrayList<Integer>();
for(String str : strs){
try{
list.add(Integer.parseInt(str));
}catch (NumberFormatException e) {
// Do nothing, just ignore the sub-string
}
}
return list;
}
}
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入java.util.List;
公共类多输入{
公共静态void main(字符串[]args)引发IOException{
InputStreamReader转换器=新的InputStreamReader(System.in);
BufferedReader in=新的BufferedReader(转换器);
while(true){
String line=in.readLine();
列表编号=获取编号(行);
如果(数字。获取(0)!=0和数字。获取(1)!=0和数字。获取(2)!=0){
字符串line2=in.readLine();
列表编号2=获取编号(第2行);
//TODO:用数字2做点什么
}否则{
打破
}
}
}
私有静态列表GetNumber(字符串行){
字符串[]strs=line.split(“”);
列表=新的ArrayList();
用于(字符串str:strs){
试一试{
add(Integer.parseInt(str));
}捕获(数字格式){
//什么都不做,只需忽略子字符串
}
}
退货清单;
}
}
以下是如何以有助于OP学习的方式回答问题
1) 找出问题所在
“问题是,您编写的程序只测试读取偶数行后是否需要停止。您可以通过查看读取输入的位置来查看这一点。”
2) 就如何解决眼前的问题给出提示
“如果您希望能够在奇数行上停止,则需要在读取奇数行后再进行一次测试。”
3) 指出其他问题
“事实上,您可能还应该检查:
readLine()
调用的结果不是null
(当程序到达输入端时会发生这种情况;例如,如果在Linux上键入^D或在Windows上键入^Z)
- 第一行、第三行、第五行等至少有3个数字
- 还有其他一些事情
如果您不测试这些,当输入错误时,您的程序将异常终止。”
这样回答一个问题(对于有经验的程序员来说)实际上更难/更费力。但是鼓励初学者开始用代码思考是非常重要的。毕竟,这是他们家庭作业的真正目标。(没有得到标记,没有生成完美的解决方案…println()调用对带零的行说了什么?删除第二个:line=in.readLine();while循环应该一次处理一行time@dutt:当a
或b
或c
等于0时,它应该中断一段时间loop@opi:不,也不起作用:/Maybe我应该做完全不同的事情?看起来像是类/家庭作业尝试中断,不起作用,给出:线程“main”中的异常java.lang.NumberFormatException:For input string:“”at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)at java.lang.Integer.parseInt(Integer.java:504)at java.lang.Integer.parseInt(Integer.java:527)at Test.main(Test.java:26)java结果:1
尝试{a=Integer.parseInt(tmp[0]);..c=Integer.parseInt(tmp[2]);}catch(NumberFormatException nfe){a=0;}hm,在我第二次运行这个时工作正常,谢谢!:)请不要对家庭作业编码问题给出笼统的答案。OP不会通过复制和粘贴你的代码学到任何东西。好吧,Stephen,我同意,但她现在知道break语句了。我是新来的,我正在学习规则,谢谢:)请不要对家庭作业编码问题给出笼统的答案。OP不会通过复制和粘贴你的代码来学习任何东西。@StephenC-如果有人想学习,我非常愿意为他们提供代码。每个人都有自己的观点,并且对如何解决一个问题有自己的看法。尽管如此,我所做的只是通过使FOR更加健壮来改进现有代码。这似乎一点也不“盆栽”的。关于如何帮助学生的示例,请参见我的答案。你需要帮助他们理解
while((line=in.readLine()) != null){
String tmp[] = line.split(" ");
a = Integer.parseInt(tmp[0]);
b = Integer.parseInt(tmp[1]);
c = Integer.parseInt(tmp[2]);
if (a == 0 || b == 0 || c == 0) {
break;
}
for (int i = 0; i < tmp.length; i++) {
list.add(new Integer(Integer.valueOf(tmp[i])));
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class MultipleInputs {
public static void main(String[] args) throws IOException {
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(converter);
while(true){
String line = in.readLine();
List<Integer> numbers = getNumbers(line);
if(numbers.get(0) != 0 && numbers.get(1) != 0 && numbers.get(2) != 0){
String line2 = in.readLine();
List<Integer> numbers2 = getNumbers(line2);
//TODO: do something with numbers2
} else {
break;
}
}
}
private static List<Integer> getNumbers(String line){
String [] strs = line.split(" ");
List<Integer> list = new ArrayList<Integer>();
for(String str : strs){
try{
list.add(Integer.parseInt(str));
}catch (NumberFormatException e) {
// Do nothing, just ignore the sub-string
}
}
return list;
}
}