有没有办法将变量带出其范围? - 爪哇

时间:2020-12-26 11:37:25

标签: java

我是一名学生,想知道是否有办法将变量从其作用域块中取出以在本地作用域中使用?我正在尝试编写某种类型的商店,在该商店中,用户可以选择几个选项来选择他们想要购买的东西、他们想要购买的数量,并为他们提供相同商品的总付款。现在在块范围之外,我想给出他们购买每件商品的总金额。这是我的代码:

import java.util.Scanner;

public class SariSariStore{
   public static void main (String [] args) {
   
   Scanner scanner = new Scanner(System.in);
   int piattos = 20;
   int wRabbit = 1;
   int maxEM = 1;
   int nescafeO = 7;
   float iceW = 1.50f;
   
   System.out.println("Would you like to buy something? [Y/N]");
   char i = scanner.next().toLowerCase().charAt(0);
   do {
          System.out.println("==================================="
                     +"\n"+  "|          Sari Sari Mart         |"
                     +"\n"+  "==================================="
                     +"\n"+  "| What do you want to buy?        |"
                     +"\n"+  "|                                 |"
                     +"\n"+  "|    1) Piattos chips     20 Php  |" 
                     +"\n"+  "|    2) White Rabbit      1 Php   |"
                     +"\n"+  "|    3) Max extra menthol 1 Php   |"
                     +"\n"+  "|    4) Nescafe Original  7 Php   |"
                     +"\n"+  "|    5) Ice water         1.5 PhP |"
                     +"\n"+  "|    6) Exit                      |"
                     +"\n"+  "===================================");
          System.out.println("Please select option from the given choices: ");
          int choice = scanner.nextInt();
        
         if (choice >= 1 && choice <= 6){       
                
             if (choice == 1){ 
               System.out.print("How many would you like to buy? " );
                 int quantity1 = scanner.nextInt(); 
                 int itotal1 = piattos * quantity1;
               System.out.println("Total price: " + itotal1 +" Php");  
            }else if (choice == 2){ 
               System.out.print("How many would you like to buy? " );
                 int quantity2 = scanner.nextInt(); 
                 int itotal2 = wRabbit * quantity2;
               System.out.println("Total price: " + itotal2 +" Php");
            }else if (choice == 3){ 
               System.out.print("How many would you like to buy? " );
                 int quantity3 = scanner.nextInt(); 
                 int itotal3 = maxEM * quantity3;
               System.out.println("Total price: " + itotal3 +" Php");
            }else if (choice == 4){ 
               System.out.print("How many would you like to buy? " );
                 int quantity4 = scanner.nextInt(); 
                 int itotal4 = nescafeO * quantity4;
               System.out.println("Total price: " + itotal4 +" Php");
            }else if (choice == 5){ 
               System.out.print("How many would you like to buy? " );
                 int quantity5 = scanner.nextInt(); 
                 float itotal5 = iceW * quantity5;
               System.out.println("Total price: " + itotal5 +" Php");
            }else if (choice == 6){ 
             }
             
        }else{
         System.out.println ("Sorry we do not have that item, please pick among the choices");
        }
        int Total = itotal1 +itotal2 +itotal3 +itotal4 +itotal6; // problematic statement 
        System.out.println ("Your total is:"+ Total + " Php"); 
        System.out.println ("Do you still want to buy something? [Y/N]");
        i = scanner.next().toLowerCase().charAt(0);
   }while (i == 'y');
   if (i =='n'){
     System.out.println("Thank you! Please come again");
   }
   
   }
}

谢谢:>

1 个答案:

答案 0 :(得分:0)

你不能只使用一个字段吗?像这样:

         System.out.println("Please select option from the given choices: ");
         int choice = scanner.nextInt();
         int total = 0;
        
         if (choice >= 1 && choice <= 6){       
                
            if (choice == 1){ 
               System.out.print("How many would you like to buy? " );

               int quantity1 = scanner.nextInt(); 
               total = piattos * quantity1;

               System.out.println("Total price: " + total +" Php");  
            } else if (choice == 2){ 
               System.out.print("How many would you like to buy? " );

               int quantity2 = scanner.nextInt(); 
               total = wRabbit * quantity2;

               System.out.println("Total price: " + total +" Php");
            } else if (choice == 3){ 
               System.out.print("How many would you like to buy? " );

               int quantity3 = scanner.nextInt(); 
               total = maxEM * quantity3;

               System.out.println("Total price: " + total +" Php");
            }
            ...
             
        } else {
         System.out.println ("Sorry we do not have that item, please pick among the choices");
         System.out.println ("Your total is:"+ total + " Php");
        }
        

还要考虑对多个 if 语句使用 switch 语句