如何在Java方法间共享Math.random生成的变量?
如何在Java的另一个方法中复用随机生成的食物选择变量
你的核心问题是:foodNum是foodMethod里的局部变量,其他方法默认访问不到。下面给你两种实用的实现方式:
方式一:让foodMethod返回foodNum,调用时传入tiredMethod
这是最符合Java封装原则的做法,把生成的食物选择值传递给需要用到的方法。
修改后的foodMethod代码
public static double foodMethod(Scanner keyboard) { double foodNum = Math.random() * 3; // 简化写法,+0可以省略 System.out.println("Now let's see..."); System.out.println(); if (foodNum <= 0.9) { System.out.println("You've decided on a sugary bowl of cereal!"); System.out.println("Although, that headache of yours isn’t going to go away anytime soon."); } if (foodNum > 0.9 && foodNum <= 1.9) { System.out.println("Hmm..."); System.out.println("Interesting"); System.out.println("Seems like someone decided to reach for the healthy option."); System.out.println("You settled on some yogurt and a tall glass of orange juice. Nice work!"); } if (foodNum > 1.9 && foodNum <= 3) { System.out.println("You end up grabbing some crackers and a glass of water to help you swallow some Tylenol to ease that headache of yours."); } return foodNum; // 返回生成的食物选择值 }
修改后的tiredMethod代码
public static void tiredMethod(Scanner keyboard, double foodNum) { System.out.println("About an hour of being bored..."); // 根据foodNum对应食物输出后续状态 if (foodNum <= 0.9) { System.out.println("That sugary cereal crash hits hard—you're feeling super tired now."); } else if (foodNum > 0.9 && foodNum <= 1.9) { System.out.println("The healthy snack keeps you steady; you're not too tired, just a bit lazy."); } else if (foodNum > 1.9 && foodNum <= 3) { System.out.println("The Tylenol kicks in, and the crackers settle your stomach—you feel refreshed, not tired at all."); } }
调用示例(在main方法中)
public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); double selectedFood = foodMethod(keyboard); tiredMethod(keyboard, selectedFood); keyboard.close(); }
方式二:将foodNum定义为类的静态成员变量
如果你的程序结构简单,也可以把foodNum改成类的静态变量,这样两个静态方法都能直接访问它。
修改后的类结构示例
import java.util.Scanner; public class YourClassName { // 定义静态成员变量,让两个方法都能访问 private static double foodNum; public static void foodMethod(Scanner keyboard) { foodNum = Math.random() * 3; // 直接给静态变量赋值 // 原有的输出逻辑不变 System.out.println("Now let's see..."); System.out.println(); if (foodNum <= 0.9) { System.out.println("You've decided on a sugary bowl of cereal!"); System.out.println("Although, that headache of yours isn’t going to go away anytime soon."); } if (foodNum > 0.9 && foodNum <= 1.9) { System.out.println("Hmm..."); System.out.println("Interesting"); System.out.println("Seems like someone decided to reach for the healthy option."); System.out.println("You settled on some yogurt and a tall glass of orange juice. Nice work!"); } if (foodNum > 1.9 && foodNum <= 3) { System.out.println("You end up grabbing some crackers and a glass of water to help you swallow some Tylenol to ease that headache of yours."); } } public static void tiredMethod(Scanner keyboard) { System.out.println("About an hour of being bored..."); // 直接使用静态变量foodNum if (foodNum <= 0.9) { System.out.println("That sugary cereal crash hits hard—you're feeling super tired now."); } else if (foodNum > 0.9 && foodNum <= 1.9) { System.out.println("The healthy snack keeps you steady; you're not too tired, just a bit lazy."); } else if (foodNum > 1.9 && foodNum <= 3) { System.out.println("The Tylenol kicks in, and the crackers settle your stomach—you feel refreshed, not tired at all."); } } public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); foodMethod(keyboard); tiredMethod(keyboard); keyboard.close(); } }
两种方式的对比
- 方式一更推荐:遵循单一职责原则,方法之间通过参数传递数据,耦合性低,代码更易维护。
- 方式二适合小型程序:实现简单,但静态变量会一直存在于内存中,且如果多个地方修改它容易引发问题。
内容的提问来源于stack exchange,提问作者Sarrah Sodawala
相关产品推荐
相关产品推荐

