С моим кодом у меня возникают проблемы с добавлением цикла в конец, чтобы код начинался заново - PullRequest
0 голосов
/ 19 января 2019

С моим кодом мне нужен цикл, который запускает код заново.Они говорят «встроить вашу программу в цикл с последним оператором / условием выхода в виде строки, равной no», но я до сих пор не понимаю, где ее поместить и какой цикл использовать.

import java.io.*;
import java.util.*;

public class addiePorterMod8Banking {
    public static void main(String args[]) {
        int depositSavings, depositChecking, withdrawalSavings, withdrawalChecking, transferSavingsToChecking;
        int b, c, d, e, f, g, lp;
        int savingsaccount = 3000;
        int checkingaccount = 650;
        Scanner kbReader = new Scanner(System.in);
        System.out.println("What do you wish to do today?\n1 for deposit to savings\n2 for deposit to checking\n3 for withdraw from savings\n4 for withdraw from checking\n5 for transfer funds from savings to checking");
        int a = kbReader.nextInt();
        if (a == 1) {
            System.out.println("1-you chose to make a deposit to savings. How much would you like to deposit?");
            int answer = kbReader.nextInt();
            lp = savingsaccount + answer;
            System.out.println("Your total in savings is $" + lp);
        }
        if (a == 2) {
            System.out.println("2-you chose to make a deposit to checking. How much would you like to deposit?");
            int answer = kbReader.nextInt();
            lp = checkingaccount + answer;
            System.out.println("Your total in checking is $" + lp);
        }
        if (a == 3) {
            System.out.println("3-you chose to withdraw from savings. How much would you like to withdraw?");
            int bunny = kbReader.nextInt();
            lp = savingsaccount - bunny;
            System.out.println("Your total in savings is $" + lp);
        }
        if (a == 4) {
            System.out.println("4-you chose to withdraw from checking. How much would you like to withdraw?");
            int bubble = kbReader.nextInt();
            lp = checkingaccount - bubble;
            System.out.println("Your total in checking is $" + lp);
        }
        if (a == 5) {
            System.out.println("5-Your current balance in savings is $3000. How much would you like to transfer?");
            int awesome = kbReader.nextInt();
            lp = savingsaccount - awesome;
            int al = checkingaccount + awesome;
            System.out.println("Your total in checking is $" + al + "." + "Your total in savings is $" + lp + ".");
        }
        System.out.println("Would you like another transaction? 1 for yes. 2 for No");
        int more = kbReader.nextInt();

    }
}

Ответы [ 2 ]

0 голосов
/ 19 января 2019

Хотя ответ, заданный Sand выше, является полностью правильным, учитывая количество используемых вами условий, которые не являются прямыми булевыми значениями, в конечном итоге будет быстрее и эффективнее использовать случай переключения, а также сохранятьвам нужно немного печатать на современных компьютерах, это не должно быть проблемой, но тем не менее

import java.io.*;
import java.util.*;

public class addiePorterMod8Banking {
    public static void main(String args[]) {
        while (true) {
            int depositSavings, depositChecking, withdrawalSavings, withdrawalChecking, transferSavingsToChecking;
            int b, c, d, e, f, g, lp;
            int savingsaccount = 3000;
            int checkingaccount = 650;
            Scanner kbReader = new Scanner(System.in);
            System.out.println(
                    "What do you wish to do today?\n1 for deposit to savings\n2 for deposit to checking\n3 for withdraw from savings\n4 for withdraw from checking\n5 for transfer funds from savings to checking");
            int a = kbReader.nextInt();
            switch (a) {
            case (1):
                System.out.println("1-you chose to make a deposit to savings. How much would you like to deposit?");
                int answer = kbReader.nextInt();
                lp = savingsaccount + answer;
                System.out.println("Your total in savings is $" + lp);
                break;
            case (2):
                System.out.println("2-you chose to make a deposit to checking. How much would you like to deposit?");
                int answer1 = kbReader.nextInt();
                lp = checkingaccount + answer1;
                System.out.println("Your total in checking is $" + lp);
                break;
            case (3):
                System.out.println("3-you chose to withdraw from savings. How much would you like to withdraw?");
                int bunny = kbReader.nextInt();
                lp = savingsaccount - bunny;
                System.out.println("Your total in savings is $" + lp);
                break;
            case (4):
                System.out.println("4-you chose to withdraw from checking. How much would you like to withdraw?");
                int bubble = kbReader.nextInt();
                lp = checkingaccount - bubble;
                System.out.println("Your total in checking is $" + lp);
                break;
            case (5):
                System.out.println("5-Your current balance in savings is $3000. How much would you like to transfer?");
                int awesome = kbReader.nextInt();
                lp = savingsaccount - awesome;
                int al = checkingaccount + awesome;
                System.out.println("Your total in checking is $" + al + "." + "Your total in savings is $" + lp + ".");
                break;
            }
            System.out.println("Would you like another transaction? 1 for yes. 2 for No");
            int more = kbReader.nextInt();

            if (more == 2) {
                break;
            }
        }
    }
}

небольшое примечание на тот случай, если вы хорошо знакомы с переключателями, вкратце, они похожи на длинныецепочка условных выражений, но как только одна задача выполнена, она не продолжает искать ее по всем значениям в виде последовательности операторов if, имеющих значение, как только что-то работает, она завершается вместо проверки каждого условия и для случаев, в которых нетBoolean считается быстрее читать дальше здесь

0 голосов
/ 19 января 2019

Вы можете использовать цикл while, как показано ниже, и когда пользователь вводит 2, прервите его

import java.io.*;
import java.util.*;

public class Main {
    public static void addiePorterMod8Banking (String args[]) {
        while (true) {
            int depositSavings, depositChecking, withdrawalSavings, withdrawalChecking, transferSavingsToChecking;
            int b, c, d, e, f, g, lp;
            int savingsaccount = 3000;
            int checkingaccount = 650;
            Scanner kbReader = new Scanner(System.in);
            System.out.println("What do you wish to do today?\n1 for deposit to savings\n2 for deposit to checking\n3 for withdraw from savings\n4 for withdraw from checking\n5 for transfer funds from savings to checking");
            int a = kbReader.nextInt();
            if (a == 1) {
                System.out.println("1-you chose to make a deposit to savings. How much would you like to deposit?");
                int answer = kbReader.nextInt();
                lp = savingsaccount + answer;
                System.out.println("Your total in savings is $" + lp);
            }
            if (a == 2) {
                System.out.println("2-you chose to make a deposit to checking. How much would you like to deposit?");
                int answer = kbReader.nextInt();
                lp = checkingaccount + answer;
                System.out.println("Your total in checking is $" + lp);
            }
            if (a == 3) {
                System.out.println("3-you chose to withdraw from savings. How much would you like to withdraw?");
                int bunny = kbReader.nextInt();
                lp = savingsaccount - bunny;
                System.out.println("Your total in savings is $" + lp);
            }
            if (a == 4) {
                System.out.println("4-you chose to withdraw from checking. How much would you like to withdraw?");
                int bubble = kbReader.nextInt();
                lp = checkingaccount - bubble;
                System.out.println("Your total in checking is $" + lp);
            }
            if (a == 5) {
                System.out.println("5-Your current balance in savings is $3000. How much would you like to transfer?");
                int awesome = kbReader.nextInt();
                lp = savingsaccount - awesome;
                int al = checkingaccount + awesome;
                System.out.println("Your total in checking is $" + al + "." + "Your total in savings is $" + lp + ".");
            }
            System.out.println("Would you like another transaction? 1 for yes. 2 for No");
            int more = kbReader.nextInt();

            if (more == 2) {
                break;
            }
        }
    }
}
...