import java.util.Scanner;
public class RectAngleObj {
public static void main(String args[]) {
String sPeri = "perimeter";
String sArea = "area";
String sQues = "?";
String sQuit = "quit";
String f;
String g;
String h;
String j;
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
f = sc.next(); // assigns the first standard input to f (can either be ? or quit)
g = sc.next(); // assigns the second word from standard input to g (can either be perimeter or area)
if(f.equals(sQues)) { // if first word is equal to "?" then the if statements continue
if (g.equals(sPeri)) { // if the second word is equal to "perimeter" print out result
double a = sc.nextDouble(), // assigns next four doubles to a, b, c, d
b = sc.nextDouble(),
c = sc.nextDouble(),
d = sc.nextDouble();
RectAngle3 r = new RectAngle3(a, b, c, d); // creates an object of RectAngle3 with dimensions of a, b, c, d
System.out.println("Perimeter of (" + r.rectNums() + "): " + r.rectPeri());
} if (g.equals(sArea)) { // if the second word is equal to "area" print out result
double a = sc.nextDouble(), // assigns next four doubles to a, b, c, d
b = sc.nextDouble(),
c = sc.nextDouble(),
d = sc.nextDouble();
RectAngle3 r = new RectAngle3(a, b, c, d); // creates an object of RectAngle3 with dimensions of a, b, c, d
System.out.println("Area of (" + r.rectNums() + "): " + r.rectArea());
}
}
}
}
}
我希望while循环自然结束而不使用System.exit(0);或打破; 请帮忙!
理想情况下,如果有人输入“?退出”,程序将结束。
我没有收到任何错误,循环只是永远持续......不知道此时该怎么做。
答案 0 :(得分:1)
取消f
应该是?
或quit
的评论,并且您不想使用break
或System.exit(0)
,可以这样做:
if(f.equals(sQuit)) {
return;
}
答案 1 :(得分:1)
您需要更改循环以使其具有退出条件,而不是使用while (sc.hasNext()
并在“问题”块中添加条件以检查用户是否输入了sQuit
,示例
boolean hasQuit = false;
do {
f = sc.next(); // assigns the first standard input to f (can either be ? or quit)
g = sc.next(); // assigns the second word from standard input to g (can either be perimeter or area)
if (f.equals(sQues)) { // if first word is equal to "?" then the if statements continue
if (g.equals(sQuit)) {
hasQuit = true;
} else if (g.equals(sPeri)) { // if the second word is equal to "perimeter" print out result
//...
} else if (g.equals(sArea)) { // if the second word is equal to "area" print out result
//...
}
}
} while (!hasQuit);
答案 2 :(得分:0)
使用sqlite3_step
。如果你从main返回,你的程序将退出(除非有后台运行的线程)