gpt4 book ai didi

java - 代码在 IDE 中正确,但在 CodeChef 中出错

转载 作者:行者123 更新时间:2023-11-30 07:41:52 25 4
gpt4 key购买 nike

我正在尝试解决 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));
}
}

}

}

错误:

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 Codechef.main(Main.java:14)

最佳答案

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

“A”解决方案是获取输入并手动解析它。您可以使用 nextLine 并在其上运行另一个 Scanner,或者在公共(public)分隔符上拆分,或者您可以简单地使用 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 - 代码在 IDE 中正确,但在 CodeChef 中出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55566645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com