[英]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.