Advertisement
Hexbugman213

Go up and down musical scale

Dec 8th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.38 KB | None | 0 0
  1. package music;
  2.  
  3. import javax.swing.JOptionPane;
  4.  
  5. public class musicc {
  6.     public static void main(String[] args) throws Exception {
  7.         int noteid;
  8.         int notechange;
  9.         boolean direction; //true = up, false = down
  10.         int newnoteid;
  11.         String newnote;
  12.         boolean debug = true; //PRINTS DEBUG INFO
  13.         //GETS USER INPUT!
  14.         noteid = musicc.getNoteId(); //gets noteid
  15.         if (debug == true) {
  16.             System.out.println(noteid); //prints noteid for debug
  17.         }
  18.         notechange = musicc.getNoteChange(); //gets note change
  19.         if (debug == true) {
  20.             System.out.println(notechange); //prints notechange for debug
  21.         }
  22.         direction = getDirection();
  23.         if (debug == true) {
  24.             System.out.println(direction); //prints direction for debug
  25.         }
  26.         //CALCULATIONS
  27.         System.out.println("Starting Calculations...");
  28.         notechange = notechange - 1;
  29.         if (direction == false) {
  30.             notechange = notechange * -1;
  31.         }
  32.         newnoteid = noteid + notechange;
  33.         newnoteid = newnoteid % 7;
  34.         if (debug == true) {
  35.             System.out.println(newnoteid); //prints id for debug
  36.         }
  37.         String result = getNewNote(newnoteid);
  38.         //RESULTS
  39.         JOptionPane.showMessageDialog(null,"The note you landed on is " + result,"Results", JOptionPane.INFORMATION_MESSAGE);
  40.     }
  41.  
  42.     public static int getNoteId() {
  43.         String note;
  44.         //asks for note
  45.         Object[] possibilities = {"A", "B", "C", "D", "E", "F", "G"};
  46.         note = (String)JOptionPane.showInputDialog(null,"What note do you want to start on?","Music Scale",JOptionPane.PLAIN_MESSAGE,null,possibilities,"A");
  47.         while (note == null) { //makes sure if cancel is pressed it tries again
  48.             JOptionPane.showMessageDialog(null,"Enter a note!", "Error!", JOptionPane.ERROR_MESSAGE);
  49.             note = (String)JOptionPane.showInputDialog(null,"What note do you want to start on?","Music Scale",JOptionPane.PLAIN_MESSAGE,null,possibilities,"A");
  50.         }
  51.         switch(note) { //note to number
  52.         case "A":return 1;
  53.         case "B":return 2;
  54.         case "C":return 3;
  55.         case "D":return 4;
  56.         case "E":return 5;
  57.         case "F":return 6;
  58.         case "G":return 7;
  59.         }
  60.         return 0;
  61.     }
  62.     public static int getNoteChange() {
  63.         String change;
  64.         int changeInt = 0; //declare int
  65.         boolean ischangeint = false;
  66.         change = (String)JOptionPane.showInputDialog(null,"How much do you want to go up or down?","Music Scale",JOptionPane.PLAIN_MESSAGE,null,null,"1"); //inital input
  67.         while (ischangeint == false) {
  68.             try {
  69.                 changeInt = Integer.parseInt(change); //converts string answer to integer
  70.                 //goes to catch if it's not
  71.                 if (changeInt >= 1) { //checks if its greater than or equal to 1
  72.                     ischangeint = true; //if all conditions satisfied, change ischangeint to true, breaking the while loop
  73.                 } else { //less than 1
  74.                     JOptionPane.showMessageDialog(null,"That is not a positive number!", "Error!", JOptionPane.ERROR_MESSAGE); //error
  75.                     change = (String)JOptionPane.showInputDialog(null,"How much do you want to go up or down?","Music Scale",JOptionPane.PLAIN_MESSAGE,null,null,"1"); //asks again
  76.                 }
  77.             } catch (NumberFormatException e) { //goes to this instead of error if not integer
  78.                 JOptionPane.showMessageDialog(null,"That is not a valid number!", "Error!", JOptionPane.ERROR_MESSAGE); //error
  79.                 change = (String)JOptionPane.showInputDialog(null,"How much do you want to go up or down?","Music Scale",JOptionPane.PLAIN_MESSAGE,null,null,"1"); //asks again
  80.             }
  81.            
  82.         }
  83.         return changeInt; //after checking if it is a valid int, it returns it
  84.     }
  85.     public static boolean getDirection() {
  86.         String directionS;
  87.         Object[] possibilities = {"Up","Down"};
  88.         directionS = (String)JOptionPane.showInputDialog(null,"Which direction do you want to count in?","Music Scale",JOptionPane.PLAIN_MESSAGE,null,possibilities,"Up");
  89.         while (directionS == null) { //makes sure if cancel is pressed it tries again
  90.             JOptionPane.showMessageDialog(null,"Enter a direction!", "Error!", JOptionPane.ERROR_MESSAGE);
  91.             directionS = (String)JOptionPane.showInputDialog(null,"Which direction do you want to count in?","Music Scale",JOptionPane.PLAIN_MESSAGE,null,possibilities,"Up");
  92.         }
  93.         if (directionS == "Up") {
  94.             return true;
  95.         } else {
  96.             return false;
  97.         }
  98.        
  99.     }
  100.     public static String getNewNote(int id) {
  101.         switch(id) { //note to number
  102.         case 1:return "A";
  103.         case 2:return "B";
  104.         case 3:return "C";
  105.         case 4:return "D";
  106.         case 5:return "E";
  107.         case 6:return "F";
  108.         case 0:return "G";
  109.         }
  110.         return "error";
  111.     }
  112. }
  113. //Created by Hexbugman213
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement