java - "main"java.util.InputMismatchException 处出现异常?

标签 java error-handling compiler-errors syntax-error runtime-error

[
import java.io.*;
import java.util.*;
import java.text.*;
import java.io.*;
import java.math.*;
import java.util.regex.*;

public class Solution 
{
static double rounnd(double r)
{
    double t=r;
    double x = Math.round(t*100);
    x=x/100;
    t=x;
    return t;
}

public static double op(int w,String sent,double t2) throws Exception
{
    String left="qwert",right="yuiop";
    int lt=1,rt=1,lastturn=0;

    double t3=0;

    Scanner sc=new Scanner (System.in);
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in) );

    lt=1;rt=1;lastturn=0;
    for(int j = 0; j < sent.length();j ++)
    {
        left="qwert";right="yuiop";
        t2=0;
        lt=lastturn==1?lt:1;
        rt=lastturn==2?rt:1;
        char ch = sent.charAt(j);
        if (left.contains(Character.toString(ch))){
            t3=t3+(0.2*lt);
            lt=lt*2;
            lastturn=1;

        }
        else if (right.contains(Character.toString(ch))){
            t3=t3+(0.1*rt);
            rt=rt*2;
            lastturn=2;

        }

    }
    return t3;
}

public static void main(String args[])throws Exception {

    Scanner sc=new Scanner (System.in);
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in) );

    String left="qwert",right="yuiop";
    int lt=1,rt=1,lastturn=0;

    double t2[],t3[];
    String sent[];

    //System.out.println("Enter Numumber Of lines");
    int t=sc.nextInt();
    int w[]=new int[t];
    t2=new double[t];
    t3=new double [t];
    sent = new String[t];
    for(int i=0;i<t;i++){            
        w[i]=sc.nextInt();
        //br.readLine();
        sent[i] = br.readLine();
        t2[i] = sc.nextDouble();
        t3[i]=(0.3*(w[i]-1));
    }
    for(int i=0;i<t;i++){
        String arr[]=sent[i].split(" ");
        String c[]=new String[arr.length];
        for(int j=0;j<w[i];j++)
            c[j]="";
        int r=0;
        for(int j = 0; j < arr.length ; j ++){
            boolean b=false;
            for(int k=0;k<arr.length;k++)//were tyui owiq 5.9 qwer yuio qiwo 5.1 type type were type were type 7.5
            {
                if(c[k].equals(arr[j]))
                    b=true;

            }
            if(b)
            {
                double q=(op(w[i],arr[j],t2[i])/2);
                t3[i]=t3[i]+q;
            }
            else {
                double q=op(w[i],arr[j],t2[i]);
                t3[i]=t3[i]+q;
            }
            c[r++]=arr[j];
        }
    }
    for(int i=0;i<t;i++){
        t3[i]=rounnd(t3[i]);
        String str=String.valueOf((t3[i]));
        str=(str.length()-str.indexOf("."))==3?str:str+"0";
        System.out.println((t3[i]<t2[i]?"WORK HARDER, "+str:t3[i]==t2[i]?"GOOD, "+str:t3[i]>t2[i]?"GREAT JOB, "+str:""));

    }
}
}][1]

https://www.hackerrank.com/contests/iiitv-bitsetgo-18/challenges/can-you-even-type 对于错误,请参阅图像代码,是否存在问题的链接,是否存在,请帮忙,发生了某种异常,程序在bluej idk上完美运行,网站上发生了什么/image/AOnc6.png忽略前面的文字我不知道需要输入多少内容才能有资格提交问题,因为我已经提供了所有详细信息。我不知道我输入了什么。

最佳答案

根据此处找到的文档:

https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextDouble()

InputMismatchException - if the next token does not match the Float regular expression, or is out of range

在这一行中,您尝试使用扫描仪读取 double 值

sent[i] = br.readLine();

但是,您所附的图片显示提供的输入可以是整数甚至字符串。似乎你从来没有检查你收到的输入是 double 还是数字。我建议你努力解决这个问题。

还有一个旁注,由于很多原因,这是极其错误的。请避免这样做:

sent = new String[t];

关于java - "main"java.util.InputMismatchException 处出现异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55209125/

相关文章:

java - Ipad 应用程序 - 访问基于 java 的 Web 服务器

angular - 错误: Uncaught (in promise) - Angular error - NullInjectorError:

java - 为什么 Java 不解析这个 <generic> 方法签名?

c# - List <myCustomClass>的集合初始值设定项无法正常工作,给出 “; expected”编译错误。为什么?

c - 当 dlopen one 时,它​​的符号没有被主符号覆盖,为什么?

java - 错误: variable might not have been initialized & if statement

java - 如何在 Spring Boot 中扩展类而不更改 MySQL 架构?

java - 部署到实例时 EJB 计时器服务不可用

PHP 常量不存在 : Notice

ruby-on-rails - 如何在Rails开发环境中关闭红色/灰色错误帮助页面工具?