修复java中的循环和中断(我无法修复它,否则它将崩溃)
我试着修复这个,每次我修复这个,它都会坏掉。我想做的是创建一个计算器,它有基本的运算,并询问你是否需要再次求解另一个方程。如果输入错误,它甚至会要求您输入操作 代码是这样的修复java中的循环和中断(我无法修复它,否则它将崩溃),java,eclipse,loops,if-statement,break,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 sta
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
我不知道错误在哪里,但需要立即修复。
请帮帮我
我没有吃午饭
我没有在学校休息
我在家里花了两个小时试图解决这个问题。
但我不能。
请帮助我你的括号有很多问题。确保在创建循环时,将循环的内容用花括号括起来
{}
。在编写条件时,请确保将每个部分用花括号括起来
这是修改后的程序的前半部分,剩下的由你来写
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
}
}
}
这是什么意思?我在你的代码中没有看到一个结束括号。经验法则-对于每个左括号
{
都需要有一个匹配的右括号}
@SamDufel,有一些。。。缩进很差。啊,它们被藏起来了!它仍然缺少几个,尽管为什么break;{
?具体来说,您要打开哪个块(为什么)?