[英]How to break label within a different method java
这是我的源代码:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class PostfixConverter{
static int top = 0;
static String[] mainStack = new String[100];
final static String asterisk = "*";
final static String divisor = "/";
final static String plus = "+";
final static String minus = "-";
final static String store = "ST TEMP";
static int temp = 0;
static int directionCounter = 0;
static String[] directions = new String[100];
static PostfixConverter s = new PostfixConverter();
static String tempString = "TEMP";
public static void main(String args[]) throws Exception {
String string = null;
String load = "LD ";
String multiply = "ML ";
String add = "AD ";
String div = "DV ";
String subtract = "SB ";
String example = "AB+C-";
try {
// file reader code
FileReader file = new FileReader("ignore for now");
BufferedReader reader = new BufferedReader(file);
String line = "";
while ((line = reader.readLine()) != null) {
example = line;
// for loop to print directions
Characterloop:
for (int i = 0; i < example.length(); i++) {
// get letter entered by user 1 by 1
char letter = example.charAt(i);
// convert char to string
String convertedChar = java.lang.String.valueOf(letter);
// finds operands in order or priority
// multiply character
if (convertedChar.equals(asterisk)) {
processOperand(PostfixConverter.multiply(string), PostfixConverter.multiply(string), load,
multiply);
}
// division character
else if (convertedChar.equals(divisor)) {
processOperand(PostfixConverter.addition(string), PostfixConverter.addition(string), load, div);
}
// addition character
else if (convertedChar.equals(plus)) {
processOperand(PostfixConverter.addition(string), PostfixConverter.addition(string), load, add);
}
// subtraction character
else if (convertedChar.equals(minus)) {
processOperand(PostfixConverter.addition(string), PostfixConverter.addition(string), load,
subtract);
}
// letter character
else {
s.push(convertedChar);
}
}
// print out the instructions
System.out.println("Assembly Directions are as follows: ");
int printDirections = 0;
for (int i = 0; i < directionCounter; i++) {
System.out.println(directions[printDirections]);
printDirections++;
}
printDirections = 0;
directionCounter = 0;
System.out.println("This is the end of the directions.");
System.out.println("");
directionCounter = 0;
temp = 0;
top = 0;
}reader.close();
} catch (FileNotFoundException exception) {
System.out.println("The file was not found.");
}
}
private static void processOperand(String postFileConverterOutput, String postFileConverterOutput2,
String instruction1, String instruction2) {
String outcome;
String opReturn1 = postFileConverterOutput;
String opReturn2 = postFileConverterOutput2;
directions[directionCounter] = instruction1 + opReturn2;
directionCounter++;
directions[directionCounter] = instruction2 + opReturn1;
directionCounter++;
temp++;
outcome = tempString + java.lang.String.valueOf(temp);
directions[directionCounter] = store + java.lang.String.valueOf(temp);
directionCounter++;
s.push(outcome);
}
// multiply method
public static String multiply(String a) {
if(top == 0){
System.out.println("Invalid Argument");
System.out.println("Please resubmit a correct String");
break Characterloop;
}
String multVariable = PostfixConverter.pop(mainStack[top]);
top--;
return multVariable;
}
// addition method
public static String addition(String a) {
if(top == 0){
System.out.println("Invalid Argument");
System.out.println("Please resubmit a correct String");
}
String addVariable = PostfixConverter.pop(mainStack[top]);
top--;
return addVariable;
}
// subtraction method
public static String subtraction(String a) {
if(top == 0){
System.out.println("Invalid Argument");
System.out.println("Please resubmit a correct String");
}
String subVariable = PostfixConverter.pop(mainStack[top]);
top--;
return subVariable;
}
// division method
public static String division(String a) {
if(top == 0){
System.out.println("Invalid Argument");
System.out.println("Please resubmit a correct String");
}
String divVariable = PostfixConverter.pop(mainStack[top]);
top--;
return divVariable;
}
public static boolean empty() {
boolean check = false;
if (top < 0){
check = true;
}
else{
check = false;
}return check;
}
public static String pop(String j) {
if (top < 0) {
System.out.println("Stack is empty");
System.exit(1);
}
return mainStack[top - 1];
}
public void push(String x) {
if (top == 99) {
System.out.println("Stack Overflow");
System.exit(1);
} else
mainStack[top] = x;
System.out.println("Top:" + top + "||" + " Array: " + mainStack[top]);
top++;
}// end push
}
当给出无效字符串时,如何让此方法停止运行循环? 我要问的项目在**问题**下方的星号中。 比如说,我得到了后缀字符串“AB+*AB+”和“AB+”。 显然这是无效的,因为不能有只有两个操作数的两个运算符。 如何跳过第一条语句并打印“无效参数”并继续下一个字符串(AB+)? 我研究了 break 和 continue,但我不知道如何在另一种方法中实现这一点。 这是一个家庭作业,只是想指出正确的方向。
// multiply method
public static String multiply(String a) {
if(top == 0){
System.out.println("Invalid Argument");
System.out.println("Please resubmit a correct String");
**break Characterloop;**
}
像这样的事情可以使用异常和 try/catch 来完成。 然而,这样做应该谨慎使用。 抛出异常不应该用于向用户“返回”某些东西。 但是抛出异常有好处:
break
语句按预期工作,则不会进行编译时检查来验证是否仅调用了该方法,如果堆栈中存在包含适当标签的方法调用让我们简单化一下问题代码:
休息
void a() {
label1: while (check()) {
b();
}
}
void b() {
c();
}
void c() {
if (check2()) {
break label1;
}
doSomething();
}
继续
void d() {
label2: while(check()) {
e();
}
doSomething();
}
void e() {
f();
}
void f() {
if (check2()) {
continue label2;
}
doSomething();
}
休息
class MyException1 extends Exception {
}
void a() {
try {
while (check()) {
b();
}
} catch (MyException1 ex) {
// we're already outside the loop; nothing to be done here
}
}
void b() throws MyException1 {
c();
}
void c() throws MyException1 {
if (check2()) {
throw new MyException1();
}
doSomething();
}
继续
class MyException2 extends Exception {
}
void d() {
while(check()) {
try {
e();
doSomething();
} catch (MyException2 ex) {
// nothing to do here; We're already at the end of the loop body
}
}
}
void e() throws MyException2 {
f();
}
void f() throws MyException2 {
if (check2()) {
throw new MyException2();
}
doSomething();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.