发布网友 发布时间:2024-10-18 22:53
共5个回答
热心网友 时间:9分钟前
替你改了一下,测试通过。以下是代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int[] t1 = { 123, 456, 7, 336, 775, 566 };
int[] t2 = { 456, 7, 888, 558, 666, 221 };
int[] t3 = { 9000, 5000, 6000, 10000, 12000, 7000 };
System.out.print("");
int length = 9999;
int i = 0;
while (cin.hasNext() && i < length){
String str = cin.nextLine();
String[] substr = str.split(" ");
int a1 = Integer.parseInt(substr[0]);
if (substr.length > 1) {
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");
i--;
}
else {
i = a1;
}
}
}
}
热心网友 时间:6分钟前
每行固定两个数的话就不必切分了
修改了一下你的代码,测一下吧
比如用 :
2
123 123
456 7
祝君在ACM的路上渐行渐远啊渐行渐远……
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int[] t1 = { 123, 456, 7, 336, 775, 566 };
int[] t2 = { 456, 7, 888, 558, 666, 221 };
int[] t3 = { 9000, 5000, 6000, 10000, 12000, 7000 };
while (cin.hasNext()) {
int n = cin.nextInt(); // 输入的第一个数,决定循环次数
for (int i = 0; i < n; i++) {
int a1 = cin.nextInt();
int b = cin.nextInt();
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");
}
}
}
}
热心网友 时间:5分钟前
因为是数组越界了,你输入1个3数组长度为1,就没有substr[1],需要加个判断
String str = cin.nextLine();
String[] substr = str.split(" ");
if(substr.length==1){
continue;
}
如果要显示的话,在continue前,加System.out.println("请输入"+substr[0]+"组数据");
热心网友 时间:3分钟前
是不是想要实现这个效果
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int[] t1 = { 123, 456, 7, 336, 775, 566 };
int[] t2 = { 456, 7, 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<count;i++) {
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");
}
System.out.println("程序结束");
}
热心网友 时间:5分钟前
这部分这么改就行了,你输入错误就是因为输入第一个整数后会敲回车,这样你循环里的第一个cin.getNextLine()就会接收这个回车而不是你的输入,所以程序就错了
这样改下就好了:
System.out.print("");
int time = cin.nextInt();
cin.nextLine();//这里加上这句是为了吸收你上句输入完整数后的回车
while (time-- != 0) {
String str = cin.nextLine();
String[] substr = str.split(" ");