异常在“main”java.util.InputMismatchException?

问题描述 投票:1回答:1
[
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网站https://i.stack.imgur.com/AOnc6.png上发生了什么。忽视前面的文字我不知道我需要输入多少才有资格提交我的问题因为我已经提供了所有细节。我输入的是什么。

java error-handling compiler-errors syntax-error runtime-error
1个回答
0
投票

根据此处的文档:

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

InputMismatchException - 如果下一个标记与Float正则表达式不匹配,或者超出范围

在这一行中,您尝试使用Scanner读取double

sent[i] = br.readLine();

然而,您附加的图片显示提供的输入可以是整数甚至是字符串。您似乎永远不会检查您收到的输入是双倍还是数字。我建议你继续努力。

另外一个旁注,由于许多原因,这是非常错误的。请避免这样做:

sent = new String[t];

© www.soinside.com 2019 - 2024. All rights reserved.