【问题标题】:Code correct in IDE but gives error in CodeChefIDE 中的代码正确,但 CodeChef 中出现错误
【发布时间】: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 转换为intdouble
  • 抱歉,nextLine 也无法正常工作

标签: java


【解决方案1】:

“错误”是由于输入无法解析为所需类型(即Scanner 无法将输入解析为intfloat

“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));
            }
        }

    }

}

这假定输入通常在一行中提供,但这种方法将允许您处理两个单独的输入。但是如果不确切知道输入是什么,就很难提供更精确的解决方案

【讨论】:

    【解决方案2】:

    您没有占用输入值之间的空间。

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

    【讨论】:

    • 当我尝试使用 nextLine 读取第一行时,它给了我错误 :::: 因为 nextLine 用于读取字符串..我猜
    • 是的。使用 codechef 显示的一行输入在您的 IDE 中再次测试,而不是实际输入两个值
    • 面临同样的问题
    • codechef 可能使用 main 方法的 args 而不是标准输入
    • 我以前从命令行取参数为 ....args[0] ... 但它是无效的
    【解决方案3】:

    一件简单的事情对我有用.... 我只用 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){
    
        }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-23
      • 1970-01-01
      • 2018-11-12
      • 1970-01-01
      • 2013-09-14
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      • 2021-11-25
      相关资源
      最近更新 更多