mardi 16 février 2016

Switch Statement boolean & elseif problems [on hold]

import java.util.*;
/**
 * Barbell weight calculator
 */
public class barbell
{

public static void main(String[] args) {


   int red = 0; // 25kg 
   int blue = 0; // 20kg 
   int yellow = 0; //15kg
   int green = 0; //10kg
   int white = 0; //5kg
   int small_red = 0; //2.5kg
   int silver = 0; //1.25kg

   int error = 0; //error flag for if something goes wrong in the while loop

   Scanner reader = new Scanner(System.in);
   System.out.println("Enter the weight of the barbell: ");
   int barbell_weight = reader.nextInt();

   Scanner readertwo = new Scanner(System.in);
   System.out.println("Enter the total weight you want to lift(including barbell) : ");
   double total_weight = readertwo.nextDouble();

   int needed = ((int)Math.round(((total_weight - barbell_weight)/2)+ .5));


   while (needed > 1.25) {




        if (error == 1) {
            needed = 0;
        }


       switch (needed) {
           case (needed>=25):
           red = red  + 1;
           needed = needed - 25;
            break;

           case (needed >=20 && needed<25):
           blue = blue + 1;
           needed = needed - 20;
            break;

           case (needed >=15 && needed < 20):
           yellow = yellow + 1;
           needed = needed - 15;
            break;

           case (needed >=10 && needed < 15):
           green = green + 1;
           needed = needed - 10;
            break;

           case  (needed >=5 && needed <10):
           white = white + 1;
           needed = needed - 5;
            break;

           case (needed >=2.5 && needed <5):
           small_red = small_red + 1;
           needed = needed-2.5;
            break;

           case (needed >=1.25 & needed < 2.5):
           silver = silver + 1;
           needed = needed - 1.25;
            break;

           default: 
           error = 1;
            break;
        }
    }

   double total = red*25 + blue*20 + yellow*15 + green*10 + white*5 + small_red*2.5 + silver*1.25;

    if (needed !=0) {
        System.out.println("The closest to that weight is"+total);
        System.out.println("It can be made by putting the following on each side:");
        System.out.println("25kg(red) plates:" +red);
        System.out.println("20kg(blue) plates:" +blue);
        System.out.println("15kg(yellow) plates:" +yellow);
        System.out.println("10kg(green) plates:" +green);
        System.out.println("5kg(white) plates:" +white);
        System.out.println("2.5kg plates:" +small_red);
        System.out.println("1.25kg plates:" +silver);  
    } else {
        System.out.printlb("The weight can be made exactly by putting the following on each side:");
        System.out.println("25kg(red) plates:" +red);
        System.out.println("20kg(blue) plates:" +blue);
        System.out.println("15kg(yellow) plates:" +yellow);
        System.out.println("10kg(green) plates:" +green);
        System.out.println("5kg(white) plates:" +white);
        System.out.println("2.5kg plates:" +small_red);
        System.out.println("1.25kg plates:" +silver);      
}




}
}

Before I start: I know only a few coding conventions and apologise if some of the stuff is poorly formatted, feel free to tell me what the standard is.

So i'm trying to write a small program to work out what weights you'd need to put on each side of a barbell when the user specifies a given weight. When compiling the code above it gives an error saying "incompatible types: boolean cannot be converted to int" - I thought that the statements I have in the brackets will return either true of false based on the value of needed and the switch will take over from there?

I tried creating booleans within the loop to make the cases booleans, i wrote this:

       boolean addred = false;
       boolean addblue = false;
       boolean addyellow = false;
       boolean addgreen = false;
       boolean addwhite = false;
       boolean addsmall = true;
       boolean addsilver = false;

       if (needed>=25){
           addred = true;
        }else if (needed >=20 && needed<25); {
            addblue = true;
        }else if(needed >=15 && needed < 20);{
            addyellow= true;
        }else if (needed >=10 && needed < 15);{
            addgreen=true;
        }else if (needed >=5 && needed <10);{
            addwhite = true;
        }else if (needed >=2.5 && needed <5);{
            addsmall= true;
        }else if (needed >=1.25 & needed < 2.5){
            addsilver = true;
        }else{
             addsilver = true;
        }

and changed the conditions in each of the cases to simply addred, addblue etc.

When I did this, it comes up with the error "'else' without 'if'" on this line:

}else if(needed >=15 && needed < 20);{

Any ideas?

Aucun commentaire:

Enregistrer un commentaire