【问题标题】:Is there a way to take a variable out of its scope? - JAVA [duplicate]有没有办法让变量超出其范围? - JAVA [重复]
【发布时间】:2020-12-26 11:37:25
【问题描述】:

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

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");
   }
   
   }
}

谢谢:>

【问题讨论】:

  • 只是不要在 if/else 语句中定义它们,而是在更高的位置,与 int total 处于相同的范围内。所以它看起来像: int choice = xxxx; int数量1,总计1,数量2,总计2,数量3,总计3 = 0;然后继续所有相同的逻辑,但是数量1 = =scanner.nextInt();
  • 我不明白你想问什么。如果您想在 do-while 循环之外访问您的总计(从 itotal1 到 itotal5),那么为什么不在 do-while 循环之外声明变量呢?即与扫描仪下的价格一起声明。

标签: java


【解决方案1】:

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

         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 语句

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-13
    • 2010-10-20
    相关资源
    最近更新 更多