Switch control structure code

Switch control structure code

* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
package swicth;

import java.util.Scanner;

* @author derdus
public class Swicth {

* @param args the command line arguments
public static void main(String[] args) {
// TODO code application logic here
//declare your variables here
String response;
double initial_balance = 10000;
//we assume that deposit is zero
double deposit = 0;
// we assume withdrawal is zero also
double withdrawal = 0;
/*We need to declare the scanner object
to allow a user to input something
Scanner scan =  new Scanner(System.in);
Scanner user_deposit = new Scanner(System.in);
Scanner user_withdrawa = new Scanner(System.in);
/*we have our variable now; we start
the outputs
System.out.println("Reply with");
System.out.println("B: to check balance");
System.out.println("W: to Withdraw");
System.out.println("D: to Deposit");
/*Now use the scanner object to receive user
input- response
response = scan.nextLine().trim();
/*We have used the trim() method to do remove any
leading or trailing space.
We will now use the response in for switch execution
switch (response){
/*Note that switch has opening and closing
curl brackets. so our code goes in between
the brackets
case "B":
System.out.println("Your balance is: " + initial_balance);
case "W":
System.out.println("Enter amount to withdraw:");
withdrawal = user_withdrawa.nextDouble();
//No sufficint funds
System.out.println("No sufficint funds!");
/*allow the user to withdraw, then compute
new balance
System.out.println("You withdrew " + withdrawal);
System.out.println("Your new balance is " + (initial_balance-withdrawal));
case "D":
System.out.println("Enter amount to deposit:");
deposit = user_deposit.nextDouble();
System.out.println("You have deposited" + deposit);
System.out.println("Your new balance is " + (initial_balance + deposit));
/*This path executed when none of the inputs match
the cases. e.g. when a user enters a letter other
than B,W or D.
System.out.println("Invalid entry");


One Comment

Add a Comment

Your email address will not be published. Required fields are marked *