-3

我真的是 Java 的新手,但我正在尝试使用这个程序。这是一个执行基本数学计算的程序,但需要用户输入。

import java.io.*;  
import java.util.Scanner;  

public class Math 
{
public static void main(String[] args) 
    {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

        Scanner in = new Scanner(System.in);  

        int sa1, sa2, ss1, ss2, sm1, sm2, s;  
    boolean c = false;  
    double sd1, sd2;  

        while(c==false)  
        {
        System.out.print("Do you want to: \n[1] Add. \n[2] Subtract.  \n[3] Multiply.  \n[4] Divide. \n[5] Exit \nPlease insert an option number below and press enter:  ");  
        s = in.nextInt();  

        if (s==1)  
            {
            System.out.print("You have chosen option 1 \nPlease enter your first number: ");  
            sa1 = in.nextInt();  
            System.out.print("Please enter your second number: ");  
            sa2 = in.nextInt();  
            System.out.print(sa1+ " + " +sa2+ " = " +(sa1+sa2));  
            }
        if (s==2)  
            {
            System.out.print("You have chosen option 2 \nPlease enter your first number: ");  
            ss1 = in.nextInt();  
            System.out.print("Please enter your second number: ");  
            ss2 = in.nextInt();  
            System.out.println(ss1+ " - " +ss2+ " = " +(ss1-ss2));  
            }
        if (s==3)
            {
            System.out.print("You have chosen option 3 \nPlease enter your first number: ");  
            sm1 = in.nextInt();  
            System.out.print("Please enter your second number: ");  
            sm2 = in.nextInt();  
            System.out.println(sm1+ " x " +sm2+ " = " +(sm1*sm2));  
            }
        if (s==4)       
            {       
            System.out.print("You have chosen option 4 \nPlease enter your first number: ");  
            sd1 = in.nextDouble();  
            System.out.print("Please enter your second number: ");  
            sd2 = in.nextDouble();  
            System.out.println(sd1+ " divided by " +sd2+ " = " +(sd1/sd2));    
            }       
            if(s>=6)  
            {
            System.out.println("You have entered an incorrect option");    
            }
        System.out.print("Would you like to try again? (Y/N): ");   
        String ans = in.nextLine();//prob with this line   
        char ans1 = ans.charAt(0);//or this line  
            if (ans1=='N' || ans1=='n')  
            {
            c = true;   
            }
            if(s==5)   
            {
            c = true;  
            }       
        } 

    }

}

当我编译它时,我收到一个错误:线程“主”java.lang.StringIndexOutOfBoundsException 中的异常:字符串索引超出范围:0 at java.lang.String.charAt(Unknown Source) at Math.main(Math.java:58 )

我试过到处寻找答案。谁能帮我这个?它需要能够从用户那里读取“Y”或“N”。

4

1 回答 1

1

运行它时会出现异常。不是在编译时。错误消息说:

您正在尝试获取字符串索引 0 处的字符,但该字符串没有索引 0。此异常发生在 Main.java 的第 58 行。

在索引 0 处没有字符的字符串是空字符串。

这可能会发生,因为您从 SYStem.in() 读取的最后一件事是一个数字,并且读取一个数字不会消耗后面的行尾。在读取用户的“是”或“否”之前添加对 readLine() 的调用。并且不要假设用户会做你告诉他做的事情。这几乎不是真的。验证您读取的输入。

于 2012-12-29T14:10:56.103 回答