Java IDE中的代码正确,但CodeChef中出现错误

Java IDE中的代码正确,但CodeChef中出现错误,java,Java,我试图解决codechef问题,我能够在IDE中获得输出,也可以使用自定义输入,当我尝试使用这些输入运行时,它会给我错误 问题链接: 代码: /* 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

我试图解决codechef问题,我能够在IDE中获得输出,也可以使用自定义输入,当我尝试使用这些输入运行时,它会给我错误

问题链接:

代码:

    /* 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.*;
导入java.lang.*;
导入java.io.*;
导入java.text.DecimalFormat;
/*只有当类是公共的时,类的名称才必须是“Main”*/
类Codechef
{
公共静态void main(字符串[]args)引发java.lang.Exception
{
扫描仪输入=新扫描仪(System.in);
int numberOne=input.nextInt();
float numberTwo=input.nextFloat();
浮动减少=0;
浮动结果=0;
DecimalFormat df2=新的DecimalFormat(“.00”);

如果(numberOne>0&&numberOne=0&&numberTwo您没有占用输入值之间的空间

只需使用nextLine读取第一行,然后相应地拆分并解析数字

该“错误”是由于输入无法解析为所需类型造成的(即,
Scanner
无法将输入解析为
int
float

“一个”解决方案是获取输入并手动解析它。您可以使用
nextLine
并在其上运行另一个
扫描仪,或者在公共分隔符上拆分,或者您可以简单地使用
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));
            }
        }

    }

}
导入java.text.DecimalFormat;
导入java.util.Scanner;
类Codechef{
公共静态void main(字符串[]args)引发java.lang.Exception{
扫描仪输入=新扫描仪(System.in);
String element=input.next();//下一个值直到下一个空格或新行。。。
int numberOne=Integer.parseInt(元素);
element=input.next();//下一个值直到下一个空格或新行。。。
float numberTwo=float.parseFloat(元素);
浮动减少=0;
浮动结果=0;
DecimalFormat df2=新的DecimalFormat(“.00”);

如果(numberOne>0&&numberOne=0&&numberTwo一件简单的事情对我有效。。。。 我只是用try-and-catch来包围代码

最终工作代码…

/* 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){

    }
    }

}
/*包codechef;//不要放置包名*/
导入java.util.*;
导入java.lang.*;
导入java.io.*;
/*只有当类是公共的时,类的名称才必须是“Main”*/
类Codechef
{
公共静态void main(字符串[]args)引发java.lang.Exception
{
试一试{
int n,和=0;
扫描仪s=新的扫描仪(System.in);
n=s.nextInt();
int a[]=新的int[n];
对于(int i=0;i对于(int j=0;j当我尝试使用nextLine读取第一行时,它会给我错误:::因为nextLine用于读取字符串..我猜是的。在IDE中使用一行输入(如codechef所示)进行再次测试,而不是实际输入两个相同问题的值。可能的codechef使用main方法的参数,而不是standard inputi用于将命令行中的参数作为…args[0]…但无效这是由于
nextLine
和/或
nextFloat
无法满足基于输入的要求造成的。您可能需要改用
nextLine
并测试将
String
s转换为
int
double
manuallysorry,但nextLine也无法工作