请教一个简单的java程序,差一步就完成了,就是在程序前面加一个数字代表接下来要测试几组数据.

请教一个简单的java程序,差一步就完成了,就是在程序前面加一个数字代表接下来要测试几组数据.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int[] t1 = { 123,456,789,336,775,566 };
int[] t2 = { 456,789,888,558,666,221 };
int[] t3 = { 9000,5000,6000,10000,12000,7000 };
System.out.print("");
while (cin.hasNext()){
String str = cin.nextLine();
String[] substr = str.split(" ");
int a1 = Integer.parseInt(substr[0]);
int b = Integer.parseInt(substr[1]);
if (t1[0] == a1 && t2[0] == b)
System.out.println(t3[0]);
else if (t1[1] == a1 && t2[1] == b)
System.out.println(t3[1]);
else if (t1[2] == a1 && t2[2] == b)
System.out.println(t3[2]);
else if (t1[3] == a1 && t2[3] == b)
System.out.println(t3[3]);
else if (t1[4] == a1 && t2[4] == b)
System.out.println(t3[4]);
else if (t1[5] == a1 && t2[5] == b)
System.out.println(t3[5]);
else
System.out.println("error");
}
}
}
照这样,例如输入:
123 123
456 789
789 789
结果是:
error
5000
error
但是期望的输入方式是:
3
123 123
456 789
789 789
此时3就代表输入了3组数据,输出内容不变.关键是这个代表输入了几组数据的整数应该如何加?我尝试用nextInt失败了,提示在Integer.parseInt附近出错.
美滋滋脆心 1年前 已收到1个回答 举报

西门太zz人 幼苗

共回答了12个问题采纳率:91.7% 举报

是不是想要实现这个效果
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int[] t1 = { 123, 456, 789, 336, 775, 566 };
int[] t2 = { 456, 789, 888, 558, 666, 221 };
int[] t3 = { 9000, 5000, 6000, 10000, 12000, 7000 };
System.out.println("请输入几组数据");
int count = Integer.parseInt(cin.nextLine());
for (int i=0;i

1年前

2
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 16 q. 0.014 s. - webmaster@yulucn.com