1

Assuming these Methods work, which I had tested earlier, can I ask for a second set of eyes on what I'm doing wrong in this While loop? I am looking for it to repeat for shapes until the user inputs a 0 specifically. as the text shows im doing Circle, Triangle and Rectangle.

public static void main(String[] args) 
{
       
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter an integer representing the shape would you         like the area of.n/ (1-Triangle, 2-Circle, 3-Rectangle)");
          int shape = scanner.nextInt();
            do{
               //dowhile loop
                 int shapecheck=1;
                 switch(shape)
                        {
                        case 1:CalcArea.CalcTri();
                        System.out.println("Another shape? (1-yes, 0-no)");
                        int shapecheck = scanner.nextInt();
                        break;
            
                        case 2:CalcArea.CalcCir();
                        System.out.println("Another shape? (1-yes, 0-no)");
                        int shapecheck = scanner.nextInt();
                        break;
            
                        case 3:CalcArea.CalcRect();
                        System.out.println("Another shape? (1-yes, 0-no)");
                        int shapecheck = scanner.nextInt();
                        break;
            
                        default:
                        shapecheck = 0;
                        }    
            }while (shapecheck==1);  
}
Chaosfire
  • 4,818
  • 4
  • 8
  • 23

2 Answers2

1

Move the declaration of shapecheck outside the loop so it is visible in the loop condition. Don't redeclare the variable inside the cases; assign to it instead.

int shapecheck = 1;
do {
    switch (shape) {
        case 1:
            CalcArea.CalcTri();
            System.out.println("Another shape? (1-yes, 0-no)");
            shapecheck = scanner.nextInt();
            break;
        case 2:
            CalcArea.CalcCir();
            System.out.println("Another shape? (1-yes, 0-no)");
            shapecheck = scanner.nextInt();
            break;
        case 3:
            CalcArea.CalcRect();
            System.out.println("Another shape? (1-yes, 0-no)");
            shapecheck = scanner.nextInt();
            break;
        default:
            shapecheck = 0;
    }
} while (shapecheck == 1);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
  • Good answer! It will also be nice to introduce beginners to the [switch expression](https://stackoverflow.com/q/65657169/10819573). Probably, erickson has also pointed to the same thing in his [comment](https://stackoverflow.com/questions/73965645/how-can-i-fix-this-do-while-and-switch-case-loop-the-outputs-of-the-methods-in/73965679#comment130600874_73965645). – Arvind Kumar Avinash Nov 20 '22 at 21:36
0

I think in the loop you do not ask the user for the shape variable, so this stays at the initial entered value. You only ask whether the user wishes to enter another value, but not the value itself. Make another scanner.nextInt() asking for the shape

Dr Mido
  • 2,414
  • 4
  • 32
  • 72