[英]Can't able to run program in codechef ide . get CodeChef NZEC error
[英]Code correct in IDE but gives error in CodeChef
我正在尝试解决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));
}
}
}
}
错误:
java.util.Scanner.next(Scanner.java:1485)处java.util.Scanner.throwFor(Scanner.java:862)处的线程“ main”中的java.util.NoSuchElementException Codechef.main(Main.java:14)的java.util.Scanner.nextInt(Scanner.java:2076)的Scanner.java:2117)
“错误”是由于无法将输入解析为所需的类型所致(例如, Scanner
不能将输入解析为int
或float
)
“ A”解决方案是获取输入并手动解析它。 您可以使用nextLine
并在其上运行另一个Scanner
,或在公共定界符上拆分,或者可以简单地使用next
,例如...
import java.text.DecimalFormat;
import java.util.Scanner;
class Codechef {
public static void main(String[] args) throws java.lang.Exception {
Scanner input = new Scanner(System.in);
String element = input.next(); // Next value up to the next space or new line...
int numberOne = Integer.parseInt(element);
element = input.next(); // Next value up to the next space or new line...
float numberTwo = Float.parseFloat(element);
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));
}
}
}
}
假设通常在单行上提供输入,但是此方法将允许您处理两个单独的输入。 但是,如果不知道确切的输入是什么,很难提供更精确的解决方案
您不会浪费输入值之间的空间。
只需使用nextLine读取第一行,然后相应地拆分并解析数字
一个简单的事情对我有用....我只是通过尝试和捕获来包围代码....
最终工作代码...
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* 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
{
try{
int n, sum = 0;
Scanner s = new Scanner(System.in);
n = s.nextInt();
int a[] = new int[n];
for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
int largest=0;
int element=0;
for(int i = 0; i < n; i++){
for(int j=0;j<n;j++){
element=a[i]%a[j];
if(largest<element){
largest=element;
}
}
}
System.out.println(largest);
}
catch(Exception e){
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.