나는 자바에 다소 새로운 멍청하다. 나는 계산기를 만들려고 노력하고, 그것이 작동하는지 모든 것을 테스트하지는 않았지만, 문제가 생겼어요. 한 번의 계산을 수행 한 후에 기본 메뉴로 돌아갈 방법을 알아낼 수 없습니다. 마지막 질문을 추가하여 사용자가 종료하거나 메인 메뉴로 계속 돌아가도록합니다. 나는 무엇을 넣어야할지 모르겠다. (whatnow == Y) {wtf 나는 메인 메뉴로 돌아 가기 위해해야만 하는가 ?? }. 그것이 약간 길거나 무엇인가 인 경우에 유감스럽게 여기십시오, 그러나 그들은 진짜로 모두 동일합니다 그래서 계산 thingy 다만 건너 뜀. 어떤 도움을 주셔서 감사합니다. 나는 정말로 자바에 익숙하지 않고 아마도이 코드를 다시 작성해야 할 것이다.자바 코드의 시작으로 돌아 가기 (계산기의 메인 메뉴)
package practice;
import java.util.Scanner;
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int choice;
int firstnumber;
int secondnumber;
int result;
char whatnow;
System.out.println("Welcome to StemCalc Z Edition(Integers only)!");
System.out.println("Made with love and basic Java");
System.out.println("Which math operation would you like to perform?");
System.out.println("");
System.out.println("WARNING: Enter the integer x, press ENTER, then enter y");
System.out.println("");
System.out.println("[1]-Addition (+)");
System.out.println("[2]-Subtraction (-)");
System.out.println("[3]-Multiplication (x)");
System.out.println("[4]-Division (/)");
System.out.println("");
System.out.println("Enter your choice[1-4 or 99]:"); choice = scan.nextInt();
while ((choice < 1 || choice > 4) && choice != 99) {
System.out.println("Please enter 1, 2, 3, 4, or 99: ");
choice = scan.nextInt();
}
if (choice == 1){
System.out.println("Enter two integer to add(x + y)");
firstnumber = scan.nextInt();
secondnumber = scan.nextInt();
result = firstnumber + secondnumber;
System.out.println(firstnumber + " + " + secondnumber + " = " + result);
}
else if (choice == 2) {
System.out.println("Enter two integers to subtract(x - y)");
firstnumber = scan.nextInt();
secondnumber = scan.nextInt();
result = firstnumber - secondnumber;
System.out.println(firstnumber + " - " + secondnumber + " = " + result);
}
else if (choice == 3) {
System.out.println("Enter two integers to multiply(x * y)");
firstnumber = scan.nextInt();
secondnumber = scan.nextInt();
result = firstnumber * secondnumber;
System.out.println(firstnumber + " * " + secondnumber + " = " + result);
}
else if (choice == 4) {
System.out.println("Enter to integers to divide(x/y)");
firstnumber = scan.nextInt();
secondnumber = scan.nextInt();
while (secondnumber == 0) {
System.out.println("ERROR-CANNOT DIVIDE TO ZERO! Type another integer:");
secondnumber = scan.nextInt();
}
result = firstnumber/secondnumber;
System.out.println(firstnumber + "/" + secondnumber + " = " + result);
}
else if (choice == 99) {
System.exit(0);
}
while (choice !=99) {
System.out.println("Do you want to continue calculating? [Y/N]:"); whatnow = scan.next().charAt(0);
if (whatnow == 'Y' || whatnow == 'y') {
}
if (whatnow == 'N' || whatnow == 'n') {
System.exit(0);
}
}
}
}
PS :
`while (choice !=99) {
System.out.println("Do you want to continue calculating? [Y/N]:"); whatnow = scan.next().charAt(0);
while(whatnow != 'Y' || whatnow != 'y' || whatnow !='N' || whatnow !='n') {
System.out.println("Enter [Y/N] only:"); whatnow = scan.next().charAt(0);
}
if (whatnow == 'N' || whatnow == 'n') {
System.exit(0);`
이동 모든 코드, 당신은 시작에 복귀해야 할 때 메소드를 호출 한 후
그리고
calc()
라는 방법을 만들 수 있습니다. –저는 멍청 해요, 내가 아는 유일한 방법은 public static void main (String args [])입니다. 지옥, 방법인지 아닌지조차 모르겠다. P – Ubuntu4EVA
예, 그 방법입니다. 그리고 당신은 그것을 좋아하는 다른 사람을 만들어야합니다. 괜찮은 이름을 짓고'메인 '내부에서 불러오고 돌아올 필요가있을 때마다 호출해야합니다.주변에서 충분한 정보가 있어야 대화에서 키워드 검색을하고 무엇을해야하는지 이해할 수 있습니다. –