修复N崩溃(我无法修复它或它会崩溃)循环和中断java

时间:2014-04-11 20:19:37

标签: java eclipse loops if-statement break

我试图解决这个问题,每次我修复它都会破坏它。我想要做的是创建一个计算器,并具有基本操作,并询问您是否需要再次解决另一个方程式。如果你输错了,它甚至会要求你输入操作。

代码就是这个

import java.util.Scanner;
import java.lang.Thread;

public class Calculator

    int B1;
int C1;
int D1; // error here
@SuppressWarnings({ "resource" })
public static void main(String[] args) throws Exception {
    for(;;)
            System.out.println("How May i asist you with your calculation."); {
            System.out.println("So what opperation do you want to use. ");
            Scanner A1 = new Scanner(System.in);
            String in = A1.nextLine();
                if (in.equals("+")){
                    System.out.println("Enter the first Number. ");
                    Scanner Z = new Scanner(System.in);
                    int B1 = Z.nextInt();
                    System.out.println("Enter The last Number. ");
                    Scanner Y = new Scanner(System.in);
                    int C1 = Y.nextInt();
                    int D1 = B1 + C1;
                    System.out.println(" you answer is " + D1 + ".");}
                    Thread.sleep(1000);
                    System.out.println("Again?");
                    Scanner A = new Scanner(System.in);
                    String B = A.nextLine();
                        if (B.equals("Yes")){
                            System.out.println("Ok");
                        if (B.equals("No"))
                            System.out.println("Ok. ill see you later");
                            break;}
                else if (in.equals("-")){
                    System.out.println("Enter the first Number. ");
                    Scanner Z = new Scanner(System.in);
                    int B1 = Z.nextInt();
                    System.out.println("Enter The last Number. ");
                    Scanner Y = new Scanner(System.in);
                    int C1 = Y.nextInt();
                    int D1 = B1 - C1;
                    System.out.println(" you answer is " + D1 + ".");}
                    Thread.sleep(1000);
                    System.out.println("Again?");
                    Scanner C = new Scanner(System.in);
                    String D = C.nextLine();
                        if (C.equals("Yes")){
                            System.out.println("Ok");}
                        if (C.equals("No"))
                            System.out.println("Ok. ill see you later");
                            break;{
                if (in.equals("*")){
                    System.out.println("Enter the first Number. ");
                    Scanner Z = new Scanner(System.in);
                    int B1 = Z.nextInt();
                    System.out.println("Enter The last Number. ");
                    Scanner Y = new Scanner(System.in);
                    int C1 = Y.nextInt();
                    int D1 = B1 * C1;
                    System.out.println(" you answer is " + D1 + ".");}
                    Thread.sleep(1000);
                    System.out.println("Again?");
                    Scanner E = new Scanner(System.in);
                    String F = E.nextLine();
                        if (E.equals("Yes")){
                            System.out.println("Ok");}
                        if (E.equals("No"))
                            System.out.println("Ok. ill see you later");
                            break;{
                if (in.equals("/")){
                    System.out.println("Enter the first Number. ");
                    Scanner Z = new Scanner(System.in);
                    int B1 = Z.nextInt();
                    System.out.println("Enter The last Number. ");
                    Scanner Y = new Scanner(System.in);
                    int C1 = Y.nextInt();
                    int D1 = B1 / C1;
                    System.out.println("You answer is " + D1 + ".");}
                    Thread.sleep(1000);
                    System.out.println("Again?");
                    Scanner G = new Scanner(System.in);
                    String H = G.nextLine();
                        if (G.equals("Yes")){
                            System.out.println("Ok");}
                        if (G.equals("No"))
                            System.out.println("Ok. ill see you later");
                            break;{
                    if (!in.equals("+"+"-"+"*"+"/"))
                        System.out.print("That's Not a valid operation.");
                        Thread.sleep(1000);
                        System.out.println("Again?");
                        Scanner I = new Scanner(System.in);
                        String J = I.nextLine();
                            if (I.equals("Yes")){
                                System.out.println("Ok");}
                            if (I.equals("No"))
                                System.out.println("Ok. ill see you later"); 
                                break; { //error here

我不知道错误在哪里,但需要修复它。 请帮我 我没吃午饭 我没有休息一下学校 我在家里花了2个小时试图解决这个问题。 但我不能。 请帮帮我

1 个答案:

答案 0 :(得分:1)

您的括号有很多问题。确保在创建循环时用循环括号{}包装循环内容。当您编写条件时,请确保将每个部分用大括号括起来。

这是修正程序的前半部分,我将留给你写下剩下的部分。

import java.util.Scanner;
import java.lang.Thread;

public class Calculator {   // missing opening bracket

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

        int B1;
        int C1;
        int D1;     

        for(;;) { // missing opening bracket
            System.out.println("How May i asist you with your calculation."); // removed extra opening bracket here
            System.out.println("So what opperation do you want to use. ");
            Scanner scanner = new Scanner(System.in);    // you only need one scanner
            String in = scanner.nextLine();
            String response;

            if (in.equals("+")){
                System.out.println("Enter the first Number. ");
                B1 = scanner.nextInt();     // only declare variables once
                System.out.println("Enter The last Number. ");
                C1 = scanner.nextInt();
                D1 = B1 + C1;
                System.out.println(" you answer is " + D1 + ".");
                Thread.sleep(1000);
                System.out.println("Again?");

                scanner.nextLine();  // clear out the previous newline
                response = scanner.nextLine();
                if (response.equals("Yes")) {
                    System.out.println("Ok");
                }
                if (response.equals("No")) {   // wrapped in brackets
                    System.out.println("Ok. ill see you later");
                    break;
                }
            }
            else if (in.equals("-")){
                System.out.println("Enter the first Number. ");
                B1 = scanner.nextInt();
                System.out.println("Enter The last Number. ");
                C1 = scanner.nextInt();
                D1 = B1 - C1;
                System.out.println(" you answer is " + D1 + "."); 
                Thread.sleep(1000);
                System.out.println("Again?");

                scanner.nextLine();  // clear out the previous newline
                response = scanner.nextLine();
                if (response.equals("Yes")){
                    System.out.println("Ok");
                }
                if (response.equals("No")) {
                    System.out.println("Ok. ill see you later");
                    break;
                }

            }   // changed opening bracket to closing bracket

        }
    }
}