我正在尝试解决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在 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)
答案 0 :(得分:2)
“错误”是由于无法将输入解析为必需的类型(即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));
}
}
}
}
这假定通常在一行上提供输入,但是此方法将允许您处理两个单独的输入。但是,如果不知道确切的输入是什么,很难提供更精确的解决方案
答案 1 :(得分:1)
您不会浪费输入值之间的空格。
只需使用nextLine读取第一行,然后相应地拆分并解析数字
答案 2 :(得分:0)
一件简单的事情对我有用.... 我只是试图抓住代码的周围。...
最终工作代码...
/* 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){
}
}
}