Skip to content

Instantly share code, notes, and snippets.

@PatchRowcester
Created January 2, 2013 10:20
Show Gist options
  • Save PatchRowcester/4433589 to your computer and use it in GitHub Desktop.
Save PatchRowcester/4433589 to your computer and use it in GitHub Desktop.
Convoluted way to demonstrate assignment operators in Java
import java.util.Scanner;
public class AssignmentOperators {
public static void main(String args[]) {
int k = 5;
double d;
Scanner myScanner = new Scanner(System.in);
System.out.println("select the assignment operator you want to use: \n" + "1. addition\n" + "2. substraction\n" + "3. multiplication \n" + "4. division \n");
int x = myScanner.nextInt();
System.out.print("You selected: ");
if (x == 1) {
System.out.println("addition");
//int k = 5;
System.out.println ("original value of k = " + k);
k += 4;
System.out.println("value of k after increment assignment operator = " + k);
}
else if (x == 2) {
System.out.println("subscrtation");
System.out.println ("original value of k = " + k);
k -=4;
System.out.println("value of k after subsctration assignment operator = " + k);
}
else if (x==3) {
System.out.println("multiplication");
System.out.println("original vlaue of k = " + k);
k *= 2;
System.out.println("value of k after multiplication assignment operator = " + k);
}
else if (x == 4) {
System.out.println("division");
System.out.println("original value of k = " + k);
d = k;
d /=2;
System.out.println("value of k after division assignment operator = " + d);
}
else {
System.out.println("invalid choice");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment