之前学c++的时候总是觉得poj上提交代码wa,re特别烦,
学了Java想练习一下基本的语法发现自己有点强迫症...就是一定要一个系统来评测一下
想想还有点小激动呢...
言归正传:
第一个compile error版的代码:
import java.util.Scanner;
public class Modify
{
static int n;
static int sum;
static int speed[];
static int time[];
static public void main(String args[])
{
speed = new int[20];
time = new int[20];
time[0] = 0;
while(true)
{
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
//System.out.println("the value of n is " + n);
if(n == -1)
break;
sum = 0;
for(int i = 1; i <= n; ++i)
{
speed[i] = sc.nextInt();
time[i] = sc.nextInt();
sum += (time[i] - time[i-1]) * (speed[i]);
}
System.out.println(sum + " miles");
}
}
}
ce原因:
看了下提示信息说Modify这个类的定义不正确,觉得可能是评测系统对类名有什么要求,在下面的链接里找到答案了
http://www.java3z.com/cwbwebhome/article/article19/res008.html
re版代码:
import java.util.Scanner;
public class Main
{
static int n;
static int sum;
static int speed[];
static int time[];
static public void main(String args[])
{
time[0] = 0;
while(true)
{
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
//System.out.println("the value of n is " + n);
if(n == -1)
break;
sum = 0;
for(int i = 1; i <= n; ++i)
{
speed[i] = sc.nextInt();
time[i] = sc.nextInt();
sum += (time[i] - time[i-1]) * (speed[i]);
}
System.out.println(sum + " miles");
}
}
}
re原因:
java在声明数组的时候没有为其分配空间,所以直接对time[0]赋值会产生exception,在本地就测出来了
re版2:
import java.util.Scanner;
public class Main
{
static int n;
static int sum;
static int speed[];
static int time[];
static public void main(String args[])
{
speed = new int[20];
time = new int[20];
time[0] = 0;
while(true)
{
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
//System.out.println("the value of n is " + n);
if(n == -1)
break;
sum = 0;
for(int i = 1; i <= n; ++i)
{
speed[i] = sc.nextInt();
time[i] = sc.nextInt();
sum += (time[i] - time[i-1]) * (speed[i]);
}
System.out.println(sum + " miles");
}
}
}
re原因:
暂时还不清楚,在本地测是没有问题的,同时也没有什么越界的问题;根据下面的ac代码,觉得问题出在while循环上
ac代码:
import java.util.Scanner;
public class Main
{
static int n;
static int sum;
static int speed[];
static int time[];
static public void main(String args[])
{
speed = new int[20];
time = new int[20];
time[0] = 0;
Scanner sc = new Scanner(System.in);
while((n = sc.nextInt()) != -1)
{
sum = 0;
for(int i = 1; i <= n; ++i)
{
speed[i] = sc.nextInt();
time[i] = sc.nextInt();
sum += (time[i] - time[i-1]) * (speed[i]);
}
System.out.println(sum + " miles");
}
}
}