import java.io.*;
import java.util.Scanner;
public class TestClass {
public static void main(String[] args) {
int t;
Scanner ip = new Scanner(System.in);
ScoreCalculation ob = new ScoreCalculation();
t=ip.nextInt();
ob.score(t); }
}
class ScoreCalculation {
void score(int t)
{
Scanner ipu=new Scanner(System.in);
int i,cum1=0,cum2=0,lead1=0,lead2=0,one,two;
for(i=1;i<=t;i++)
{
//if(ipu.hasNextInt())
one=ipu.nextInt();
//if(ipu.hasNextInt())
two=ipu.nextInt();
cum1+=one;
cum2+=two;
if(cum1>cum2)
{
if(lead1<(cum1-cum2))
lead1=(cum1-cum2);
}
else
{
if(lead2<(cum2-cum1))
lead2=(cum2-cum1);
}
}
if(lead1>lead2)
System.out.println("1 "+lead1);
else
System.out.println("2 "+lead2);
}
}
这是我的代码,当我转换为hasNextInt它仍然显示错误 TestClass名称和两个类的使用必须
错误是
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at ScoreCalculation.score(TestClass.java:20)
at TestClass.main(TestClass.java:9)
hasint
的错误TestClass.java:20: error: incompatible types: boolean cannot be converted to int
one=ipu.hasNextInt();
^
TestClass.java:22: error: incompatible types: boolean cannot be converted to int
two=ipu.hasNextInt();
答案 0 :(得分:0)
首先,hasNextInt()返回一个布尔对象,因此你不能将它转换为Int,这在c ++中是允许的,但在java中不允许。
其次,你是否对for循环进行了追踪?我认为当您读取循环外部的扫描仪时,它会在第二个循环中崩溃。如果t参数大于1,那么它应该崩溃,就像它读取一旦没有更多的整数读取一样。