![](/img/trans.png)
[英]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.