【发布时间】:2019-08-29 04:51:37
【问题描述】:
我正在尝试解决 codechef 问题,我能够在 IDE 和自定义输入中获得输出,当我尝试使用那里的输入运行时,它给了我错误
问题链接: https://www.codechef.com/problems/HS08TEST
代码:
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.text.DecimalFormat;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner input = new Scanner(System.in);
int numberOne = input.nextInt();
float numberTwo = input.nextFloat();
float reduction = 0;
float result = 0;
DecimalFormat df2 = new DecimalFormat(".00");
if(numberOne > 0 && numberOne <= 2000 & numberTwo >= 0 && numberTwo <= 2000){
if(numberOne % 5 == 0){
reduction = (float)numberOne+(0.50f);
if(reduction <= numberTwo){
result = numberTwo-reduction;
System.out.println(df2.format(result));
}
if(reduction > numberTwo){
System.out.println(df2.format(numberTwo));
}
}
else{
System.out.println(df2.format(numberTwo));
}
}
}
}
错误:
线程“main”中的异常 java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:862) 在 java.util.Scanner.next(Scanner.java:1485) 在 java.util.Scanner.nextInt(Scanner.java:2117) 在 java.util.Scanner.nextInt(Scanner.java:2076) 在 Codechef.main(Main.java:14)
【问题讨论】:
-
这是由于
nextInt和/或nextFloat无法满足基于输入的要求。您可能需要改用nextLine并测试手动将Strings 转换为int和double -
抱歉,nextLine 也无法正常工作
标签: java