当前位置: 首页 > 知识库问答 >
问题:

有没有办法将变量移出其作用域?-JAVA[重复]

吕天逸
2023-03-14

我是一名学生,我想知道是否有一种方法可以将一个变量从它的范围块中取出,用于本地范围?我正在尝试对一个商店进行编码,在那里用户可以选择他们想要购买什么,他们想要购买多少,并给他们同一商品的总付款。现在在块范围之外,我想给出他们购买每件商品的总金额。这是我的代码:

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个答案

谢飞舟
2023-03-14

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

         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语句的开关语句

 类似资料:
  • 问题内容: 我想将背景URL存储在自定义属性(CSS变量)中,并将其与background属性一起使用。但是,当使用字符串作为参数时,我找不到插值的方法。 这是我的示例代码: 我知道可以使用插值函数在Sass或LESS中轻松完成此操作,但我很好奇是否有一种无需任何预处理器的方法。 问题答案: 您可以使用大多数CSS函数执行插值,包括的示例。实际上,插值是自定义属性的主要功能之一。 但是,您不能使用

  • 我在Kubernetes集群中有一个服务和RabbitMQ。我想做的是,我希望服务的不同实例(或副本)在启动时声明一个全新的队列。这些队列将绑定到同一个exchange。 我可以在这里放一个最后的变量吗: 本质上,我只需要一种方法,用生成的名称创建一个队列,然后使用@RabbitListener侦听这个队列。

  • 问题内容: 我正在尝试用其他常量定义常量,但似乎无法完成,因为当所需常量依赖于它时,初始常量尚未准备就绪。我想确定这是否完全不可能。 目前,我有这样的常量: 后两个常数是我想要完成的 问题答案: 定义控制器,服务和其他控制器之间的依赖关系的角度方法是通过依赖关系注入(DI)。因此,如果您有一个依赖于服务B的控制器A,则必须像这样创建它。 可以看到,angular将检查serviceB依赖关系,并查

  • 方法是否可能返回“object”?谢谢

  • 我有 2 个代码示例: 工作正常。 虽然这段代码有编译错误: 说: 唯一的区别是int或int数组。 根本原因是什么?

  • 问题内容: 我正在尝试运行使用类似本地Linux逻辑路径的命令,但是该逻辑路径(这是一个用户环境变量)不能通过来使用。但是当我使用Interactive时,我可以看到用户变量,并且命令在交互式会话上运行良好。我只能从“ exec”会话中查看系统级环境变量。使用JSch库甚至可以实现,如果是,那么我应该如何实现它;如果不是,我应该使用哪个库来实现? 在下面添加我的课程代码:`public class